Bladeren bron

three pets added and tests made and passed

Jennifer Chao 6 jaren geleden
bovenliggende
commit
5d6be8c014

+ 9
- 0
pom.xml Bestand weergeven

@@ -8,5 +8,14 @@
8 8
     <artifactId>polymorphism</artifactId>
9 9
     <version>1.0-SNAPSHOT</version>
10 10
 
11
+    <dependencies>
12
+        <!-- https://mvnrepository.com/artifact/junit/junit -->
13
+        <dependency>
14
+            <groupId>junit</groupId>
15
+            <artifactId>junit</artifactId>
16
+            <version>4.12</version>
17
+            <scope>test</scope>
18
+        </dependency>
19
+    </dependencies>
11 20
 
12 21
 </project>

+ 15
- 0
src/main/java/io/zipcoder/polymorphism/Cat.java Bestand weergeven

@@ -0,0 +1,15 @@
1
+package io.zipcoder.polymorphism;
2
+
3
+public class Cat extends Pets {
4
+
5
+    public Cat() {
6
+        this("", "");
7
+    }
8
+
9
+    public Cat(String name, String type) {
10
+        super(name, type);
11
+    }
12
+
13
+    public String speak() { return "\"Myaaaoow.\""; }
14
+
15
+}

+ 55
- 0
src/main/java/io/zipcoder/polymorphism/Console.java Bestand weergeven

@@ -0,0 +1,55 @@
1
+package io.zipcoder.polymorphism;
2
+
3
+import java.util.Arrays;
4
+import java.util.Scanner;
5
+
6
+public class Console {
7
+
8
+    public static int getNumOfPets(String prompt) {
9
+        Scanner scanner = new Scanner(System.in);
10
+        System.out.println(prompt);
11
+        return scanner.nextInt();
12
+    }
13
+
14
+    public static String getUserInput(String prompt) {
15
+        Scanner scanner = new Scanner(System.in);
16
+        System.out.println(prompt);
17
+        return scanner.nextLine();
18
+    }
19
+
20
+    public static String capitalizePetName(String petName) {
21
+        String[] properPetNameArray = petName.split(" ");
22
+        StringBuilder properPetName = new StringBuilder();
23
+
24
+        for (int i = 0; i < properPetNameArray.length; i++) {
25
+            String capitalFirst = properPetNameArray[i].substring(0, 1).toUpperCase() + properPetNameArray[i].substring(1).toLowerCase();
26
+            if (i != properPetNameArray.length - 1) {
27
+                properPetName.append(capitalFirst + " ");
28
+            } else {
29
+                properPetName.append(capitalFirst);
30
+            }
31
+        }
32
+        return properPetName.toString();
33
+    }
34
+}
35
+
36
+
37
+//    public static String[] splitPetNames(String petNames) {
38
+//
39
+//        String[] indivPetNames = petNames.split(", ");
40
+//
41
+//        return indivPetNames;
42
+//    }
43
+//
44
+//    public static String[] splitPetTypes(String petTypes) {
45
+//
46
+//        String[] indivPetTypes = petTypes.split(", ");
47
+//
48
+//        return indivPetTypes;
49
+//    }
50
+
51
+//    public void returnPetInfo (String properPetName, String lowerCaseType) {
52
+//        Snake.setName(properPetName);
53
+//        this.setType(lowerCaseType);
54
+//        System.out.print("Your pet named " + this.getName() + " is a " + this.getType() + ": " + this.speak());
55
+//    }

+ 17
- 0
src/main/java/io/zipcoder/polymorphism/Dog.java Bestand weergeven

@@ -0,0 +1,17 @@
1
+package io.zipcoder.polymorphism;
2
+
3
+public class Dog extends Pets {
4
+
5
+    public Dog() {
6
+        this("", "");
7
+    }
8
+
9
+    public Dog(String name, String type) {
10
+        super(name, type);
11
+    }
12
+
13
+    public String speak() {
14
+        return "\"Bork bork!\"";
15
+    }
16
+
17
+}

