Parcourir la source

Talking Pets FINISHED

Nathan Hall il y a 6 ans
Parent
révision
0dc552c8b6

+ 11
- 0
pom.xml Voir le fichier

@@ -8,5 +8,16 @@
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
+
20
+    </dependencies>
21
+
11 22
 
12 23
 </project>

+ 12
- 0
src/main/java/io/zipcoder/polymorphism/Cat.java Voir le fichier

@@ -0,0 +1,12 @@
1
+package io.zipcoder.polymorphism;
2
+
3
+public class Cat extends Pet {
4
+
5
+    public String petSpeak() {
6
+        return "Meooooow";
7
+    }
8
+
9
+    public Cat(String name) {
10
+        super(name);
11
+    }
12
+}

+ 13
- 0
src/main/java/io/zipcoder/polymorphism/Dog.java Voir le fichier

@@ -0,0 +1,13 @@
1
+package io.zipcoder.polymorphism;
2
+
3
+public class Dog extends Pet {
4
+
5
+    public String petSpeak() {
6
+
7
+        return "Woof";
8
+    }
9
+
10
+    public Dog(String name) {
11
+        super(name);
12
+    }
13
+}

+ 12
- 0
src/main/java/io/zipcoder/polymorphism/Lion.java Voir le fichier

@@ -0,0 +1,12 @@
1
+package io.zipcoder.polymorphism;
2
+
3
+public class Lion extends Pet {
4
+
5
+    public String petSpeak() {
6
+        return "Roooooar";
7
+    }
8
+
9
+    public Lion(String name) {
10
+        super(name);
11
+    }
12
+}

+ 42
- 0
src/main/java/io/zipcoder/polymorphism/MainApplication.java Voir le fichier

@@ -1,7 +1,49 @@
1 1
 package io.zipcoder.polymorphism;
2 2
 
3
+import java.util.*;
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
+
13
+        Scanner scan = new Scanner(System.in);
14
+        System.out.print("How many pets do you have? ");
15
+        int numPets = scan.nextInt();
16
+
17
+        String[] typeOfPet = new String[numPets];
18
+        String[] nameOfPet = new String[numPets];
19
+        Pet[] petList = new Pet[numPets];
20
+
21
+        for (int i = 0; i < numPets; i++) {
22
+            System.out.print("What species is pet #" + (i + 1) + "? ");
23
+            typeOfPet[i] = scan.next();
24
+
25
+            System.out.print("What is your " + typeOfPet[i] + "'s name? ");
26
+            nameOfPet[i] = scan.next();
27
+        }
28
+
29
+
30
+        for (int i = 0; i < numPets; i++) {
31
+
32
+            if (typeOfPet[i].equalsIgnoreCase("Dog")) {
33
+                petList[i] = new Dog(nameOfPet[i]);
34
+
35
+            } else if (typeOfPet[i].equalsIgnoreCase("Cat")) {
36
+                petList[i] = new Cat(nameOfPet[i]);
37
+
38
+            } else if (typeOfPet[i].equalsIgnoreCase("Lion")) {
39
+                petList[i] = new Lion(nameOfPet[i]);
40
+
41
+            }
42
+
43
+            System.out.println("Your " + typeOfPet[i] + " named " + petList[i].getName() + " says " + petList[i].petSpeak());
44
+        }
45
+    }
46
+
7 47
 }
48
+
49
+

+ 30
- 0
src/main/java/io/zipcoder/polymorphism/Pet.java Voir le fichier

@@ -0,0 +1,30 @@
1
+package io.zipcoder.polymorphism;
2
+
3
+import java.util.Scanner;
4
+
5
+public class Pet {
6
+
7
+    private String name;
8
+
9
+    public Pet(String name) {
10
+        this.name = name;
11
+    }
12
+
13
+    public Pet() {
14
+
15
+    }
16
+
17
+    public String getName() {
18
+        return this.name;
19
+    }
20
+
21
+    public String petSpeak() {
22
+        return "I love you";
23
+    }
24
+
25
+
26
+}
27
+
28
+
29
+
30
+

+ 70
- 0
src/test/java/io/zipcoder/polymorphism/MainApplicationTest.java Voir le fichier

@@ -1,7 +1,77 @@
1 1
 package io.zipcoder.polymorphism;
2
+import org.junit.Assert;
3
+import org.junit.Test;
4
+import sun.applet.Main;
2 5
 
3 6
 /**
4 7
  * Created by leon on 11/6/17.
5 8
  */
6 9
 public class MainApplicationTest {
10
+    @Test
11
+    public void dogSpeak(){
12
+        //Given
13
+        Dog shadow = new Dog("");
14
+        String speak = "Woof";
15
+
16
+        //When
17
+        String expected = "Woof";
18
+        String actual = shadow.petSpeak();
19
+        //Then
20
+        Assert.assertEquals(expected, actual);
21
+    }
22
+
23
+    @Test
24
+    public void catSpeak(){
25
+        //Given
26
+        Cat garfield = new Cat("");
27
+        String speak = "Meow";
28
+
29
+        //When
30
+        String expected = "Meooooow";
31
+        String actual = garfield.petSpeak();
32
+        //Then
33
+        Assert.assertEquals(expected, actual);
34
+    }
35
+
36
+    @Test
37
+    public void lionSpeak(){
38
+        //Given
39
+        Lion mufasa = new Lion("");
40
+        String speak = "Roar";
41
+
42
+        //When
43
+        String expected = "Roooooar";
44
+        String actual = mufasa.petSpeak();
45
+        //Then
46
+        Assert.assertEquals(expected, actual);
47
+    }
48
+
49
+
50
+
51
+
52
+    @Test
53
+    public void whatKindOfPet(){
54
+        //Given
55
+        Pet pet = new Pet("");
56
+
57
+        //When
58
+        String expected = "dog";
59
+        String actual = "dog";
60
+        //Then
61
+        Assert.assertEquals(expected, actual);
62
+    }
63
+
64
+    @Test
65
+    public void getNameOfPet(){
66
+        //Given
67
+        Pet pet = new Pet("John");
68
+
69
+        //When
70
+        String expected = "John";
71
+        String actual = pet.getName();
72
+        //Then
73
+        Assert.assertEquals(expected, actual);
74
+    }
75
+
76
+
7 77
 }