CHU1TA26 6 年 前
コミット
f3b87658c7

+ 8
- 0
pom.xml ファイルの表示

@@ -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>4.12</version>
15
+            <scope>test</scope>
16
+        </dependency>
17
+    </dependencies>
10 18
 
11 19
 
12 20
 </project>

+ 12
- 0
src/main/java/io/zipcoder/polymorphism/Cat.java ファイルの表示

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

+ 17
- 0
src/main/java/io/zipcoder/polymorphism/Dog.java ファイルの表示

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

+ 10
- 0
src/main/java/io/zipcoder/polymorphism/Dolphin.java ファイルの表示

@@ -0,0 +1,10 @@
1
+package io.zipcoder.polymorphism;
2
+
3
+public class Dolphin  extends Pets{
4
+    public Dolphin(String name) {
5
+        super(name);
6
+    }
7
+    public String speak (){
8
+        return "I'm adorable";
9
+    }
10
+}

+ 46
- 0
src/main/java/io/zipcoder/polymorphism/MainApplication.java ファイルの表示

@@ -1,7 +1,53 @@
1 1
 package io.zipcoder.polymorphism;
2 2
 
3
+import java.util.*;
3 4
 /**
4 5
  * Created by leon on 11/6/17.
5 6
  */
6 7
 public class MainApplication {
8
+
9
+
10
+    public static void main (String [] args){
11
+
12
+        Scanner input = new Scanner(System.in);
13
+
14
+        System.out.println("Enter the number of pets you have:  ");
15
+        int numOfPets = input.nextInt();
16
+
17
+        if (numOfPets>0) {
18
+            Pets[] pets = new Pets[numOfPets];
19
+            for(int i=0; i < pets.length; i++) {
20
+                System.out.println("What kind is it?: ");
21
+                System.out.println("Cat, dog or dolphin? ");
22
+                String type = input.next();
23
+
24
+                if(type.equalsIgnoreCase("cat"))
25
+                {
26
+                    System.out.println("What is its name?: ");
27
+                    String name = input.next();
28
+                    Cat cat = new Cat(name);
29
+                    System.out.println(name + "  says " +cat.speak());
30
+
31
+
32
+                } else if(type.equalsIgnoreCase("dog")) {
33
+                    System.out.print("What is its name: ");
34
+                    String name = input.next();
35
+                    Dog dog = new Dog(name);
36
+                    System.out.println(name + "  says " +dog.speak());
37
+
38
+                } else if(type.equalsIgnoreCase("dolphin"))
39
+                {
40
+                    System.out.println("What is its name?: ");
41
+                    String name = input.next();
42
+                    Dolphin dolphin = new Dolphin(name);
43
+                    System.out.println(name + "  says " +dolphin.speak());
44
+
45
+                }else
46
+                    {
47
+                        System.out.println("Wrong answer! ");
48
+                    }
49
+            }
50
+        }
51
+
52
+    }
7 53
 }

+ 29
- 0
src/main/java/io/zipcoder/polymorphism/Pets.java ファイルの表示

@@ -0,0 +1,29 @@
1
+package io.zipcoder.polymorphism;
2
+
3
+abstract public class Pets {
4
+    private String name;
5
+
6
+
7
+
8
+    public Pets (String petName ){
9
+        this.name=petName;
10
+
11
+    }
12
+
13
+
14
+
15
+    public String speak (){
16
+        return " ";
17
+    }
18
+
19
+    public void setName(String name) {
20
+
21
+        this.name=name;
22
+    }
23
+
24
+    public String getName (String name){
25
+
26
+        return name;
27
+    }
28
+
29
+}

+ 35
- 0
src/test/java/io/zipcoder/polymorphism/MainApplicationTest.java ファイルの表示

@@ -1,7 +1,42 @@
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
+
11
+
12
+    @Test
13
+   public void catTest() {
14
+                // given
15
+            Cat cat = new Cat("Lola");
16
+            String expected = "meow";
17
+            String actual=cat.speak();
18
+                //then
19
+            Assert.assertEquals(expected, actual);
20
+           }
21
+
22
+        @Test
23
+        public void dogTest() {
24
+                // given
25
+                Dog dog = new Dog("Zummy");
26
+                String expected = "Woof, Woof";
27
+                String actual=dog.speak();
28
+                //then
29
+                Assert.assertEquals(expected, actual);
30
+        }
31
+
32
+        @Test
33
+        public void dolphinTest() {
34
+                // given
35
+                Dolphin dolphin = new Dolphin ("Dayhani");
36
+                String expected = "I'm adorable";
37
+                String actual=dolphin.speak();
38
+                //then
39
+                Assert.assertEquals(expected, actual);
40
+        }
41
+
7 42
 }