链接两个类

时间:2018-10-26 19:07:01

标签: java oop

对不起,我是编程新手,有点困惑。

如何链接两个类的变量?即得到一类的变量到另一类?我正在尝试为一个游戏平台提供面向对象的游戏场景,在该平台上玩家可以加入游戏(每场游戏最多2个玩家,并支付相关费用。我;试图获得一种将这些总计在Game类中的方法但是我不确定如何将所有玩家“ amountOwed”加进去。我还必须确保两个玩家都不是同一个人?任何建议都非常感谢!这是我到目前为止的一切

import java.time.LocalDate;
import java.time.Period;
import java.util.Date;

public class Player {
    private static int instanceCounter = 0;
    private int playerId;
    private String playerName;
    public int birthDay;
    public int birthMonth;
    public int birthYear;
    public int playerAge;
    public double amountOwed;

    public Player(int playerId, String playerName, int birthDay, int birthMonth, int birthYear, double amountOwed) {
        this.playerId = playerId;
        this.playerName = playerName;
        this.birthDay = birthDay;
        this.birthMonth = birthMonth;
        this.birthYear = birthYear;
        this.amountOwed = amountOwed;
        instanceCounter++;
    }

    public double getAmountOwed() {
        return (amountOwed);
    }

    public int calculateAge() {
        LocalDate birthDate = LocalDate.of(birthYear, birthMonth, birthDay);
        LocalDate currentDate = LocalDate.now();
        return playerAge = Period.between(birthDate, currentDate).getYears();
    }

    public String printDetails() {
        return (playerName + ", with ID " + playerId + ", is " + playerAge + " and owes the game platform£" + amountOwed);

    }

    public String payFees() {
        amountOwed = 0;
        return ("Thank you" + playerName + ",you have paid for your game, your balance now stands at £0.00");
    }

    public String joinGameChess() {
        if (instanceCounter < 3) {
            return (playerName + " has joined the game.");
        } else {
            return ("Sorry, maximum of 2 players per game");
        }
    }

    public String leaveGame() {
        return (playerName + "has left the game.");
    }
}

public class Game {
    private String gameName;
    private int gameId;
    private int minAge;
    private double fee;
    public double amountOwed;
    public double totalFeesOwed;

    public Game(String gameName, int gameId, int minAge, double fee) {
        this.gameId = gameId;
        this.gameName = gameName;
        this.minAge = minAge;
        this.fee = fee;

    }

    public String printGameDetails() {
        return (gameName + "of ID " + gameId + "has a minimum age of " + minAge + " and costs " + fee + " to play");
    }
}

public class W07Practical {
    public static void main(String[] args) {

        Player marina = new Player(123, "Marina", 15, 4, 1999, 0);
        marina.calculateAge();
        System.out.println(marina.printDetails());

        Game chess = new Game("chess", 1234, 19, 2);

        marina.getAmountOwed();
        System.out.println(marina.joinGameChess());

        Player elise = new Player(153, "elise", 16, 3, 2000, 0);
        System.out.println(elise.joinGameChess());

        Player john = new Player(322, "john", 23, 5, 2002, 0);

        System.out.println(john.joinGameChess());

        System.out.println(john.printDetails());
        System.out.println(elise.printDetails());
    }
}

1 个答案:

答案 0 :(得分:0)

欠款总额:

amountOwed = player1.getAmountOwed() + player2.getAmountOwed();

您可以检查两个球员的姓名,或者通过比较他们的生日来更精确地确定他们的名字。为了更精确地比较两者