ThuyKhong преди 6 години
родител
ревизия
650bdd40a9

+ 25
- 1
pom.xml Целия файл

@@ -8,5 +8,29 @@
8 8
     <artifactId>polymorphism</artifactId>
9 9
     <version>1.0-SNAPSHOT</version>
10 10
 
11
-
11
+    <properties>
12
+        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
13
+        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
14
+        <java.version>1.8</java.version>
15
+    </properties>
16
+    <build>
17
+        <plugins>
18
+            <plugin>
19
+                <groupId>org.apache.maven.plugins</groupId>
20
+                <artifactId>maven-compiler-plugin</artifactId>
21
+                <configuration>
22
+                    <source>1.8</source>
23
+                    <target>1.8</target>
24
+                </configuration>
25
+            </plugin>
26
+        </plugins>
27
+    </build>
28
+    <dependencies>
29
+        <dependency>
30
+            <groupId>junit</groupId>
31
+            <artifactId>junit</artifactId>
32
+            <version>4.12</version>
33
+            <scope>test</scope>
34
+        </dependency>
35
+    </dependencies>
12 36
 </project>

+ 14
- 0
src/main/java/io/zipcoder/polymorphism/Bird.java Целия файл

@@ -0,0 +1,14 @@
1
+package io.zipcoder.polymorphism;
2
+
3
+public class Bird extends Pet{
4
+
5
+    public Bird(String name){
6
+        super("Bird", name);
7
+    }
8
+
9
+
10
+
11
+    public String speak() {
12
+        return ("I tawt I taw a puddy tat!");
13
+    }
14
+}

+ 11
- 0
src/main/java/io/zipcoder/polymorphism/Cat.java Целия файл

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

+ 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 Pet {
4
+
5
+
6
+    public Dog(String name) {
7
+        super("Dog", name);
8
+    }
9
+
10
+    public Dog() {
11
+
12
+    }
13
+
14
+    public String speak() {
15
+        return ("Woof woof!");
16
+    }
17
+}

+ 71
- 0
src/main/java/io/zipcoder/polymorphism/MainApplication.java Целия файл

@@ -1,7 +1,78 @@
1 1
 package io.zipcoder.polymorphism;
2 2
 
3
+import java.util.ArrayList;
4
+import java.util.List;
5
+import java.util.Scanner;
6
+
3 7
 /**
4 8
  * Created by leon on 11/6/17.
5 9
  */
6 10
 public class MainApplication {
11
+    protected static Scanner scan = new Scanner(System.in);
12
+
13
+    public static void main(String[] args) {
14
+
15
+        int numberOfPet = getNumberOfPets();
16
+        List<Pet> pets = createPets(numberOfPet);
17
+        String pet = buildPetInfo(pets);
18
+
19
+        System.out.println(pet);
20
+
21
+    }
22
+
23
+    private static String buildPetInfo(List<Pet> pets) {
24
+        StringBuilder builder = new StringBuilder();
25
+        for(int i = 0; i < pets.size(); i++) {
26
+            Pet pet = pets.get(i);
27
+            builder.append("Your " + pet.getPetType() + " , " + pet.getPetName() + " , say " + pet.speak());
28
+            builder.append("\n");
29
+        }
30
+
31
+        return builder.toString();
32
+    }
33
+
34
+    public static int getNumberOfPets() {
35
+
36
+        System.out.println("How many pets do you have?");
37
+        int numberOfPets = scan.nextInt();
38
+        scan.nextLine();
39
+        return numberOfPets;
40
+    }
41
+
42
+
43
+    public static List<Pet> createPets(int numberOfPet) {
44
+        ArrayList<Pet> listOfPet = new ArrayList<Pet>();
45
+
46
+        StringBuilder builder = new StringBuilder();
47
+        for (int i = 0; i < numberOfPet; i++) {
48
+            System.out.println("What type of animal(s) do you have?");
49
+            String petType = scan.nextLine();
50
+
51
+            System.out.println("What is his/her name?");
52
+            String petName = scan.nextLine();
53
+
54
+            listOfPet.add(createPet(petType, petName));
55
+        }
56
+
57
+        return listOfPet;
58
+
59
+    }
60
+
61
+    public static Pet createPet(String petType, String petName){
62
+        Pet pet = null;
63
+        if (petType.equals("dog")) {
64
+            pet = new Dog(petName);
65
+        }
66
+        else if (petType.equals("cat")) {
67
+            pet = new Cat(petName);
68
+        }
69
+        else if (petType.equals("bird")) {
70
+            pet = new Bird(petName);
71
+        }
72
+
73
+        return pet;
74
+    }
75
+
76
+
7 77
 }
