HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaModerate

Menu to save and load football team information

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
teamsavemenufootballloadandinformation

Problem

I am a newb, and whilst I'm sure my formatting can be improved, I really want to know, how I can re-use my code for this simple program. The objective is simple, football game with a menu, that allows game status to be loaded and saved.

MainmenuTest.java

```
package mainmenutest;

/**
*
* @author Darren Estcourt
*/

import java.util.InputMismatchException;
import java.util.Scanner;

public class MainmenuTest extends premierLeagueClubs{

int clubChoice;

public MainmenuTest(){

//constructor
}

public static void main(String args[]){

MainmenuTest team = new MainmenuTest();

team.getInput();

} // end main method

public void getInput(){

boolean exit = false;
int option=0;
while (!exit) {

try{
Scanner in = new Scanner(System.in);

menu();
System.out.println("\n");
option = in.nextInt();
} // end try

catch(InputMismatchException e) {

} // end catch
switch (option) {

case 1:
chooseTeam();
exit = true;
break;

case 2:
createProfile();
exit = true;
break;

case 3:
DeserializeDemo load = new DeserializeDemo();
load.Deserialize(clubChoice);
exit = true;
break;

case 4:
credits();
exit = true;
break;

case 5:
exit = true;
System.out.println("Goodbye!");
break;

case 6:
SerializeDemo save = new SerializeDemo();
save.Serialize(clubChoice);
exit = true;
break;

default:
System.out.println("Invalid option");

} // end switch

} // end loop

} // end getInput

public int chooseTeam(){

boolean exit = false;
while (!exit) {
System.out.println("\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
System.out.println("Please enter an option 1-20 to select a team, or 21 to quit : ");
System.out.println("1. Arsenal");
System.out.println("2. Aston Villa");
System.out.println("3. Bournemouth");
System.out.println("4. Chelsea");
System.out.println("5. Crystal Palace");
System.out.println("6. Everton");
System.ou

Solution

DRY

Let's start with your methods arsenal(), astonVilla() and so on. From the title of the question, I think you're already aware these are problematic. Remember that while at its core your program is going to be made up of branches and loops, high-level languages like Java allow us to write in a way that the source code captures the structure of the task it's achieving. It helps to say in plain English what you actually want to do here:

I want to print a greeting message saying that the player is the new manager of the club they chose.

Your code more closely reflects:

I want to print one of many different greeting messages, depending on which club the player chose.

Like your code, this is technically correct, but is less successful in capturing the structure of what's similar and different between the messages. So trying a more literal interpretation of the first statement, we can write:

public void printGreeting(String clubName) {
    System.out.println("\n\n\n\n\n\n\n\n\n\n\n\n\n\n");

    System.out.println("You are the new manager of " + clubName);
    System.out.println("\n");
    System.out.println("Press any key to continue...");
    MainmenuTest back = new MainmenuTest();
    back.getInput();
}


Now instead of many very similar methods, we have one parameterized method. We've cut out a lot of repetition- all the aspects of the messages which were the same every time were being repeated over and over. This principle of not repeating information is called Don't Repeat Yourself.
SRP

You know what would make your game great? A graphical interface. Well, okay, maybe that's a more than you want to add at the moment. But what about a menu like in many keyboard-controlled games where instead of everything having a number, you have a current selected item in the menu, and you can move up and down, then press enter to select? Or, I dunno, maybe you want to highlight some words in different colours, or have the player type words rather than numbers or translate the game to Spanish.

Maybe (in fact, probably), you'll never want to do any of those things, but the important point to notice is that all of those are to do with how you present the game (get input/output), and none of them have any impact at all on how the game itself works. In reality, the rules and logic of your game are completely separate concerns to how you want to display your game. But in your code, they're so mixed together they'd be extremely difficult to pull apart. Any of the changes I mentioned would require changing almost everything.

So with this in mind, let's look at how we might print our club selection menu:

private String displayClubMenu(String[] clubs) {
    for(int i=0; i<clubs.length;i++) {
        System.out.printlin(i + ". " + clubs[i]);
    }

    Scanner in = new Scanner(System.in);     
    System.out.println("\n");
    choice = in.nextInt();

    return clubs[choice];
}


To this you'd pass an array of names of all your clubs, and it would return the selected club. Notice how this is only concerned with presentation concerns, whereas the part that we've now extracted (the club names) is now completely isolated from presentation. If we changed to a GUI, this method would be changed entirely, but the code which uses it could remain exactly the same.

This idea of each piece of code only being concerned with doing a single thing is called the Single Responsibility Principle. Eventually, you'll find that there are more sophisticated structures that can help you with this (in Java, that will be object-oriented programming), but even making sure a single method isn't mixing many responsibilities is important.

There's probably a lot more things to point out, and I'm sure other answers will. However from my perspective, these are the two key high-level concepts that will help you progress at this point.

Code Snippets

public void printGreeting(String clubName) {
    System.out.println("\n\n\n\n\n\n\n\n\n\n\n\n\n\n");

    System.out.println("You are the new manager of " + clubName);
    System.out.println("\n");
    System.out.println("Press any key to continue...");
    MainmenuTest back = new MainmenuTest();
    back.getInput();
}
private String displayClubMenu(String[] clubs) {
    for(int i=0; i<clubs.length;i++) {
        System.out.printlin(i + ". " + clubs[i]);
    }

    Scanner in = new Scanner(System.in);     
    System.out.println("\n");
    choice = in.nextInt();

    return clubs[choice];
}

Context

StackExchange Code Review Q#105595, answer score: 10

Revisions (0)

No revisions yet.