Reputation: 247
I'm just practicing Java and pretty new to this. I was just trying to create a Random Number Generator program which tracks a player's wins, losses, winning percentage and total winnings. The logic of the program is that a player gets 3 chances per session and the computer generates a random number which the player needs to guess or rather should match.
I have three classes currently: Game(which holds the main logic), Player(which should have the wins/losses etc.. is what I'm guessing) and RandomNumberGenerator(which generates random numbers).
I've got a start with the program but confused whether I need to declare gamesWon, gamesLost, totalWinnings, winPercent as separate variables in the player class? Any help would be appreciated.
Here's the player class so far:
public class Player {
private String name;
private int totalWinnings;
private int gamesWon;
private int gamesLost;
public Player() {
this.name = "default";
this.totalWinnings = 0;
this.gamesWon = 0;
this.gamesLost = 0;
}
public Player(String name) {
this.name = "default";
this.totalWinnings = 0;
this.gamesWon = 0;
this.gamesLost = 0;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getGamesWon() {
return gamesWon;
}
public void setGamesWon(int gamesWon) {
this.gamesWon += gamesWon;
}
public int getGamesLost() {
return gamesLost;
}
public void setGamesLost(int gamesLost) {
this.gamesLost += gamesLost;
}
public void setTotalWinnings(int totalWinnings) {
this.totalWinnings += totalWinnings;
}
public int getTotalWinnings() {
return totalWinnings;
}
}
Game Class:
public class Game {
private Player player;
private LuckyNumberGenerator lng;
public Game() {
player = new Player();
lng = new LuckyNumberGenerator();
}
private void eventLoop() {
Scanner scanner = new Scanner(System.in);
int choice = 0;
boolean exit = false;
while (!exit) {
System.out.println("Welcome to the Guessing Game");
System.out.println("==============================");
System.out.println("(1) Set Up New Player");
System.out.println("(2) Play One Round");
System.out.println("(3) Player Win Statistics");
System.out.println("(4) Display Game Help");
System.out.println("(5) Exit Game");
System.out.println("Choose an option: ");
try {
choice = Integer.parseInt(scanner.nextLine());
if (choice < 1 || choice > 5) {
System.err.println("Error : Choose an option between 1 and 5");
choice = 0;
}
} catch (NumberFormatException e) {
System.err.println("Error : Choose an option between 1 and 5");
choice = 0;
}
switch (choice) {
case 1:
createNewPlayer(scanner);
break;
case 2:
guessNumber(scanner);
break;
case 3:
printStatistics();
break;
case 4:
printHelp();
break;
case 5:
exit = true;
}
}
scanner.close();
}
}
Upvotes: 0
Views: 2133
Reputation: 26354
I have modified the constructor to accept the name.
public Player() {
Player("default");
}
public Player(String name) {
this.name = name;
this.winPercent = 0;
this.totalWinnings = 0;
this.gamesWon = 0;
this.gamesLost = 0;
}
Player is an object which consumes Game . So better to write the logic in Game and keep the Player as simple as possible.
Upvotes: 1
Reputation: 726
According to OO Design, the properties like gamesWon, gamesLost, totalWinnings and winPercent should be in the Player class only(Correct).
You have a parameterized Constructor which is same as Default or Zero parameterized Constructor. You have made no use of passing the name(String) as parameter as you are setting the name to "default" same string for every object irrespective of passed value.
Asking the user between from 1 to 100(choice) makes no sense as u have only 5 cases in switch.
You should not pass scanner object to other methods as It should be used only for scanning the data and close it immediately after use. You can define a scanner object in which ever method you need it.
Upvotes: 1