3

My template opens with a menu of options and the user inputs something between 1-3 to select one of the three 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 a String teamMemberFirstName and a char firstInitialLastName. Then it will write the input to a private arraylist located in another class TeamMember.

Once they have input the info in option 1 and 2, they can choose option 3. It allows you to print the list of inputted names based on which team number you put them on.

I am not sure how, in option 3, to call the private arraylist from the TeamMember class teamList. Any guidance on how to proceed with this step? My code is below.

Main class:

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);
    System.out.println("Success!");
    }
  }
  if (foundIt == false) {
  System.out.println("Try again.");
  }
}

if (input.equals("3")){
  for (int a = 0; a < numberList.size(); a++) {
  Team addTeam = numberList.get(a);
  //Not sure what to put where there are ????'s - I tried a few ideas and stuff I found online, but nothing worked    
  //I assume I call the method/class here????
  System.out.println("Team: " + addTeam.findTeam() + " Members: " + 
  "I will put the member called from the arraylist here????"); 
  }
}
}while (stayInLoop == true;)
}}

TeamMember class:

public class TeamMember {

private final String teamMemberFirstName;
private final char firstInitialLastName;
private 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();
}
}
FormulaOne82
  • 35
  • 1
  • 4
  • Logically, your team member list should be in your `Team` class instead of the `TeamMember` class. Then just defined a getter to get the private list from the `Team` class. – Robby Cornelissen Feb 24 '16 at 04:28

3 Answers3

1

You cannot directly access private fields from other classes. Either move your list to the Team class or create a getter to retrieve the list.

  • From the way I interpreted the assignment it seems like the list is required to be private (at least that's how it is in what little code that was provided), so your second option may be what I'm looking for. I tried creating a getter in the TeamMember class to retrieve the list, but am unsure how to set it up in the main class. Any thoughts, or am I way off on that? – FormulaOne82 Feb 24 '16 at 05:03
  • ok well what im seeing in your code is that each time youre creating a team member youre creating a new `team list` this isnt going to work the way that you want. I'd move your teamList into the main class so you only have one copy – Arachnid Hivemind Feb 24 '16 at 05:10
0

In a public class, you can return a private object in a public method. This seems like the easiest way in this project. Add a new method to your TeamMember class, and have it return teamList:

//inside your TeamMember class, anywhere after you assign the private variable

public static ArrayList show(){ 
//the static keyword, in short, will make the method callable without a class instance.
    return teamList;
}

Since the TeamMember method show() is now static, you should be able to simply call TeamMember.show() and get the ArrayList.

Important note: In order for this to work, you must make the private arraylist static too. A static object cannot call a non-static object.

This will turn it into private static ArrayList<TeamMember> teamList = new ArrayList<>();

In the Main class, like I said above, simply call TeamMember.show(). You do not need to create an instance.

JustDucky
  • 132
  • 1
  • 10
  • teamList was made private for the assignment, so I'm not sure if I am allowed to change it. – FormulaOne82 Feb 24 '16 at 05:05
  • Oh. In that case, better safe than sorry. I'll edit the answer. – JustDucky Feb 24 '16 at 05:08
  • The new method works and I see where you're going to this. I'm not sure how to assign TeamMember.show() to a variable/call the method without creating a new instance of the class. That seems to be where I got stuck in the first place - whatever I try returns the error: non-static method show() cannot be reference from a static context – FormulaOne82 Feb 24 '16 at 05:40
  • Okay, I'll edit it again. – JustDucky Feb 24 '16 at 05:48
0

If you change your teamList to public instead of private your Main class will be able to access the variable. When you make something private in Java you're basically making that instance variable accessible only through the class that it's instantiated in. If you want the variable to be visible to other classes for reference you should make it public

Since the assignment calls for it, you're going to need to define a getter and setter for your 'teamList' variable.

public void setArray(ArrayList newTeamList){
    teamList = newTeamList;
}

public ArrayList getArray(){
    return teamList;
}

This'll allow you to access the private variable through the methods

Jodo1992
  • 745
  • 2
  • 10
  • 32
  • The assignment seems to require it to be private, as the line of code in TeamMember that declares the arraylist was provided to us, and it was set to private. – FormulaOne82 Feb 24 '16 at 05:05
  • Well that is just silly since there's literally no need to invoke a private declaration in this circumstance. If that is the case then you need to write both a getter and setter method for 'teamList', that way you can access the private instance variable from outside of the class. – Jodo1992 Feb 24 '16 at 05:20