+ 57
- 0
src/main/java/io/zipcoder/polymorphism/MainApplication.java Bestand weergeven

@@ -1,7 +1,64 @@
1 1
 package io.zipcoder.polymorphism;
2 2
 
3
+import java.util.Arrays;
4
+
3 5
 /**
4 6
  * Created by leon on 11/6/17.
5 7
  */
8
+
6 9
 public class MainApplication {
10
+
11
+    public static void main(String[] args) {
12
+
13
+        int numOfPets = Console.getNumOfPets("How many pets do you have?");
14
+
15
+        String petName;
16
+        String petType;
17
+        String properPetName;
18
+        String lowerCaseType;
19
+        String[] petNames = new String[numOfPets];
20
+        String[] petTypes = new String[numOfPets];
21
+
22
+        for (int i = 1; i <= numOfPets; i++) {
23
+            if (i == 1 && numOfPets == 1) {
24
+                petName = Console.getUserInput("What is the name of your pet?");
25
+                properPetName = Console.capitalizePetName(petName);
26
+                petNames[i - 1] = properPetName;
27
+            } else if (i == 1) {
28
+                petName = Console.getUserInput("What is the name of your first pet?");
29
+                properPetName = Console.capitalizePetName(petName);
30
+                petNames[i - 1] = properPetName;
31
+            } else {
32
+                petName = Console.getUserInput("What is the name of your next pet?");
33
+                properPetName = Console.capitalizePetName(petName);
34
+                petNames[i - 1] = properPetName;
35
+            }
36
+
37
+            petType = Console.getUserInput("What type of animal is " +  Console.capitalizePetName(petName) + "?");
38
+            lowerCaseType = petType.toLowerCase();
39
+            petTypes[i - 1] = lowerCaseType;
40
+        }
41
+
42
+        for (int i = 0; i < numOfPets; i++) {
43
+            if (petTypes[i].equals("cat")) {
44
+                Cat newCat = new Cat();
45
+                newCat.setName(petNames[i]);
46
+                newCat.setType(petTypes[i]);
47
+                System.out.println("Your pet named " + newCat.getName() + " is a " + newCat.getType() + ": " + newCat.speak());
48
+            } else if (petTypes[i].equals("dog")) {
49
+                Dog newDog = new Dog();
50
+                newDog.setName(petNames[i]);
51
+                newDog.setType(petTypes[i]);
52
+                System.out.println("Your pet named " + newDog.getName() + " is a " + newDog.getType() + ": " + newDog.speak());
53
+            } else if (petTypes[i].equals("snake")) {
54
+                Snake newSnake = new Snake();
55
+                newSnake.setName(petNames[i]);
56
+                newSnake.setType(petTypes[i]);
57
+                System.out.println("Your pet named " + newSnake.getName() + " is a " + newSnake.getType() + ": " + newSnake.speak());
58
+            } else {
59
+                System.out.println("Your pet name " + petNames[i] + " is a " + petTypes[i] + ", but I don't know what sound that animal makes!");
60
+            }
61
+        }
62
+    }
7 63
 }
64
+

+ 38
- 0
src/main/java/io/zipcoder/polymorphism/Pets.java Bestand weergeven

@@ -0,0 +1,38 @@
1
+package io.zipcoder.polymorphism;
2
+
3
+abstract class Pets {
4
+
5
+    // fields
6
+
7
+    private String name;
8
+    private String type;
9
+
10
+    // constructor
11
+
12
+    public Pets(String name, String type) {
13
+        this.name = name;
14
+        this.type = type;
15
+    }
16
+
17
+    // setter and getter methods
18
+
19
+    public String getName() {
20
+        return name;
21
+    }
22
+
23
+    public void setName(String name) {
24
+        this.name = name;
25
+    }
26
+
27
+    public String getType() {
28
+        return type;
29
+    }
30
+
31
+    public void setType(String type) {
32
+        this.type = type;
33
+    }
34
+
35
+    // methods
36
+
37
+    public abstract String speak();
38
+}

