1

For some reason, after I add a pet named "Oliver" the main menu prints out twice with the "invalid choice" line along with it. I just need another set of eyes to look at it because I have been looking at it for hours on end and been fixing little mistakes to no avail.

The code when ran looks like this:

     /*Welcome to the pet store.Type the letter to make your selection
      A.  List the pets in the store.
      B.  Age up the pets
      C.  Add a new pet
      D.  Adopt a pet
      E.  Quit
      C
      Please type in a name
      Oliver
      Please type in an age
      22
      Oliver has just been added to the store!
      Welcome to the pet store.Type the letter to make your selection
      A.  List the pets in the store.
      B.  Age up the pets
      C.  Add a new pet
      D.  Adopt a pet
      E.  Quit
      Invalid choice
       Welcome to the pet store.Type the letter to make your selection
       A.  List the pets in the store.
       B.  Age up the pets
       C.  Add a new pet
       D.  Adopt a pet
       E.  Quit*/

Here is my main class code:

    private static void mainmenu(){
    System.out.println("Welcome to the pet store.Type the letter to make 
    your selection");
    System.out.println("A."+"  " + "List the pets in the store.");
    System.out.println("B."+"  " + "Age up the pets");
    System.out.println("C."+"  " + "Add a new pet");
    System.out.println("D."+"  " + "Adopt a pet");
    System.out.println("E."+"  " + "Quit");

    MainPets.Getuserinput();

}

public static String Getuserinput(){

    userinput=scan.nextLine();

    return userinput; 

}

   public static void main (String [] args){
    int pet3age;
    String pet3name;
    Pet Pet1=new Pet("Fido",3); 
    Pet Pet2=new Pet("Furball",1);
    Pet Pet3=null;
    int userinputint;

    MainPets.mainmenu();


     while(userinput.equals("A")||userinput.equals("B")||userinput.equals("C")||userinput.equals("D")||userinput.equals("E")){

         switch(userinput){
         case "C":

            if (Pet3!=null&&userinput.equals("C")){
                System.out.println("Sorry the store is full");
            }

            if(Pet3==null){ 
                System.out.println("Please type in a name");
                pet3name=scan.nextLine();
                System.out.println("Please type in an age");
                pet3age=scan.nextInt();
                Pet3=new Pet(pet3name,pet3age);
                System.out.println(pet3name + " has just been added to the store!");
            }
            MainPets.mainmenu();
            break;
            }
            }
        while(!userinput.equals("A")||!userinput.equals("B")||!userinput.equals("C")||!userinput.equals("D")||!userinput.equals("E")){
      System.out.println("Invalid choice");
      MainPets.mainmenu();
   }

Here is the class with all the methods:

public class Pet {
String Name; 
String AdoptionStatus; 
int Age;

public Pet() {}

public Pet(String Name, int Age) {
    this.Name = Name;
    this.Age = Age;
}

public void SetName(String namesetup) {
    Name = namesetup;
}

public String GetName() {
    return Name;
}

public int GetAge() {
    return Age;
}

public int ageincrease() {
    return Age++;
}

public String Getadoptionstatus() {
    return AdoptionStatus;
}

 public void Setadoptionstatustonotadopted(int petnumber) {
    AdoptionStatus="not adopted";
}

public void Setadoptionstatustoadopted(int petnumber){
    AdoptionStatus="adopted";
}

}
  • 4
    Please read about java naming conventins. It almost feels like you are violating them on purpose. Serioulsy: just how you wrote your code makes me turn around and do something else ... – GhostCat Oct 09 '17 at 12:59
  • Possible duplicate of [Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo()?](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-nextint-or-other-nextfoo) – Kevin Anderson Oct 09 '17 at 13:00
  • 1
    are you using case without a switch? Should you double check for any compilation errors first... – robot_alien Oct 09 '17 at 13:08
  • I don't think the code compile : missing switch, missing a } etc... – Tuco Oct 09 '17 at 13:10

1 Answers1

0

It looks like you're trying to use static as much as possible for practice of what it does?

Anyway, see below for a minimal example of which you can build on (i.e. it will let you enter 'C' as many times as you want to 'add' new pets).

static String petname, petage;
    public static void main(String[] args) {
        initialText();
        String userinput = userInput();
        while (userinput.equals("A") || userinput.equals("B") || userinput.equals("C") || userinput.equals("D") || userinput.equals("E")) {
            if(userinput.equals("C")){
                System.out.println("Please type in a name");
                petname = userInput();
                System.out.println("Please type in an age");
                petage = userInput();
                Pet p = new Pet(petname, petage);
                System.out.println(petname + " has been added to the store.");
            }
            else{
                System.out.println("Option not configured yet");
                //TODO - the rest of the options
            }
            initialText();
            userinput = userInput();
        }
    }

    public static void initialText() {
        System.out.println("Welcome to the pet store.Type the letter to make your selection");
        System.out.println("A." + "  " + "List the pets in the store.");
        System.out.println("B." + "  " + "Age up the pets");
        System.out.println("C." + "  " + "Add a new pet");
        System.out.println("D." + "  " + "Adopt a pet");
        System.out.println("E." + "  " + "Quit");
    }

    public static String userInput(){
        Scanner s = new Scanner(System.in);
        return s.nextLine();
    }

It's by no means perfect, just knocked it together very quickly to give you a chance to work on it.

achAmháin
  • 3,944
  • 3
  • 12
  • 39