[Java] Użycie metod subklasy w klasie głównej
Na zaliczenie laborek podjąłem się napisania gry monopoly w Javie. Niestety napotkałem pewne problemy w dość prostej wydaje się rzeczy.
Mianowicie, szkielet aplikacji wygląda póki co tak:
Kod:
public class Field {
protected int fieldPosition;
Field(){
}
}
Kod:
public class Property extends Field{
private String propertyGroup;
private String propertyOwner;
private String propertyName;
private float propertyPrice;
private boolean allowBuilding;
private int numberOfHouses;
private int numberOfHotels;
private float basicValue;
private float houseValue;
final private int maxNumberOfHouses = 4;
final private int getMaxNumberOfHotels = 1;
public Property(int fieldPosition, String propertyGroup, String propertyName, float propertyPrice, float basicValue, float houseValue){
this.fieldPosition = fieldPosition;
this.propertyGroup = propertyGroup;
propertyOwner = "none";
this.propertyName = propertyName;
this.propertyPrice = propertyPrice;
allowBuilding = false;
this.basicValue = basicValue;
this.houseValue = houseValue;
numberOfHotels = 0;
numberOfHotels = 0;
}
/*
Setters
*/
public void setAllowBuilding(boolean allowBuilding) {
this.allowBuilding = allowBuilding;
}
public void setPropertyOwner(String propertyOwner) {
this.propertyOwner = propertyOwner;
}
/*
Getters
*/
public String getPropertyGroup() { return propertyGroup; }
public String getPropertyOwner() {
return propertyOwner;
}
public String getPropertyName() {
return propertyName;
}
public float getPropertyPrice() {
return propertyPrice;
}
public boolean isAllowBuilding() {
return allowBuilding;
}
public int getNumberOfHouses() {
return numberOfHouses;
}
public int getNumberOfHotels() {
return numberOfHotels;
}
public int getMaxNumberOfHouses() {
return maxNumberOfHouses;
}
public float getBasicValue() {
return basicValue;
}
public float getHouseValue() {
return houseValue;
}
/*
Add and remove house and hotel methods
*/
public void addHouse(){
this.numberOfHouses = getNumberOfHouses() + 1;
this.propertyPrice = getPropertyPrice() + 100;
}
public void removeHouse(){
this.numberOfHouses = getNumberOfHouses() - 1;
this.propertyPrice = getPropertyPrice() - 100;
}
public void addHotel(){
this.numberOfHotels = getNumberOfHotels() + 1;
this.propertyPrice = getPropertyPrice() + 400;
}
public void removeHotel(){
this.numberOfHotels = getNumberOfHotels() - 1;
this.propertyPrice = getPropertyPrice() - 400;
}
@Override
public String toString() {
return propertyName + getNumberOfHouses();
}
}
Z klasy Field dziedziczyć będzie jeszcze kilka mniej złożonych klas jak Start, Idź do więzienia, Czerwona/Niebieska szansa, Property opisuje każdą rzecz którą można kupić.
Następnie tworzę klasę Board, która defacto jest listą obiektów Field
Kod:
public class Board {
Field board[] = new Field[40];
public Board() {
board[0] = new Start();
board[1] = new Property(1,"Katowice","Mariacka", 600, 100, 175);
board[2] = new Property(2, "Katowice","Stawowa", 750, 125, 215);
board[3] = new Property(3, "Katowice","Korfantego", 1000, 150, 250);
}
public Field[] getBoard(){
return board;
}
}
A za akcje wykonywane w grze odpowiada klasa Game, w której mam min. metodę buyProperty
Kod:
public void buyProperty(Player player, Field property){
if(player.getPlayerCash() >= property.getPropertyPrice() &&
(property.getPropertyOwner().equals("none"))){
player.addProperty(property);
property.setPropertyOwner(player.getPlayerName());
player.removeCash(property.getPropertyPrice());
}
}
Cytuj:
Cannot resolve method 'getPropertyPrice()'
Tutaj właśnie pojawia się problem, w metodzie buyProperty jako 2gi parametr muszę dać obiekt klasy Field, podczas gdy metody w środku, jak getPropertyPrice pochodzą z klasy Property. Przypominam, że Property dziedziczy po Field, więc nie mogę użyć metod Property w Field. Przesunięcie atrybutow i metody getPropertyPrice wyżej (tzn. do głównej klasy) niby powinno pomóc, ale nie potrzeba mi w klasie Field takich danych, bo ta klasa ma rozszerzać również pola które żadnej ceny nie mają.
Na koniec pokazuje jak chce się odwołać do wspomnianej metody by wszystko było jasne
Kod:
public static void main(String[] args) {
Board gameBoard = new Board();
Game game = new Game();
Player johny = new Player("Johny");
game.buyProperty(johny, gameBoard.getBoard()[1]);
}
Pytanie czy da się to jakoś sprytnie obejść czy cała aplikacja jest źle zaplanowana i dlatego takie coś wyszło?