These are the steps I am stuck on in my programming assignment:
- Create a while loop that will run until the user types in the exit command at the menu. a. After the loop print a goodbye message.
- Within the loop determine which menu choice the user picked and do the appropriate action. The different possible actions are defined in the following steps. a. Don’t forget to take a new input from the user after you’ve processed their choice.
Here's my code I'm not sure what to change because when I input a character it won't display the if command it's assigned too.
import java.util.*;
public class Assignment5KJ
{
static Scanner scan = new Scanner(System.in);
public static void main(String[]args)
{
Pet pet1 = new Pet("Fido", 3);
Pet pet2 = new Pet("Furball", 1);
Pet pet3 = null;
System.out.println("Welcome to my pet store.");
list(scan);
do
{
if(list(scan).equals("A"))
{
System.out.println(pet1.getName() + " is " + pet1.getAge() + " years old and " + pet1.getAdopt() + ".");
System.out.println(pet2.getName() + " is " + pet2.getAge() + " year old and " + pet2.getAdopt() + ".");
if (pet3 != null)
{
System.out.println(pet3.getName() + " is " + pet3.getAge() + " years old and " + pet3.getAdopt() + ".");
}
list(scan);
}
else if(list(scan).equals('B'))
{
pet1.setAge(3);
pet2.setAge(1);
list(scan);
}
else if(list(scan).equals("C"))
{
if(pet3 != null)
{
System.out.println("The pet store is full.");
}
else
{
System.out.println("Enter a name for the new pet");
String name4 = scan.nextLine();
System.out.println("Enter an age for the new pet");
int age4 = scan.nextInt();
Pet pet4 = new Pet(name4, age4);
pet3 = pet4;
System.out.println(pet4.getName() + " has been added to the store.");
}
list(scan);
}
}
while(!list(scan).equals("E"));
System.out.println("Have a nice day.");
}
public static String list(Scanner scan) {
String list = "\n" + "A. List the pets in the store." +
"\n" + "B. Age up the pets" +
"\n" + "C. Add a new pet" +
"\n" + "D. Adopt a pet" +
"\n" + "E. Quit";
System.out.println("Type the letter to make your selection." + list);
scan = new Scanner(System.in);
char selection = Character.toUpperCase((scan.next().charAt(0)));
while (!(selection >= 'A' && selection <= 'E'))
{
System.out.println("Not a valid selection.");
selection = Character.toUpperCase((scan.next().charAt(0)));
}
return selection + "";
}
public static boolean isValidInput(char selection) {
return (selection >= 'A' && selection <= 'E');
}
}
public class Pet
{
private String name;
private int age;
private boolean adoptStatus;
public Pet()
{
name = "";
age = 0;
adoptStatus = false;
}
public Pet(String petName, int petAge)
{
name=petName;
age=petAge;
}
public void setName(String name1)
{
name=name1;
}
public String getName()
{
return name;
}
public void setAge(int age1)
{
age = age1++;
}
public int getAge()
{
return age;
}
public void Adopt()
{
adoptStatus = true;
}
public boolean getAdopt()
{
return adoptStatus;
}
}