+ 17
- 0
src/main/java/io/zipcoder/polymorphism/Snake.java Bestand weergeven

@@ -0,0 +1,17 @@
1
+package io.zipcoder.polymorphism;
2
+
3
+public class Snake extends Pets {
4
+
5
+    public Snake() {
6
+        this("", "");
7
+    }
8
+
9
+    public Snake(String name, String type) {
10
+        super(name, type);
11
+    }
12
+
13
+    public String speak() {
14
+        return "\"Ssssnek...\"";
15
+    }
16
+
17
+}

+ 199
- 0
src/test/java/io/zipcoder/polymorphism/MainApplicationTest.java Bestand weergeven

@@ -1,7 +1,206 @@
1 1
 package io.zipcoder.polymorphism;
2 2
 
3
+import org.junit.Assert;
4
+import org.junit.Test;
5
+
3 6
 /**
4 7
  * Created by leon on 11/6/17.
5 8
  */
9
+
6 10
 public class MainApplicationTest {
11
+
12
+    @Test
13
+    public void testCatSpeak() {
14
+        // Given
15
+        Cat cat = new Cat();
16
+        String expectedOutput = "\"Myaaaoow.\"";
17
+
18
+        //When
19
+        String actualOutput = cat.speak();
20
+
21
+        Assert.assertEquals(expectedOutput, actualOutput);
22
+    }
23
+
24
+    @Test
25
+    public void testDogSpeak() {
26
+        // Given
27
+        Dog dog = new Dog();
28
+        String expectedOutput = "\"Bork bork!\"";
29
+
30
+        //When
31
+        String actualOutput = dog.speak();
32
+
33
+        Assert.assertEquals(expectedOutput, actualOutput);
34
+    }
35
+
36
+    @Test
37
+    public void testSnakeSpeak() {
38
+        // Given
39
+        Snake snake = new Snake();
40
+        String expectedOutput = "\"Ssssnek...\"";
41
+
42
+        //When
43
+        String actualOutput = snake.speak();
44
+
45
+        Assert.assertEquals(expectedOutput, actualOutput);
46
+    }
47
+
48
+    @Test
49
+    public void testCatName() {
50
+        // Given
51
+        Cat cat = new Cat();
52
+        String properPetName = "Fluffy";
53
+        String expectedOutput = "Fluffy";
54
+
55
+        // When
56
+        cat.setName(properPetName);
57
+        String actualOutput = cat.getName();
58
+
59
+        Assert.assertEquals(expectedOutput, actualOutput);
60
+    }
61
+
62
+    @Test
63
+    public void testDogName() {
64
+        // Given
65
+        Dog dog = new Dog();
66
+        String properPetName = "Rover";
67
+        String expectedOutput = "Rover";
68
+
69
+        // When
70
+        dog.setName(properPetName);
71
+        String actualOutput = dog.getName();
72
+
73
+        Assert.assertEquals(expectedOutput, actualOutput);
74
+    }
75
+
76
+    @Test
77
+    public void testSnakeName() {
78
+        // Given
79
+        Snake snake = new Snake();
80
+        String properPetName = "Noodle";
81
+        String expectedOutput = "Noodle";
82
+
83
+        // When
84
+        snake.setName(properPetName);
85
+        String actualOutput = snake.getName();
86
+
87
+        Assert.assertEquals(expectedOutput, actualOutput);
88
+    }
89
+
90
+    @Test
91
+    public void testCatType() {
92
+        // Given
93
+        Cat cat = new Cat();
94
+        String petType = "cat";
95
+        String expectedOutcome = "cat";
96
+
97
+        // When
98
+        cat.setType(petType);
99
+        String actualOutput = cat.getType();
100
+
101
+        Assert.assertEquals(expectedOutcome, actualOutput);
102
+    }
103
+
104
+    @Test
105
+    public void testDogType() {
106
+        // Given
107
+        Dog dog = new Dog();
108
+        String petType = "dog";
109
+        String expectedOutcome = "dog";
110
+
111
+        // When
112
+        dog.setType(petType);
113
+        String actualOutput = dog.getType();
114
+
115
+        Assert.assertEquals(expectedOutcome, actualOutput);
116
+    }
117
+
118
+    @Test
119
+    public void testSnakeType() {
120
+        // Given
121
+        Snake snake = new Snake();
122
+        String petType = "snake";
123
+        String expectedOutcome = "snake";
124
+
125
+        // When
126
+        snake.setType(petType);
127
+        String actualOutput = snake.getType();
128
+
129
+        Assert.assertEquals(expectedOutcome, actualOutput);
130
+    }
131
+
132
+    @Test
133
+    public void testLowerCase() {
134
+        // Given
135
+        String petType = "Cat";
136
+        String expectedOutput = "cat";
137
+
138
+        // When
139
+        String actualOutput = petType.toLowerCase();
140
+
141
+        Assert.assertEquals(expectedOutput, actualOutput);
142
+    }
143
+
144
+    @Test
145
+    public void testLowerCase1() {
146
+        // Given
147
+        String petType = "CAT";
148
+        String expectedOutput = "cat";
149
+
150
+        // When
151
+        String actualOutput = petType.toLowerCase();
152
+
153
+        Assert.assertEquals(expectedOutput, actualOutput);
154
+    }
155
+
156
+    @Test
157
+    public void testCapitalizePetName() {
158
+        // Given
159
+        String petName = "fluffy";
160
+        String expectedOutput = "Fluffy";
161
+
162
+        //When
163
+        String actualOutput = Console.capitalizePetName(petName);
164
+
165
+        Assert.assertEquals(expectedOutput, actualOutput);
166
+    }
167
+
168
+    @Test
169
+    public void testCapitalizePetName1() {
170
+        // Given
171
+        String petName = "flUFfy";
172
+        String expectedOutput = "Fluffy";
173
+
174
+        // When
175
+        String actualOutput = Console.capitalizePetName(petName);
176
+
177
+        Assert.assertEquals(expectedOutput, actualOutput);
178
+    }
179
+
180
+    @Test
181
+    public void testCapitalizePetName2() {
182
+        // Given
183
+        String petName = "lil seBastIAN";
184
+        String expectedOutput = "Lil Sebastian";
185
+
186
+        // When
187
+        String actualOutput = Console.capitalizePetName(petName);
188
+
189
+        Assert.assertEquals(expectedOutput, actualOutput);
190
+    }
191
+
192
+    @Test
193
+    public void testCapitalizePetName3() {
194
+        // Given
195
+        String petName = "jugemu jugEmu goko no surikire kaijARisuigyo no SUIGyomatsu unraimatsu furaimatsu ku neru tokoro ni sumu tokoro yabura koji no bura koji paipo-paipo paipo no shuringan shuringan no gurindai gurindai no ponpokopi no ponpokona no chokyumei no chosuKE";
196
+        String expectedOutput = "Jugemu Jugemu Goko No Surikire Kaijarisuigyo No Suigyomatsu Unraimatsu Furaimatsu Ku Neru Tokoro Ni Sumu Tokoro Yabura Koji No Bura Koji Paipo-paipo Paipo No Shuringan Shuringan No Gurindai Gurindai No Ponpokopi No Ponpokona No Chokyumei No Chosuke";
197
+
198
+        // When
199
+        String actualOutput = Console.capitalizePetName(petName);
200
+
201
+        Assert.assertEquals(expectedOutput, actualOutput);
202
+    }
7 203
 }
204
+
205
+
206
+