|
@@ -0,0 +1,74 @@
|
|
1
|
+package io.zipcoder.polymorphism;
|
|
2
|
+
|
|
3
|
+import java.util.ArrayList;
|
|
4
|
+import java.util.InputMismatchException;
|
|
5
|
+import java.util.Scanner;
|
|
6
|
+
|
|
7
|
+public class Console {
|
|
8
|
+
|
|
9
|
+ static ArrayList<Pet> pets = new ArrayList<Pet>();
|
|
10
|
+
|
|
11
|
+ public static void getInformation(){
|
|
12
|
+ String type = getPetType();
|
|
13
|
+ String name = getName(type);
|
|
14
|
+ int age = getAge(name);
|
|
15
|
+ createNewPet(type, name, age);
|
|
16
|
+ }
|
|
17
|
+
|
|
18
|
+ public static int getAmountOfPets(){
|
|
19
|
+ Scanner scanner = new Scanner(System.in);
|
|
20
|
+ System.out.println("How many pets do you have?");
|
|
21
|
+ try{
|
|
22
|
+ int numOfPets = scanner.nextInt();
|
|
23
|
+ return numOfPets;
|
|
24
|
+ }catch (InputMismatchException E){
|
|
25
|
+ System.out.println("Not a number, try again.");
|
|
26
|
+ getAmountOfPets();
|
|
27
|
+ return 0;
|
|
28
|
+ }
|
|
29
|
+ }
|
|
30
|
+
|
|
31
|
+ public static String getPetType(){
|
|
32
|
+ Scanner scanner = new Scanner(System.in);
|
|
33
|
+ System.out.println("What kind of pet?");
|
|
34
|
+ String petType = scanner.nextLine();
|
|
35
|
+ return petType;
|
|
36
|
+ }
|
|
37
|
+
|
|
38
|
+ public static int getAge(String name){
|
|
39
|
+ Scanner scanner = new Scanner(System.in);
|
|
40
|
+ System.out.println("How old is " + name + "?");
|
|
41
|
+ int petAge = scanner.nextInt();
|
|
42
|
+ return petAge;
|
|
43
|
+ }
|
|
44
|
+
|
|
45
|
+ public static String getName(String type){
|
|
46
|
+ Scanner scanner = new Scanner(System.in);
|
|
47
|
+ System.out.println("Whats your " + type + "'s name?");
|
|
48
|
+ String petName = scanner.nextLine();
|
|
49
|
+ return petName;
|
|
50
|
+ }
|
|
51
|
+
|
|
52
|
+ public static void petForLoop(int numOfPets){
|
|
53
|
+ for(int i = 0; i < numOfPets; i++){
|
|
54
|
+ getInformation();
|
|
55
|
+ }
|
|
56
|
+ }
|
|
57
|
+
|
|
58
|
+ public static void createNewPet(String petType, String name, int age){
|
|
59
|
+ if(petType.equalsIgnoreCase("Dog")){
|
|
60
|
+ pets.add(new Dog(name, age));
|
|
61
|
+ }else if(petType.equalsIgnoreCase("Cat")){
|
|
62
|
+ pets.add(new Cat(name, age));
|
|
63
|
+ }else if(petType.equalsIgnoreCase("Horse")){
|
|
64
|
+ pets.add(new Horse(name, age));
|
|
65
|
+ }
|
|
66
|
+ }
|
|
67
|
+
|
|
68
|
+ public static void petList(){
|
|
69
|
+ for (Pet pet: pets) {
|
|
70
|
+ System.out.println("Your " + pet.getClass().getSimpleName() + " is named " + pet.getName() + " says " + pet.speak() + " and is " + pet.getAge() + " year old.");
|
|
71
|
+ }
|
|
72
|
+ }
|
|
73
|
+
|
|
74
|
+}
|