|
@@ -1,9 +1,89 @@
|
1
|
1
|
package io.zipcoder.polymorphism;
|
2
|
2
|
|
|
3
|
+import java.util.Scanner;
|
|
4
|
+
|
|
5
|
+
|
3
|
6
|
/**
|
4
|
7
|
* Created by leon on 11/6/17.
|
5
|
8
|
*/
|
6
|
9
|
public class MainApplication {
|
7
|
10
|
|
8
|
|
- // dog extends pet (dog inherits from pet (the superclass)
|
|
11
|
+ public static void main(String[] args) {
|
|
12
|
+
|
|
13
|
+ //Gets user input of how many pets they have
|
|
14
|
+ String prompt1 = "How many pets do you have?";
|
|
15
|
+ int numberOfPets = getUserInputInt(prompt1);
|
|
16
|
+
|
|
17
|
+ //Two new String arrays the length of the number of pets the user has to hold type and names of pets.
|
|
18
|
+ String[] typeOfPet = new String[numberOfPets];
|
|
19
|
+ String[] nameOfPet = new String[numberOfPets];
|
|
20
|
+
|
|
21
|
+ //Runs through the for the amount of pets the user has.
|
|
22
|
+ for (int i = 0; i < numberOfPets; i++) {
|
|
23
|
+
|
|
24
|
+ //Collects and stores the pet type for each pet.
|
|
25
|
+ String prompt2 = ("Pet " + (i + 1) + ": Identify type of animal.");
|
|
26
|
+ typeOfPet[i] = getUserInputString(prompt2).toLowerCase();
|
|
27
|
+
|
|
28
|
+ //Collects and stores the pet name for each pet.
|
|
29
|
+ String prompt3 = ("Pet " + (i + 1) + ": Identify the name of your " + typeOfPet[i]);
|
|
30
|
+ nameOfPet[i] = getUserInputString(prompt3);
|
|
31
|
+
|
|
32
|
+ }
|
|
33
|
+ //prints the name of the pet and what they say.
|
|
34
|
+ printResultsNoises(typeOfPet, nameOfPet);
|
|
35
|
+
|
|
36
|
+ }
|
|
37
|
+
|
|
38
|
+ //Takes a prompt and asks users for input of int type and returns that value.
|
|
39
|
+ public static int getUserInputInt(String prompt) {
|
|
40
|
+ Scanner in = new Scanner(System.in);
|
|
41
|
+ System.out.println(prompt);
|
|
42
|
+ int valueInt = in.nextInt();
|
|
43
|
+ return valueInt;
|
|
44
|
+ }
|
|
45
|
+
|
|
46
|
+ //Takes a prompt and asks users for input of String type and returns that value.
|
|
47
|
+ public static String getUserInputString(String prompt) {
|
|
48
|
+ Scanner in = new Scanner(System.in);
|
|
49
|
+ System.out.println(prompt);
|
|
50
|
+ String valueString = in.nextLine();
|
|
51
|
+ return valueString;
|
|
52
|
+ }
|
|
53
|
+
|
|
54
|
+ //Prints two string arrays with the same index from each array on one line.
|
|
55
|
+ public static void printResults(String[] a, String[] b) {
|
|
56
|
+
|
|
57
|
+ for (int i = 0; i < a.length; i++) {
|
|
58
|
+ System.out.print(a[i] + " ");
|
|
59
|
+ System.out.println(b[i]);
|
|
60
|
+
|
|
61
|
+ }
|
|
62
|
+ }
|
|
63
|
+
|
|
64
|
+ //Prints the values from one array and uses a second array to call the speak method and print the response.
|
|
65
|
+ public static void printResultsNoises(String[] a, String[] b) {
|
|
66
|
+
|
|
67
|
+ for (int i = 0; i < b.length; i++) {
|
|
68
|
+ System.out.print(b[i] + " says: ");
|
|
69
|
+
|
|
70
|
+ if (a[i].equals("dog")) {
|
|
71
|
+ Dog dog = new Dog();
|
|
72
|
+ System.out.println(dog.speak());
|
|
73
|
+ } else if (a[i].equals("cat")) {
|
|
74
|
+ Cat cat = new Cat();
|
|
75
|
+ System.out.println(cat.speak());
|
|
76
|
+ } else if (a[i].equals("fish")) {
|
|
77
|
+ Fish fish = new Fish();
|
|
78
|
+ System.out.println(fish.speak());
|
|
79
|
+ } else {
|
|
80
|
+ //Catch all in case user has a pet that does not have it's own class.
|
|
81
|
+ System.out.println("I don't know what a " + a[i] + " says!");
|
|
82
|
+ }
|
|
83
|
+
|
|
84
|
+ }
|
|
85
|
+ }
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
|
9
|
89
|
}
|