извините, что раздражаю, я новичок в программировании и немного запутался.
Как связать переменные двух классов? т.е. получить переменную одного класса в другой? Я пытаюсь создать сценарий ООП для игровой платформы, где игрок может присоединиться к игре (максимум 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());
}
}