Java 垄断财产多重所有人

Java 垄断财产多重所有人,java,Java,我正在用Java创建一个垄断程序,但我遇到了一个问题:当一个玩家在一处房产上登陆时,他总是会得到买家的购买权,即使它已经拥有 下面是两段有问题的代码: 物业类别: class Property{ public int ownerID = -1; public Player owner; public boolean isOwned; public int price, rent; // defined in Constructor public void

我正在用Java创建一个垄断程序,但我遇到了一个问题:当一个玩家在一处房产上登陆时,他总是会得到买家的购买权,即使它已经拥有

下面是两段有问题的代码:

物业类别:

class Property{
    public int ownerID = -1;
    public Player owner;
    public boolean isOwned;
    public int price, rent; // defined in Constructor

    public void onLanded(Player p){
        if(OwnerID < 0){
            p.offerProperty(this);
        } else if(ownerID != p.getID(){
            p.transfer(-rent);
            owner.transfer(rent);
            System.ou.println("bla bla");
        } else {
            System.out.println("bla bla");
        }
    }

    public void buy(Player p){
        p.transfer(-price);
        setOwner(p);
        p.assets.add(this);
    }

    public void setOwner(Player p){
        owner = p;
        ownerID = p.getID();
        isOwner = true;
    }
}
类属性{
public int ownerID=-1;
公共玩家所有者;
公共布尔同属;
public int price,rent;//在构造函数中定义
公共无效联机(玩家p){
如果(所有者ID<0){
p、 要约财产(本);
}else if(ownerID!=p.getID(){
p、 转让(租金);
业主。转让(租金);
System.ou.println(“bla-bla”);
}否则{
系统输出打印项次(“bla-bla”);
}
}
公开无效购买(玩家p){
p、 转让(价格);
设置所有者(p);
p、 资产。添加(本);
}
公共无效设置所有者(玩家p){
所有者=p;
ownerID=p.getID();
isOwner=true;
}
}
职业球员:

class Player{
    int id, money; // defined in Constructor
    ArrayList<Property> assets = new ArrayList<Property>();

    public void offerProperty(Property p){
        if(money >= p.getPrice()){
            System.out.println("wanna bu?");
            String inputAnswer = scanner.next();
            if(agreed(inpuAnswer))
                p.buy(this);
            }
    }

    public boolean agreed(String s){
        if(s.equals("yes") return true;
            return false;
    }

    public int getID(){
        return id;
    }

    public void transfer(int amount){
        money += amount;
    }
}
职业玩家{
int id,money;//在构造函数中定义
ArrayList资产=新的ArrayList();
公共财产无效(财产p){
如果(货币>=p.getPrice()){
System.out.println(“想要bu?”);
字符串inputswer=scanner.next();
如果(同意(inpuAnswer))
p、 买(这个);
}
}
公共布尔值(字符串s){
如果(s.equals)(“是”)返回true;
返回false;
}
公共int getID(){
返回id;
}
公共无效转移(整数金额){
货币+=金额;
}
}

现在,正如我所说,每次我运行游戏时,我都会发现,如果玩家在已经拥有的房产上着陆,他就有可能购买房产,而不必向业主支付租金。我已经寻找解决方案好几天了,我也尝试过其他方法来解决问题,但总是不成功。有人知道如何解决这个问题吗在类属性中,更改此方法:

public void buy(Player p) {
    if (owner == null) {
        p.transfer(-price);
        setOwner(p);
        p.assets.add(this);
    }
}

我发现了我的bug,但它实际上并不在我在问题中编写的代码中。:)

问题是,每次玩家移动时,他都会创建一个新的棋盘并在上面移动,因此该属性始终不归其所有。

如果(!isOwned)你也可以做
如果(!isOwned)
我也尝试过,如果(!isOwned)')和如果(ownerID<0),但它不起作用。实际上,如果属性已经拥有,它甚至不应该出现在方法buy中,我认为错误在方法onLanded()中,就像它忽略if语句一样。