Reputation: 35
My template opens with a menu of options. When the user chooses option 1, it asks them to input a number teamNumber
. One must instantiate the class Team
, then it writes it to an arraylist.
If there is at least one number in numberList
, the user can select option 2. It asks them to input any of the numbers from the arraylist and searches it. If the number they input is found, then you input the team member. It will write the input to a private arraylist located in class TeamMember
.
I'm very close to figuring this all out except for one problem. It seems that regardless of which stored team number I input after choosing option 2, the team member I add to teamList
is added to ALL team numbers, not just the one I want it to be added to. So, say in option 1 I added a team 1 and a team 2. Then in option 2, I type 1 and proceed to enter the member. It ends up adding the team member to both team 1 and 2. Is there a way to fix this? I tried a few options but can't seem to wrap my head around it.
public class Main {
public static void main(String[] args) {
int choosing;
Scanner scan = new Scanner(System.in);
String input;
int teamNumber;
boolean stayInLoop;
ArrayList<Team> numberList = new ArrayList<Team>();
do {
stayInLoop = true;
System.out.println("1. Add a new team");
System.out.println("2. Add a new team member");
System.out.println("3. View teams");
input = scan.nextLine();
if (input.equals("1")) {
System.out.println("Enter a team number:");
teamNumber = scan.nextInt();
scan.nextLine();
Team addTeam = new Team(teamNumber);
numberList.add(addTeam);
}
if (input.equals("2")){
boolean foundIt = false;
boolean valid = true;
System.out.println("Team number:");
teamNumber = scan.nextInt();
scan.nextLine();
for (int a = 0; a < numberList.size() && foundIt == false; a++){
Team addTeam = numberList.get(a);
if (addTeam.findTeam() == teamNumber) {
foundIt = true;
System.out.println("Enter first name of team member:");
String teamMemberFirstName = scan.nextLine();
System.out.println("Enter first initial of last name:");
char firstInitialLastName = scan.nextLine().charAt(0);
TeamMember inputTeamMember = new TeamMember(teamMemberFirstName, firstInitialLastName);
inputTeamMember.addMember(inputTeamMember, valid = true);
int teamSize = (inputTeamMember.size(valid = true));
System.out.println("Team " + teamNumber + " has " + teamSize + " members!");
}
}
if (foundIt == false) {
System.out.println("Try again.");
}
}
}while (stayInLoop == true;)
}}
TeamMember:
public class TeamMember {
//the code provided in the task had teamList set to private, so I'm assuming it's required to be that way. I added static in order to eventually be able to call it with a method to view the team in option 3. But if the only solution involves changing the list to public, then so be it.
private final String teamMemberFirstName;
private final char firstInitialLastName;
private static ArrayList<TeamMember> teamList = new ArrayList<>();
public TeamMember(String teamMemberFirstName, char firstInitialLastName) {
this.teamMemberFirstName = teamMemberFirstName;
this.firstInitialLastName = firstInitialLastName;
}
public int addMember(TeamMember member, boolean valid) {
valid = teamList.add(member);
return teamList.size();
}
public static int size(boolean valid) {
return teamList.size();
}
}
Upvotes: 0
Views: 46
Reputation: 7403
teamList is static, hence shared by all instances of TeamMember. Remove the static keyword.
Upvotes: 1