I have a problem in a class, Shipping, in my monopoly game. I want to find out how many shippings a player owns and thereby determine the rent. (It is a danish version of the game, so i think the rules differ from country to country). I use the Collections.frequency but for some reason it always goes to the else statement and gives me 8 times the rent. Here is the code:
package fields;
import java.util.Collections;
import java.util.Scanner;
public class Shipping extends Ownable
{
public int rent;
private int occurence;
private String yesOrNo;
private Shipping shipping;
Scanner scan = new Scanner(System.in);
public Shipping(int fieldNumber, String fieldName, int price)
{
super(fieldNumber, fieldName, price);
this.rent = 500;
}
public int getRent()
{
return rent;
}
@Override
public int getPrice()
{
return price;
}
@Override
public String getName()
{
return fieldName;
}
@Override
public int getNumber()
{
return fieldNumber;
}
@Override
public matador.Player getOwner()
{
return owner;
}
@Override
public void getConsequence()
{
if(getOwner() == null)
{
System.out.println("Do you want to buy it? (Y/N)");
yesOrNo = scan.next();
if(yesOrNo.equalsIgnoreCase("y"))
{
matador.Main.currentPlayer.money -= getPrice();
owner = matador.Main.currentPlayer;
matador.Main.currentPlayer.ownedGrounds.add((Shipping) data.board[matador.Main.currentPlayer.getPosition()]);
System.out.println(matador.Main.currentPlayer.getName()
+ " bought " + getName());
}
else
{
System.out.println(matador.Main.currentPlayer.getName() +
" did not buy " + data.board[matador.Main.currentPlayer.getPosition()].getName());
}
}
else if(getOwner() != matador.Main.currentPlayer)
{
occurence = Collections.frequency(owner.ownedGrounds, shipping);
if(occurence == 1)
{
System.out.println(matador.Main.currentPlayer.getName() + " needs to pay " +
owner.getName() + " " + getRent());
matador.Main.currentPlayer.money -= getRent();
owner.money += getRent();
}
else if(occurence == 2)
{
System.out.println(matador.Main.currentPlayer.getName() + " needs to pay " +
owner.getName() + " " + (2 * getRent()));
matador.Main.currentPlayer.money -= (2 * getRent());
owner.money += (2 * getRent());
}
else if(occurence == 3)
{
System.out.println(matador.Main.currentPlayer.getName() + " needs to pay " +
owner.getName() + " " + (4 * getRent()));
matador.Main.currentPlayer.money -= (4 * getRent());
owner.money += (4 * getRent());
}
else
{
System.out.println(matador.Main.currentPlayer.getName() + " needs to pay " +
owner.getName() + " " + (8 * getRent()));
matador.Main.currentPlayer.money -= (8 * getRent());
owner.money += (8 * getRent());
}
}
}
}
I think the problem is the object i try to get the frequency of, but i have no idea.
thanks in advance!
Regards!