Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Scanner;
- public class DogDriver {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String username;
- Dog chosenDog;
- String yesOrNo;
- ArrayList<Dog> arrayOfDogs;
- do {
- arrayOfDogs = createListOfDogs();
- username = getUserName(scanner);
- chosenDog = chooseDog(scanner, arrayOfDogs);
- while (!(arrayOfDogs.contains(chosenDog))) {
- System.out.println("The dog you chose is not on the list. Choose again.");
- //username = getUserName(scanner);
- chosenDog = chooseDog(scanner, arrayOfDogs);
- }
- assert chosenDog != null;
- chosenDog.showInfo();
- System.out.println("Adopt " + chosenDog.getName() + "?");
- yesOrNo = scanner.nextLine();
- if (yesOrNo.equalsIgnoreCase("Yes")) {
- chosenDog.setOwner(username); // Set owner's Name from "None" to user's name
- System.out.println("Updated Information:");
- chosenDog.showInfo(); // Print the info again, this time with the correct owner's name
- }
- } while (yesOrNo.equalsIgnoreCase("no"));
- // if the dog is not to be adopted, go back to the very beginning...
- }
- private static String getUserName(Scanner s) {
- System.out.print("What is your name? ");
- String name = s.nextLine();
- return name;
- }
- private static Dog chooseDog(Scanner s, ArrayList<Dog> list) {
- System.out.println("Here are dogs up for adoption: ");
- for (Dog dog : list) {
- System.out.println(dog.getName());
- }
- System.out.print("\nType the name of the dog you chose: ");
- String nameOfDog = s.nextLine();
- for (Dog dog : list) {
- if (dog.getName().equalsIgnoreCase(nameOfDog)) {
- return dog;
- }
- }
- return null;
- }
- private static ArrayList<Dog> createListOfDogs() {
- // Create an arraylist of Dog objects
- ArrayList<Dog> list = new ArrayList <>();
- // Create Dog objects to be added to the arraylist
- Dog dog1 = new Dog("Oscar","None");
- Dog dog2 = new Dog("Bear","None");
- Dog dog3 = new Dog("Charlie","None");
- Dog dog4 = new Dog("Nico","None");
- Dog dog5 = new Dog("Sheeba","None");
- // Add Dog objects to arraylist
- list.add(dog1);
- list.add(dog2);
- list.add(dog3);
- list.add(dog4);
- list.add(dog5);
- return list;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement