Search code examples
javaclassobject

How to go back to a method in previous class in java?


I have these 2 classes where the main method in Association.class creates a teams object in the constructor.

I am using this teams object to go to the the teams menu in Teams.class. After this how can I call a method which will take it back to menu in the Association.class.

I was hoping I will call the associationMenu back by creating a object in teams. But that doesn't seem to work

Sample I/O

Welcome to the Association! Please make a selection from the menu:
1. Explore the teams.
2. Arrange a new season.
X. Exit the system.
Enter a choice: 1
Welcome to the Teams Page! Please make a selection from the menu:
1. Display all teams.
2. Display all players.
3. Add a new team.
4. Manage an existing team.
5. Delete an existing team.
6. Display all players by an level.
R. Return to previous menu.
Enter a choice: R
Welcome to the Association! Please make a selection from the menu:
1. Explore the teams.
2. Arrange a new season.
X. Exit the system.
Enter a choice: X
Done
public class Association{
    
    private Teams teams;
    private Season season;
   
    public Association(){ // take no parameters
        teams = new Teams();
    }
    
    public static void main(String[] args){
        new Association().associationMenu();  
    }

    public void associationMenu(){
        associationMenuHelp();
        char choice;
        while ((choice = readChoice()) != 'X' ){
            switch (choice) {
                case '1': exploreTeams(); break;
                case '2': newSeason(); break;
                default: associationMenu(); break;
            }
        }
        System.out.println("Done");
    }

    public void exploreTeams(){
        teams.teamsPage();
    }
import java.util.ArrayList;
public class Teams {
    public ArrayList<Team> teams = new ArrayList<Team>();
    public Teams(){         //take no parameters
        teams.add(new Team("Suns"));
        teams.add(new Team("Bulls"));
        teams.add(new Team("Hawks"));
        teams.add(new Team("Nets"));
    }

     public void teamsPage(){
        teamsPageHelp();
        char choice = readChoice();
        switch (choice) {
            case '1': displayAllTeams(); break;
            case '2': displayAllPlayers(); break;
            case '3': addTeam(); break;
            case '4': manageTeam(readTeam()); break;
            case '5': deleteTeam(); break;
            case '6': displayPlayersLevel(); break;
            //case 'R': goooooooback(); break;
            default: teamsPage(); break;
        }
    }

Solution

  • "After this how can I call a method which will take it back to menu in the Association.class."

    I think what you expect here is to go back to the user menu by selecting a option (in your case selecting 'R').

    I think you could achieve this by changing the public void associationMenu() method in to a static method in class Association like below.

    public static void associationMenu()

    Then you could directly call this method directly using class name without creating an instance/object of the Association class each time. See below call.

    Association.associationMenu()

    Ideally you could move this associationMenu static method in to a Utility class (example name MenuUtils) for the separation of concern and reusability.

    The below calling method segment could be changed accordingly by calling the earlier mentioned method call, which user could be redirected to the user menu upon 'R' selection input.

    public void teamsPage(){
        teamsPageHelp();
        char choice = readChoice();
        switch (choice) {
            case '1': displayAllTeams(); break;
            case '2': displayAllPlayers(); break;
            case '3': addTeam(); break;
            case '4': manageTeam(readTeam()); break;
            case '5': deleteTeam(); break;
            case '6': displayPlayersLevel(); break;
            case 'R': Association.associationMenu(); break;//Redirect to previous menu
            default: teamsPage(); break;
        }
    

    Then the user could re-enter input inside association menu to do whatever user wants.

    Please see the additional comment on refactoring the provided code.

    public ArrayList<Team> teams = new ArrayList<Team>();
    

    It is not a good thing to use public instance variables since it violates encapsulation. Better to restrict the access modifier levels with private and use accessor/getter method to access the variables.