Advertisement
binibiningtinamoran

DogDriver.java

Dec 1st, 2019
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.67 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Scanner;
  3.  
  4. public class DogDriver {
  5.  
  6.     public static void main(String[] args) {
  7.  
  8.         Scanner scanner = new Scanner(System.in);
  9.  
  10.         String username;
  11.         Dog chosenDog;
  12.         String yesOrNo;
  13.         ArrayList<Dog> arrayOfDogs;
  14.  
  15.         do {
  16.             arrayOfDogs = createListOfDogs();
  17.             username = getUserName(scanner);
  18.             chosenDog = chooseDog(scanner, arrayOfDogs);
  19.  
  20.             while (!(arrayOfDogs.contains(chosenDog))) {
  21.                 System.out.println("The dog you chose is not on the list. Choose again.");
  22.                 //username = getUserName(scanner);
  23.                 chosenDog = chooseDog(scanner, arrayOfDogs);
  24.             }
  25.  
  26.             assert chosenDog != null;
  27.             chosenDog.showInfo();
  28.             System.out.println("Adopt " + chosenDog.getName() + "?");
  29.             yesOrNo = scanner.nextLine();
  30.  
  31.             if (yesOrNo.equalsIgnoreCase("Yes")) {
  32.                 chosenDog.setOwner(username); // Set owner's Name from "None" to user's name
  33.                 System.out.println("Updated Information:");
  34.                 chosenDog.showInfo(); // Print the info again, this time with the correct owner's name
  35.             }
  36.         } while (yesOrNo.equalsIgnoreCase("no"));
  37.         // if the dog is not to be adopted, go back to the very beginning...
  38.     }
  39.  
  40.     private static String getUserName(Scanner s) {
  41.         System.out.print("What is your name? ");
  42.         String name = s.nextLine();
  43.         return name;
  44.     }
  45.  
  46.     private static Dog chooseDog(Scanner s, ArrayList<Dog> list) {
  47.         System.out.println("Here are dogs up for adoption: ");
  48.         for (Dog dog : list) {
  49.             System.out.println(dog.getName());
  50.         }
  51.  
  52.         System.out.print("\nType the name of the dog you chose: ");
  53.         String nameOfDog = s.nextLine();
  54.  
  55.         for (Dog dog : list) {
  56.             if (dog.getName().equalsIgnoreCase(nameOfDog)) {
  57.                 return dog;
  58.             }
  59.         }
  60.         return null;
  61.     }
  62.  
  63.     private static ArrayList<Dog> createListOfDogs() {
  64.         // Create an arraylist of Dog objects
  65.         ArrayList<Dog> list = new ArrayList <>();
  66.  
  67.         // Create Dog objects to be added to the arraylist
  68.         Dog dog1 = new Dog("Oscar","None");
  69.         Dog dog2 = new Dog("Bear","None");
  70.         Dog dog3 = new Dog("Charlie","None");
  71.         Dog dog4 = new Dog("Nico","None");
  72.         Dog dog5 = new Dog("Sheeba","None");
  73.  
  74.         // Add Dog objects to arraylist
  75.         list.add(dog1);
  76.         list.add(dog2);
  77.         list.add(dog3);
  78.         list.add(dog4);
  79.         list.add(dog5);
  80.  
  81.         return list;
  82.     }
  83.  
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement