I am attempting to write a program that will allow a user to input four characteristics of a "Player" object and add that object to an array list of other Player objects. However, it seems that all Player objects are treated as the same.
I suspect that all the program is doing is filling the arraylist with many references to the same object with the same instance variables, but I cannot figure out a way to fill the arraylist with discrete objects that all have different instance variables.
If I try to make all fields in the Player class nonstatic I get the error "nonstatic variable/method cannot be referenced from a static context".
Driver Class Code
package playerdatamanager;
import java.util.*;
public class MainMenu {
public static void main(String[] args) {
ArrayList<Player> playerRoster = new ArrayList<>();
Scanner input = new Scanner(System.in);
int exitMenu = 0;
while (!"7".equals(exitMenu)) {
System.out.println("Please choose an option or press 7 to exit [1-7].");
System.out.println("1. " + "Add a player");
System.out.println("2. " + "Update a player");
System.out.println("3. " + "Delete a player");
System.out.println("4. " + "List all players");
System.out.println("5. " + "Calculate the average BMIs for the players");
System.out.println("6. " + "Display the details of the tallest player");
System.out.println("7. " + "Exit");
int menuOption = Integer.parseInt(input.nextLine());
switch (menuOption) {
case (1):
System.out.println("Please enter the name of the player.");
String newPlayerName = input.nextLine();
System.out.println("Please enter the jersey number of the player.");
int newPlayerJerseyNumber = Integer.parseInt(input.nextLine());
System.out.println("Please enter the height of the player.");
double newPlayerHeight = Double.parseDouble(input.nextLine());
System.out.println("Please enter the weight of the player.");
double newPlayerWeight = Double.parseDouble(input.nextLine());
playerRoster.add(new Player(newPlayerName, newPlayerJerseyNumber, newPlayerHeight, newPlayerWeight));
break;
case (2):
System.out.println("Which player would you like to update?");
System.out.println(playerRoster.toString());
int playerIndex = Integer.parseInt(input.nextLine());
System.out.println("Selected" + playerRoster.get(playerIndex).toString());
System.out.println("Enter the value you would like to change[1. name ,2. jersey number,3. weight,4. height]");
int userValueUpdate = Integer.parseInt(input.nextLine());
switch (userValueUpdate) {
case (1):
System.out.println("Please enter the new value for the player's name.");
Player.setName(input.nextLine());
break;
case (2):
System.out.println("Please enter the new value for the player's jersey number.");
Player.setJerseyNumber(Integer.parseInt(input.nextLine()));
break;
case (3):
System.out.println("Please enter the new value for the player's weight.");
Player.setWeightInPounds(Double.parseDouble(input.nextLine()));
break;
case (4):
System.out.println("Please enter the new value for the player's height.");
Player.setHeightInInches(Double.parseDouble(input.nextLine()));
break;
}
break;
case (3):
System.out.println("Which player would you like to delete?");
int userDeletion = Integer.parseInt(input.nextLine());
playerRoster.toString();
playerRoster.remove(userDeletion);
break;
case (4):
for (int i = 0; i < playerRoster.size();i++) {
System.out.print("Name: " + playerRoster.get(i).getName() + "\nHeight: " + Double.toString(playerRoster.get(i).getHeight()) + "\nWeight: " + Double.toString(playerRoster.get(i).getWeight()) + "\nJersey Number: " + Integer.toString( playerRoster.get(i).getJerseyNumber()) + "\n\n");
}
break;
}
}
}
}
Player class code
package playerdatamanager;
/**
*
* @author Austin
*/
public class Player {
private static String name;
private static int jerseyNumber;
private static double height;
private static double weight;
/**
* The default constructor for the Player object.
*/
public Player() {
}
/**
* Overloaded constructor to initialize Player object with user values.
*
* @param name The name of the Player
* @param jerseyNumber The Jersey Number of the Player
* @param height The height of the Player
* @param weight
*/
public Player(String name, int jerseyNumber, double height, double weight) {
this.name = name;
this.jerseyNumber = jerseyNumber;
this.height = height;
this.weight = weight;
}
/**
* method to calculate the BMI of a given player.
*
* @param height The height of a player.
* @param weight The weight of a player.
* @return the BMI of a player.
*/
/**
* setter method to set the player's name.
*
* @param userName The name given by the user.
*/
public static void setName(String userName) {
name = userName;
}
/**
* setter method to set a player's jersey number.
*
* @param userJerseyNumber The jersey number given by the user.
*/
public static void setJerseyNumber(int userJerseyNumber) {
jerseyNumber = userJerseyNumber;
}
/**
* setter method to set a player's height in inches.
*
* @param userHeight The height in inches given by the user.
*/
public static void setHeightInInches(double userHeight) {
height = userHeight;
}
/**
* setter method to set a player's weight in pounds.
*
* @param userWeight The weight in pounds given by the user.
*/
public static void setWeightInPounds(double userWeight) {
weight = userWeight;
}
/**
* getter method to retrieve the name of a player.
*
* @return The player's name.
*/
public static String getName() {
return name;
}
/**
* getter method to retrieve the jersey number of a player.
*
* @return The player's jersey number.
*/
public static int getJerseyNumber() {
return jerseyNumber;
}
/**
* getter method to retrieve the weight in pounds of a player.
*
* @return The player's weight in pounds.
*/
public static double getWeight() {
return weight;
}
/**
* getter method to retrieve the height in inches of a player.
*
* @return The player's height in inches.
*/
public static double getHeight() {
return height;
}
public static double BMICalculation() {
double BMI;
BMI = 703 * (getWeight() / (getHeight() * getHeight()));
return BMI;
}
public static void playerRosterToString(double height, double weight, int jerseyNumber, String name) {
String playerStats = "Name: " + name + "\nHeight: " + Double.toString(height) + "\nWeight: " + Double.toString(weight) + "\nJersey Number: " + Integer.toString(jerseyNumber);
System.out.print(playerStats);
}
}
The user should be able to input a player's name, jersey number, weight, and height. Then the user should be able to read it anytime they like.
However, if a user inputs player name "Babe Ruth", jersey number 1, weight 155, and height 70 that information will be stored. Then, if a user inputs player name "Joe Dimaggio", jersey number 2, weight 170, and height 75 the program doesn't store any information for Joe Dimaggio, but it will store Babe Ruth's attributes.
If a user lists all players, the output is:
Name: Babe Ruth
Jersey number: 1
Weight: 155
Height: 70
Name: Babe Ruth
Jersey number: 1
Weight: 155
Height: 70
when it should be:
Name: Babe Ruth
Jersey number: 1
Weight: 155
Height: 70
Name: Joe Dimaggio
Jersey number: 2
Weight: 170
Height: 75
static
from all the class member variables. Go back and review whatstatic
means when applied to class members.private
, so if you need access to them, add "getter" methods for the attributes you need to retrieve. If that's not the answer you need, explain why you think you can't use it inmain()
static
qualifiers in thePlayer
class.Player
, and usenew
somewhere in there...