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:
- Prints the menu
- Scans for input
- Converts that input to uppercase
- Checks if this value is "A","B","C","D", or "E"
- If not, it asks for another value
- 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!