#36 Akeem Cherry

Open
akeemcherry wants to merge 1 commits from akeemcherry/CR-MicroLabs-JavaFundamentals-TalkingPets:master into master

+ 8
- 0
pom.xml View File

@@ -7,6 +7,14 @@
7 7
     <groupId>io.zipcoder</groupId>
8 8
     <artifactId>polymorphism</artifactId>
9 9
     <version>1.0-SNAPSHOT</version>
10
+    <dependencies>
11
+        <dependency>
12
+            <groupId>junit</groupId>
13
+            <artifactId>junit</artifactId>
14
+            <version>RELEASE</version>
15
+            <scope>test</scope>
16
+        </dependency>
17
+    </dependencies>
10 18
 
11 19
 
12 20
 </project>

+ 8
- 0
src/main/java/io/zipcoder/polymorphism/Cat.java View File

@@ -0,0 +1,8 @@
1
+package io.zipcoder.polymorphism;
2
+
3
+public class Cat extends Pet {
4
+    @Override
5
+    public String Speak() {
6
+        return "Meow";
7
+    }
8
+}

+ 8
- 0
src/main/java/io/zipcoder/polymorphism/Dog.java View File

@@ -0,0 +1,8 @@
1
+package io.zipcoder.polymorphism;
2
+
3
+public class Dog extends Pet {
4
+    @Override
5
+    public String Speak() {
6
+        return "Woof";
7
+    }
8
+}

+ 75
- 0
src/main/java/io/zipcoder/polymorphism/MainApplication.java View File

@@ -1,7 +1,82 @@
1 1
 package io.zipcoder.polymorphism;
2 2
 
3
+import java.util.Scanner;
4
+
3 5
 /**
4 6
  * Created by leon on 11/6/17.
5 7
  */
6 8
 public class MainApplication {
9
+
10
+    public static void main(String[] args) {
11
+
12
+        String anyPets = "How many pets do you have?";
13
+        int numberOfPets = getNumberOfPets(anyPets);
14
+
15
+        String[] typeOfPet = new String[numberOfPets];
16
+        String[] nameOfPet = new String[numberOfPets];
17
+
18
+        for (int i=0; i < numberOfPets; i++){
19
+            String petType = ("What type of animal is Pet" + (i + 1));
20
+            typeOfPet[i] = getPetType(petType).toLowerCase();
21
+
22
+            String petName = ("What is your " + typeOfPet[i] + "'s name?");
23
+            nameOfPet[i] = getPetName(petName);
24
+        }
25
+
26
+        printResultsNoises(typeOfPet, nameOfPet);
27
+
28
+    }
29
+
30
+    public static int getNumberOfPets(String prompt){
31
+        Scanner userInput = new Scanner(System.in);
32
+        System.out.println(prompt);
33
+        int valueInt = userInput.nextInt();
34
+        return valueInt;
35
+    }
36
+
37
+    public static String getPetType(String prompt){
38
+        Scanner userInput = new Scanner(System.in);
39
+        System.out.println(prompt);
40
+        String valueString = userInput.nextLine();
41
+        return valueString;
42
+
43
+    }
44
+
45
+    public static String getPetName(String prompt){
46
+        Scanner userInput = new Scanner(System.in);
47
+        System.out.println(prompt);
48
+        String valueString = userInput.nextLine();
49
+        return valueString;
50
+
51
+    }
52
+
53
+    public static void printResults(String[] a, String[] b){
54
+        for (int i=0; i < a.length; i++){
55
+            System.out.print(a[i] + " ");
56
+            System.out.println(b[i]);
57
+        }
58
+    }
59
+
60
+    public static void printResultsNoises(String[] a, String[] b){
61
+        for (int i=0; i < b.length; i++){
62
+            System.out.print(b[i] + " says ");
63
+
64
+            if (a[i].equals("dog")){
65
+                Dog dog = new Dog();
66
+                System.out.println(dog.Speak());
67
+            }
68
+            else if (a[i].equals("cat")){
69
+                Cat cat = new Cat();
70
+                System.out.println(cat.Speak());
71
+            }
72
+            else if (a[i].equals("snake")){
73
+                Snake snake = new Snake();
74
+                System.out.println(snake.Speak());
75
+            }
76
+            else {
77
+                System.out.println("something that I dont understand");
78
+            }
79
+        }
80
+    }
81
+
7 82
 }

+ 20
- 0
src/main/java/io/zipcoder/polymorphism/Pet.java View File

@@ -0,0 +1,20 @@
1
+package io.zipcoder.polymorphism;
2
+
3
+public class Pet {
4
+
5
+    public String getName(String petName) {
6
+        return setName(petName);
7
+    }
8
+
9
+    public String setName(String petName) {
10
+
11
+        return petName;
12
+    }
13
+
14
+    private String name;
15
+
16
+    public String Speak(){
17
+        return " ";
18
+    }
19
+
20
+}

+ 8
- 0
src/main/java/io/zipcoder/polymorphism/Snake.java View File

@@ -0,0 +1,8 @@
1
+package io.zipcoder.polymorphism;
2
+
3
+public class Snake extends Pet {
4
+    @Override
5
+    public String Speak() {
6
+        return "Ssssh";
7
+    }
8
+}

+ 69
- 0
src/test/java/io/zipcoder/polymorphism/MainApplicationTest.java View File

@@ -1,7 +1,76 @@
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
  */
6 9
 public class MainApplicationTest {
10
+    @Test
11
+    public void dogSpeakTest(){
12
+        //Given
13
+        Dog dog = new Dog();
14
+        String expectedDogSound = "Woof";
15
+        //When
16
+        String actualDogSound = dog.Speak();
17
+        //Then
18
+        Assert.assertEquals(expectedDogSound, actualDogSound);
19
+    }
20
+
21
+    @Test
22
+    public void catSpeakTest(){
23
+        //Given
24
+        Cat cat = new Cat();
25
+        String expectedCatSound = "Meow";
26
+        //When
27
+        String actualCatSound = cat.Speak();
28
+        //Then
29
+        Assert.assertEquals(expectedCatSound, actualCatSound);
30
+    }
31
+
32
+    @Test
33
+    public void snakeSpeakSound(){
34
+        //Given
35
+        Snake snake = new Snake();
36
+        String expectedSnakeSound = "Ssssh";
37
+        //When
38
+        String actualSnakeSound = snake.Speak();
39
+        //Then
40
+        Assert.assertEquals(expectedSnakeSound, actualSnakeSound);
41
+
42
+    }
43
+
44
+    @Test
45
+    public void getDogName(){
46
+        //Given
47
+        Dog dog = new Dog();
48
+        String expectedDogName = "Doc";
49
+        //When
50
+        String actualDogName = dog.getName(expectedDogName);
51
+        //Then
52
+        Assert.assertEquals(expectedDogName, actualDogName);
53
+    }
54
+
55
+    @Test
56
+    public void getCatName(){
57
+        //Given
58
+        Cat cat = new Cat();
59
+        String expectedCatName = "Mr. Sniffles";
60
+        //When
61
+        String actualCatName = cat.getName(expectedCatName);
62
+        //Then
63
+        Assert.assertEquals(expectedCatName, actualCatName);
64
+    }
65
+
66
+    @Test
67
+    public void getSnakeName(){
68
+        //Given
69
+        Snake snake = new Snake();
70
+        String expectedSnakeName = "Charles";
71
+        //When
72
+        String actualSnakeName = snake.setName(expectedSnakeName);
73
+        //Then
74
+        Assert.assertEquals(expectedSnakeName, actualSnakeName);
75
+    }
7 76
 }