78
+

+ 44
- 0
src/main/java/io/zipcoder/polymorphism/Pet.java Целия файл

@@ -0,0 +1,44 @@
1
+package io.zipcoder.polymorphism;
2
+
3
+import java.util.ArrayList;
4
+import java.util.Scanner;
5
+
6
+public class Pet {
7
+    private String petName;
8
+    private String petType;
9
+
10
+
11
+
12
+    public Pet(){
13
+        petName = "";
14
+        petType = "";
15
+    }
16
+
17
+    public Pet(String petType, String petName){
18
+        this.petName = petName;
19
+        this.petType = petType;
20
+    }
21
+
22
+    public String setPetName(String name){
23
+        return this.petName = name;
24
+    }
25
+
26
+    public String getPetName() {
27
+        return petName;
28
+    }
29
+
30
+    public String getPetType() {
31
+        return petType;
32
+    }
33
+
34
+    public String setPetType (String type){
35
+        return this.petType = type;
36
+    }
37
+    public String speak(){
38
+        return("i say..");
39
+    }
40
+
41
+
42
+
43
+}
44
+

+ 19
- 0
src/test/java/io/zipcoder/polymorphism/BirdTest.java Целия файл

@@ -0,0 +1,19 @@
1
+package io.zipcoder.polymorphism;
2
+
3
+import org.junit.Assert;
4
+import org.junit.Test;
5
+
6
+public class BirdTest {
7
+
8
+    @Test
9
+    public void testBirdSpeak() {
10
+
11
+        Bird tweety = new Bird("tweety");
12
+
13
+        String say = "I tawt I taw a puddy tat!";
14
+
15
+        String actual = tweety.speak();
16
+        Assert.assertEquals(say, actual);
17
+    }
18
+
19
+}

+ 18
- 0
src/test/java/io/zipcoder/polymorphism/CatTest.java Целия файл

@@ -0,0 +1,18 @@
1
+package io.zipcoder.polymorphism;
2
+
3
+import org.junit.Assert;
4
+import org.junit.Test;
5
+
6
+public class CatTest {
7
+
8
+    @Test
9
+    public void testCatSpeak() {
10
+
11
+        Cat kat = new Cat("Kat");
12
+
13
+        String say = "Meow meow";
14
+
15
+        String actual = kat.speak();
16
+        Assert.assertEquals(say, actual);
17
+    }
18
+}

+ 18
- 0
src/test/java/io/zipcoder/polymorphism/DogTest.java Целия файл

@@ -0,0 +1,18 @@
1
+package io.zipcoder.polymorphism;
2
+
3
+import org.junit.Assert;
4
+import org.junit.Test;
5
+
6
+public class DogTest {
7
+
8
+    @Test
9
+    public void testDogSpeak() {
10
+
11
+        Dog butters = new Dog();
12
+
13
+        String say = "Woof woof!";
14
+
15
+        String actual = butters.speak();
16
+        Assert.assertEquals(say, actual);
17
+    }
18
+}

+ 7
- 1
src/test/java/io/zipcoder/polymorphism/MainApplicationTest.java Целия файл

@@ -1,7 +1,13 @@
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
-public class MainApplicationTest {
9
+
10
+public class MainApplicationTest{
11
+
12
+
7 13
 }

+ 34
- 0
src/test/java/io/zipcoder/polymorphism/PetTest.java Целия файл

@@ -0,0 +1,34 @@
1
+package io.zipcoder.polymorphism;
2
+
3
+import org.junit.Assert;
4
+import org.junit.Test;
5
+
6
+public class PetTest {
7
+
8
+
9
+    @Test
10
+    public void testSetGetName(){
11
+
12
+        Pet dog = new Dog();
13
+
14
+        String name = "Butters";
15
+
16
+        dog.setPetName(name);
17
+        String actual = dog.getPetName();
18
+
19
+        Assert.assertEquals(name, actual);
20
+    }
21
+
22
+    @Test
23
+    public void testSetPetType(){
24
+
25
+        Pet dog = new Dog();
26
+
27
+        String type = "Dog";
28
+
29
+        String actual = dog.setPetType(type);
30
+
31
+        Assert.assertEquals(type, actual);
32
+
33
+    }
34
+}