0

For my programming class, I have the following assignment:

In this assignment you will write a program that will model a pet store. The program will have a Pet class to model individual pets and the Assignment5 class will contain the main and act as the pet store. Users will be able to view the pets, make them age a year at a time, add a new pet, and adopt any of the pets.

Create a private static String method which prints out the main menu of the program. It then accepts a String from the user and returns their choice. The commands to list are as follows. a. List the pets in the store. b. Age up the pets. c. Add a new pet. d. Adopt a pet. e. Quit. i. Your method must verify that the user typed in a valid input before returning the input.

I've got my code so that it:

  1. Prints the menu
  2. Scans for input
  3. Converts that input to uppercase
  4. Checks if this value is "A","B","C","D", or "E"
  5. If not, it asks for another value
  6. If so, a section of code is executed

What I'm having the most difficulty with is where I should be placing this method, the scanner, and the rest of my code. An explanation of where I should put each and why would be extremely helpful.

Below is some of my code: import java.util.Scanner;

public class Assignment5{

// Creates a new scanner
Scanner scan = new Scanner(System.in);

private static String mainMenu(Scanner scan){
        // Print menu and ask user for input
        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");
        System.out.print("Type a letter to make your selection: ");

        // Scan for input. Convert to uppercase
        String letter = scan.next().toUpperCase();

        // Check if letter is valid. Return true or false
        public static boolean isValidInput(String letter) {
            return (letter == "A" || letter == "B" || letter == "C" || letter == "D" || letter == "E");
        }

        // If isValidInput is false, ask the user to input another letter.
        while (!isValidInput(letter){
            System.out.println("That is not one of the options. Input another letter.");
            letter = scan.next().toUpperCase();
        }
        return letter;
    }
public static void main(String[] args){





    // Create two pets
    Pet one = new Pet("Spot", 3);
    Pet two = new Pet("Fluffy", 24);
    // Initially, pet #3 has no values attached to it
    Pet three = null;

    // Initial greeting
    System.out.println("Welcome to the pet store!");
    mainMenu(scan);


    // A: List the pets
    if (mainMenu(scan) == "A"){
        System.out.println("Listing pets...");
        System.out.println(one.getName() + " is " + one.getAge() + "-years-old and is currently " + one.getStatus());
        System.out.println(two.getName() + " is " + two.getAge() + "-years-old and is currently " + two.getStatus());
        // Check if there is a third pet before printing its information
        if (three != null){
            System.out.println(three.getName() + " is " + three.getAge() + "-years-old and is currently " + three.getStatus());
        }
        mainMenu(scan);
    }

there is more code afterward, but my teacher doesn't like us to post our full code because others might find it and copy it.

Thanks!

Emily
  • 43
  • 8
  • If you don't know the where, start with the how. Initialize a Scanner, then put it into different parts of the code that you think are likely candidates and see if it works. Right now you are just asking us to do homework for you. – Mad Physicist Oct 05 '17 at 20:38
  • If your code runs correctly, it's in the right place. Does your code run correctly? – Mad Physicist Oct 05 '17 at 20:40
  • You want to use a loop that contains the call to `mainMenu()`. `while (true) {String action = mainMenu(scan); doSomething(action);}` – 001 Oct 05 '17 at 20:42
  • 1
    `letter == "A"` -> [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Pshemo Oct 05 '17 at 20:50

1 Answers1

1

I would say the placement of your method and main method looks good. Generally the placement of those will depend with who you work with and coding styles that people around you use, but plain methods before the main method the way you have it works well

As for the scanner. Having it at the top like you have it, so it can be reused for both your main and your main menu method is good. I would include an Access Modifier (public, protected, or private) on it.

For a rough estimate though all your initializations will go first though (such as how you initialized your scanner) then your methods (some people order these by the order in which they are used and some people order these by the access modifier tied to them) then your main method coming last is pretty standard.

Not entirely sure this is what you were asking, but I believe it was something similar to this.

G. Weaver
  • 51
  • 2