浏览代码

Completed part 3

Lauren Green 6 年前
父节点
当前提交
a5d2cefdd1

+ 1
- 1
src/main/java/io/zipcoder/polymorphism/Fish.java 查看文件

@@ -4,6 +4,6 @@ public class Fish extends Pet {
4 4
 
5 5
     public String speak() {
6 6
 
7
-        return "I'm a fish, I don't speak";
7
+        return "I'm a fish, I don't speak.";
8 8
     }
9 9
 }

+ 81
- 1
src/main/java/io/zipcoder/polymorphism/MainApplication.java 查看文件

@@ -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
 }

+ 1
- 1
src/test/java/io/zipcoder/polymorphism/MainApplicationTest.java 查看文件

@@ -47,7 +47,7 @@ public class MainApplicationTest {
47 47
 
48 48
         Fish fish = new Fish();
49 49
         // Given
50
-        String expectedSpeak = "I'm a fish, I don't speak";
50
+        String expectedSpeak = "I'm a fish, I don't speak.";
51 51
 
52 52
         // When
53 53
         String actualSpeak = fish.speak();