ソースを参照

completed the Talking_Pets lab

thulasi 6 年 前
コミット
9c13cbffc0

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

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

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

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

@@ -0,0 +1,62 @@
1
+package io.zipcoder.polymorphism;
2
+
3
+import java.util.ArrayList;
4
+import java.util.Scanner;
5
+
6
+public class Console {
7
+    Scanner input = new Scanner(System.in);
8
+    ArrayList<Pet> pets = new ArrayList<Pet>();
9
+
10
+
11
+    public void display(){
12
+        System.out.println("Enter the number of pets you have :");
13
+        int number = input.nextInt();
14
+        for(int i=0;i<number;i++){
15
+            System.out.println("Enter the type of pet : ");
16
+            String type = input.next();
17
+            addPets(type);
18
+        }
19
+
20
+        System.out.println();
21
+
22
+        for(Pet p : pets)
23
+            System.out.println(p.getPetName() + " : " + p.speak());
24
+
25
+    }
26
+
27
+    public void addPets(String type){
28
+
29
+        Pet p;
30
+        String name = "";
31
+        //type = type.toLowerCase();
32
+
33
+
34
+        if ("cat".equals(type)) {
35
+            System.out.println("Enter your cat name : ");
36
+            name = input.next();
37
+            p = new Cat(name);
38
+            pets.add(p);
39
+
40
+        } else if ("dog".equals(type)) {
41
+            System.out.println("Enter your dog name : ");
42
+            name = input.next();
43
+            p = new Dog(name);
44
+            pets.add(p);
45
+
46
+        } else if ("duck".equals(type)) {
47
+            System.out.println("Enter your duck name : ");
48
+            name = input.next();
49
+            p = new Duck(name);
50
+            pets.add(p);
51
+
52
+        } else {
53
+            System.out.println("Given type did not match");
54
+        }
55
+
56
+    }
57
+
58
+
59
+
60
+
61
+
62
+}

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

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

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

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

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

@@ -1,7 +1,16 @@
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
+    public static void main(String args[]){
10
+
11
+        Console console = new Console();
12
+
13
+        console.display();
14
+
15
+    }
7 16
 }

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

@@ -0,0 +1,23 @@
1
+package io.zipcoder.polymorphism;
2
+
3
+public class Pet {
4
+    protected String petName;
5
+    //protected String type;
6
+
7
+
8
+    public Pet(String petName) {
9
+        this.petName = petName;
10
+    }
11
+
12
+
13
+
14
+    public String getPetName() {
15
+        return petName;
16
+    }
17
+
18
+
19
+
20
+    public String speak(){
21
+        return "pet speaking";
22
+    }
23
+}

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

@@ -0,0 +1,23 @@
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 nameTest(){
10
+        Pet cat = new Cat("kitty");
11
+        String expected = "kitty";
12
+        String actual = cat.getPetName();
13
+    }
14
+
15
+    @Test
16
+    public void speakTest(){
17
+
18
+        Pet cat = new Cat("kitty");
19
+        String expected = "Meow Meow";
20
+        String actual = cat.speak();
21
+        Assert.assertEquals(expected, actual);
22
+    }
23
+}

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

@@ -0,0 +1,25 @@
1
+package io.zipcoder.polymorphism;
2
+
3
+import org.junit.Assert;
4
+import org.junit.Test;
5
+
6
+import java.util.ArrayList;
7
+
8
+public class ConsoleTest {
9
+
10
+   /* @Test
11
+    public void testDisplay(){
12
+
13
+    }*/
14
+
15
+   @Test
16
+    public void testAddPets(){
17
+       ArrayList<Pet> pets = new ArrayList<Pet>();
18
+       Pet p = new Cat("tommy");
19
+       pets.add(p);
20
+       Assert.assertEquals(1, pets.size());
21
+
22
+   }
23
+
24
+
25
+}

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

@@ -0,0 +1,24 @@
1
+package io.zipcoder.polymorphism;
2
+
3
+import org.junit.Assert;
4
+import org.junit.Test;
5
+
6
+public class DogTest {
7
+
8
+
9
+    @Test
10
+    public void nameTest(){
11
+        Pet dog = new Dog("scooby");
12
+        String expected = "scooby";
13
+        String actual = dog.getPetName();
14
+    }
15
+
16
+    @Test
17
+    public void speakTest(){
18
+
19
+        Dog dog = new Dog("scooby");
20
+        String expected = "Woof Woof";
21
+        String actual = dog.speak();
22
+        Assert.assertEquals(expected, actual);
23
+    }
24
+}

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

@@ -0,0 +1,24 @@
1
+package io.zipcoder.polymorphism;
2
+
3
+import org.junit.Assert;
4
+import org.junit.Test;
5
+
6
+public class DuckTest {
7
+
8
+
9
+    @Test
10
+    public void nameTest(){
11
+        Pet duck = new Duck("white_duck");
12
+        String expected = "white_duck";
13
+        String actual = duck.getPetName();
14
+    }
15
+
16
+    @Test
17
+    public void speakTest(){
18
+
19
+        Duck duck = new Duck("white_duck");
20
+        String expected = "Quack Quack";
21
+        String actual = duck.speak();
22
+        Assert.assertEquals(expected, actual);
23
+    }
24
+}

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

@@ -1,7 +1,12 @@
1 1
 package io.zipcoder.polymorphism;
2 2
 
3
+import org.omg.CORBA.MARSHAL;
4
+
3 5
 /**
4 6
  * Created by leon on 11/6/17.
5 7
  */
6 8
 public class MainApplicationTest {
9
+
10
+
11
+
7 12
 }

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

@@ -0,0 +1,31 @@
1
+package io.zipcoder.polymorphism;
2
+
3
+
4
+import org.junit.Assert;
5
+import org.junit.Test;
6
+
7
+public class PetTest {
8
+
9
+
10
+
11
+
12
+    @Test
13
+    public void testGetName(){
14
+        Pet pet = new Pet("tommy");
15
+        String expected = "tommy";
16
+        String actual = pet.getPetName();
17
+        Assert.assertEquals(expected, actual);
18
+    }
19
+
20
+    @Test
21
+    public void speakTest(){
22
+        Pet pet = new Pet("petname");
23
+        String expected = "pet speaking";
24
+        String actual = pet.speak();
25
+        Assert.assertEquals(expected, actual);
26
+    }
27
+
28
+
29
+
30
+
31
+}