import java.util.ArrayList;
import java.util.Scanner;
public class Driver {
private static ArrayList dogList = new ArrayList();
private static ArrayList monkeyList = new ArrayList();//list of current monkeys
// Instance variables (if needed)
public static char userSelection; //allows variable to be used in printAnimals method
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
initializeDogList();
initializeMonkeyList();
do {
displayMenu();
userSelection = scnr.next().charAt(0); //users menu selection input
switch (userSelection) {
case '1': //calls intakeNewDog method
intakeNewDog(scnr);
break;
case '2': //calls intakeNewMonkey Method
intakeNewMonkey(scnr);
break;
case '3': //calls reserveAnimal Method
reserveAnimal(scnr);
break;
case '4': //calls printAnimals method to print dogList array
printAnimals();
break;
case '5': //calls printAnimals method to print monkeyList array
printAnimals();
break;
case '6': //calls printAnimals method and prints list of available animals
printAnimals();
break;
case 'q': //
System.out.println("Thanks for using our system, goodbye!");
System.exit(0);
default: //handles input validation
System.out.println("Invalid selection, please try again.");
break;
}
} while (userSelection != 'q'); //keeps loop until user selection is 'q'
}
// This method prints the menu options
public static void displayMenu() {
System.out.println("nn");
System.out.println("ttttRescue Animal System Menu");
System.out.println("[1] Intake a new dog");
System.out.println("[2] Intake a new monkey");
System.out.println("[3] Reserve an animal");
System.out.println("[4] Print a list of all dogs");
System.out.println("[5] Print a list of all monkeys");
System.out.println("[6] Print a list of all animals that are not reserved");
System.out.println("[q] Quit application");
System.out.println();
System.out.println("Enter a menu selection");
}
// Adds dogs to a list for testing
public static void initializeDogList() {
Dog dog1 = new Dog("Spot", "German Shepherd", "male", "1", "25.6", "05-12-2019", "United States", "intake", false, "United States");
Dog dog2 = new Dog("Rex", "Great Dane", "male", "3", "35.2", "02-03-2020", "United States", "Phase I", true, "United States");
Dog dog3 = new Dog("Bella", "Chihuahua", "female", "4", "25.6", "12-12-2019", "Canada", "in service", true, "Canada");
dogList.add(dog1);
dogList.add(dog2);
dogList.add(dog3);
}
// Adds monkeys to a list for testing
public static void initializeMonkeyList() {
Monkey monkey1 = new Monkey("George", "Squirrel Monkey", "male", "3", "2.1", "05-12-2019", "United States", "in service", false, "United States", "15.2", "11.3", "12.4" );
Monkey monkey2 = new Monkey("Kong", "Marmoset", "male", "1", "1.89", "02-03-2020", "United States", "Phase I", false, "United States", "8.7", "6.8", "7.4");
Monkey monkey3 = new Monkey("Matilda", "Capuchin", "female", "4", "6.54", "12-12-2019", "Canada", "in service", false, "Canada", "17.6", "16.29", "14.3");
monkeyList.add(monkey1); //adds monkey1, monkey2, monkey3 to monkeyList with their attributes
monkeyList.add(monkey2);
monkeyList.add(monkey3);
}
//intakeNewDog method
public static void intakeNewDog(Scanner scanner) {
String name;
System.out.println("What is the dog's name?");
name = scanner.nextLine();
for(Dog dog: dogList) { //loop to validate if name does not already exist in dogList
if(dog.getName().equalsIgnoreCase(name)) {
System.out.println("nnThis dog is already in our systemnn");
return; //returns to menu
}
}
scanner.nextLine();
//Asks user for dogs info
System.out.println("What is the dogs's breed?");
String breed = scanner.nextLine();
System.out.println("What is the dogs's gender?");
String gender = scanner.nextLine();
System.out.println("What is the dogs's age?");
String age = scanner.nextLine();
System.out.println("What is the dogs's weight?");
String weight = scanner.nextLine();
System.out.println("What is the dogs's aquisition date? Use the following format: __-__-__");
String aquisitionDate = scanner.nextLine();
System.out.println("What is the dogs's aquisition country?");
String aquisitionCountry = scanner.nextLine();
System.out.println("What is the dogs's trainging status? New dogs will always be 'Intake'.");
String trainingStatus = scanner.nextLine();
System.out.println("Is this dog reserved? Enter True for yes or False for no:");
boolean reserved = scanner.nextBoolean();
System.out.println("Which country will this dog be in service?");
String inServiceCountry = scanner.nextLine();
// Creates instantiated object and adds to dogList
Dog new_Dog = new Dog(name,
breed,
gender,
age,
weight,
aquisitionDate,
aquisitionCountry,
trainingStatus,
reserved,
inServiceCountry);
dogList.add(new_Dog);
System.out.println("This dog has been added to the system."); //confirmation and return to menu
return;
}
public static void intakeNewMonkey(Scanner scanner) {
//array list of allowed monkeys to validate user input for species
ArrayList<String> allowedMonkeysList = new ArrayList<String>();
allowedMonkeysList.add("Capuchin");
allowedMonkeysList.add("Guenon");
allowedMonkeysList.add("Macaque");
allowedMonkeysList.add("Marmoset");
allowedMonkeysList.add("Squirrel Monkey");
allowedMonkeysList.add("Tamarin");
//loop to validate if monkeys name already exist within monkeyList
System.out.println("What is the monkey's name?");
String name = scanner.nextLine();
for(Monkey monkey: monkeyList) {
if(monkey.getName().equalsIgnoreCase(name)) {
System.out.println("nnThis monkey is already in our systemnn");
return;
}
}
scanner.nextLine(); //allows user to input string name
System.out.println("What is the monkey's species?");
String species = scanner.nextLine();
if (allowedMonkeysList.contains(species)!= true) {
System.out.println("This species of monkey is not eligible to be an rescue animal.");
return;
}
//Asks user for monkey's info
System.out.println("What is the monkey's gender?");
String gender = scanner.nextLine();
System.out.println("What is the monkey's age?");
String age = scanner.nextLine();
System.out.println("What is the monkey's weight?");
String weight = scanner.nextLine();
System.out.println("What is the monkey's aquisition date?");
String aquisitionDate = scanner.nextLine();
System.out.println("What is the monkey's aquisition country?");
String aquisitionCountry = scanner.nextLine();
System.out.println("What is the monkey's trainging status? New monkeys will always be 'Intake'.");
String trainingStatus = scanner.nextLine();
System.out.println("Is this monkey reserved? Enter true for yes or false for no: ");
boolean reserved = scanner.nextBoolean();
System.out.println("Which country will this monkey be in service?");
String inServiceCountry = scanner.nextLine();
System.out.println("Since this is a monkey, we'll need additional information."); //Monkey needs additional info not used by dog
System.out.println("What is the monkey's tail length?");
String tailLength = scanner.nextLine();
System.out.println("What is the monkey's height?");
String height = scanner.nextLine();
System.out.println("What is the monkey's dody height?");
String bodyLength = scanner.nextLine();
//instantiates new_monkey object and adds to monkeyList array list
Monkey new_Monkey = new Monkey(name,
species,
gender,
age,
weight,
aquisitionDate,
aquisitionCountry,
trainingStatus,
reserved,
inServiceCountry,
tailLength,
height,
bodyLength);
monkeyList.add(new_Monkey);
System.out.println("This monkey has been added to the system.");
}
}
如果用户的输入和ArrayList
匹配,则intakeNewDog
和intakeNewMonkey
方法的输入验证将无法正确执行并返回菜单。它只是继续到代码的其余部分,并执行其余的print语句,而不管名称是否匹配。我尝试删除增强的for循环,并使用.contains()
方法验证输入,但没有成功。我们还没有讨论过使用列表流,所以我不知道如何从这个角度来处理这个问题。
这就是扫描仪的工作原理。
next((和hasNext((方法及其基元类型伴随方法(如nextInt(和hasNextInt(((首先跳过任何与分隔符模式匹配的输入,然后尝试返回下一个令牌。
而nextLine((前进到行的末尾并返回跳过的字符。
假设用户输入一个单词,然后点击Enter。如果调用next((,Scanner将返回下一个标记(用户输入的单词(,但不会从输入中删除行尾字符。随后的nextLine((将读取当前行的其余部分,即空字符串。
在这种情况下,您可以简单地使用nextLine((,而不是User@kiuby_88已经指出的next((。通常,例如,如果您使用其他nextXXX((方法之一,您可能希望在读取"真实"下一行之前,通过调用nextLine((来丢弃当前行上的剩余输入。