|
@@ -1,5 +1,7 @@
|
1
|
1
|
package io.zipcoder.polymorphism;
|
2
|
2
|
|
|
3
|
+import java.sql.SQLOutput;
|
|
4
|
+import java.util.ArrayList;
|
3
|
5
|
import java.util.Scanner;
|
4
|
6
|
|
5
|
7
|
/**
|
|
@@ -7,64 +9,116 @@ import java.util.Scanner;
|
7
|
9
|
*/
|
8
|
10
|
public class MainApplication {
|
9
|
11
|
|
|
12
|
+ ArrayList<Pet> pets = new ArrayList<>();
|
|
13
|
+
|
|
14
|
+ int numberOfPets = 0;
|
|
15
|
+ String typeOfPet = "";
|
|
16
|
+ String nameOfPet = "";
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+ Scanner scanner = new Scanner(System.in);
|
|
21
|
+
|
10
|
22
|
public static void main(String[] args) {
|
11
|
23
|
|
|
24
|
+
|
|
25
|
+ MainApplication app = new MainApplication();
|
|
26
|
+
|
|
27
|
+ //int numberOfPets;
|
|
28
|
+
|
|
29
|
+
|
12
|
30
|
Scanner scanner = new Scanner(System.in);
|
13
|
31
|
StringBuilder builder1 = new StringBuilder();
|
14
|
32
|
StringBuilder builder2 = new StringBuilder();
|
15
|
33
|
|
16
|
|
- String typeOfPet = "";
|
17
|
|
- String nameOfPet = "";
|
18
|
34
|
|
19
|
|
- System.out.println("How many pets do you currently own? You can include your kids as well! :)");
|
20
|
|
- int numberOfPets = Integer.parseInt(scanner.nextLine());
|
|
35
|
+ app.askAmount();
|
|
36
|
+ String petTypes = app.askType().toString();
|
|
37
|
+ String petNames = app.askName().toString();
|
21
|
38
|
|
|
39
|
+ System.out.println("Your pets seem amazing, cant believe you have " + app.numberOfPets + " that are " + petTypes + " named " + petNames);
|
22
|
40
|
|
23
|
|
- for (int i = 0; i < numberOfPets; i++) {
|
24
|
|
- typeOfPet = scanner.nextLine();
|
25
|
|
- builder1.append(typeOfPet);
|
26
|
|
- if (i < numberOfPets) {
|
27
|
|
- System.out.println("Do you have more pets? If so what kind is the next?");
|
28
|
|
- builder1.append(", ");
|
29
|
|
- }
|
|
41
|
+ app.allPetsSpeak();
|
30
|
42
|
|
31
|
43
|
|
32
|
|
- System.out.println("What's the first one's name?");
|
|
44
|
+ }
|
33
|
45
|
|
34
|
|
- for (int j = 0; j < i; j++) {
|
35
|
|
- nameOfPet = scanner.nextLine();
|
36
|
|
- builder2.append(nameOfPet);
|
37
|
|
- if (j < numberOfPets) {
|
38
|
|
- System.out.println("Do you have more pets? If so what kind is the next?");
|
39
|
|
- builder2.append(", ");
|
40
|
46
|
|
41
|
|
- }
|
42
|
|
- }
|
43
|
47
|
|
|
48
|
+ public void askAmount(){
|
|
49
|
+ System.out.println("How many pets do you have?");
|
|
50
|
+ numberOfPets = Integer.parseInt(scanner.nextLine());
|
|
51
|
+
|
|
52
|
+ }
|
|
53
|
+
|
|
54
|
+ public StringBuilder askType(){
|
|
55
|
+ StringBuilder builder = new StringBuilder();
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+ for(int i = 0; i < numberOfPets; i++){
|
|
60
|
+
|
|
61
|
+ System.out.println("What kinds of pets do you have?");
|
|
62
|
+ String typeOfPet = scanner.nextLine();
|
|
63
|
+ builder.append(typeOfPet);
|
|
64
|
+ typeOfPet.trim();
|
|
65
|
+ typeOfPet.toLowerCase();
|
|
66
|
+ switch(typeOfPet){
|
|
67
|
+ case "dog":
|
|
68
|
+ Dog dog = new Dog(nameOfPet);
|
|
69
|
+ //dog.speak();
|
|
70
|
+ pets.add(dog);
|
|
71
|
+ break;
|
44
|
72
|
|
45
|
|
- System.out.println(builder1.toString());
|
46
|
|
- System.out.println(builder2.toString());
|
|
73
|
+ case "cat":
|
|
74
|
+ Cat cat = new Cat(nameOfPet);
|
|
75
|
+ //cat.speak();
|
|
76
|
+ pets.add(cat);
|
|
77
|
+ break;
|
|
78
|
+
|
|
79
|
+ case "pig":
|
|
80
|
+ Pig pig = new Pig(nameOfPet);
|
|
81
|
+ //pig.speak();
|
|
82
|
+ pets.add(pig);
|
|
83
|
+ break;
|
47
|
84
|
|
48
|
|
- String[] petNameArray = builder2.toString().split(", ");
|
49
|
|
- String[] petKindArray = builder1.toString().split(", ");
|
50
|
85
|
|
51
|
|
- for (int k = 0; k < numberOfPets; k++) {
|
52
|
|
- Dog dog = new Dog(petNameArray[k]);
|
53
|
|
- Cat cat = new Cat(petNameArray[k]);
|
54
|
|
- String sounds = "";
|
55
|
86
|
|
56
|
|
- if (petKindArray[k].equals("cat")) {
|
57
|
|
- sounds = cat.speak();
|
58
|
|
- } else if (petKindArray[k].equals("dog")) {
|
59
|
|
- sounds = dog.speak();
|
60
|
|
- } else {
|
61
|
|
- sounds = "White Noise!!!";
|
62
|
|
- }
|
63
|
87
|
}
|
|
88
|
+
|
|
89
|
+ builder.append(" & ");
|
|
90
|
+
|
|
91
|
+ }
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+ return builder;
|
|
96
|
+ }
|
|
97
|
+
|
|
98
|
+ public StringBuilder askName(){
|
|
99
|
+ StringBuilder builder = new StringBuilder();
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+ for(int i = 0; i < numberOfPets; i++){
|
|
103
|
+ System.out.println("Whats the name of your pet?");
|
|
104
|
+ String nameOfPet = scanner.next();
|
|
105
|
+ builder.append(nameOfPet);
|
|
106
|
+ builder.append(", ");
|
|
107
|
+
|
64
|
108
|
}
|
|
109
|
+
|
|
110
|
+ return builder;
|
65
|
111
|
}
|
66
|
|
-}
|
67
|
112
|
|
|
113
|
+ public void allPetsSpeak() {
|
|
114
|
+ for (int j = 0; j < pets.size(); j++) {
|
|
115
|
+ pets.get(j).speak();
|
|
116
|
+ }
|
|
117
|
+ }
|
|
118
|
+
|
|
119
|
+ }
|
|
120
|
+
|
|
121
|
+//
|
68
|
122
|
// public class Pet {
|
69
|
123
|
// public void Speak(){
|
70
|
124
|
// //String sound
|