Browse Source

Completed TalkingPets

Chaitali Patel 8 years ago
parent
commit
52a1bddd9c

+ 38
- 0
pom.xml View File

@@ -7,6 +7,44 @@
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
+        <dependency>
18
+            <groupId>org.testng</groupId>
19
+            <artifactId>testng</artifactId>
20
+            <version>RELEASE</version>
21
+            <scope>test</scope>
22
+        </dependency>
23
+        <dependency>
24
+            <groupId>org.junit.jupiter</groupId>
25
+            <artifactId>junit -jupiter-api</artifactId>
26
+            <version>RELEASE</version>
27
+            <scope>test</scope>
28
+        </dependency>
29
+        <dependency>
30
+            <groupId>junit</groupId>
31
+            <artifactId>junit</artifactId>
32
+            <version>4.12</version>
33
+            <scope>test</scope>
34
+        </dependency>
35
+        <dependency>
36
+            <groupId>org.jetbrains</groupId>
37
+            <artifactId>annotations-java5</artifactId>
38
+            <version>RELEASE</version>
39
+            <scope>compile</scope>
40
+        </dependency>
41
+        <dependency>
42
+            <groupId>org.jetbrains</groupId>
43
+            <artifactId>annotations-java5</artifactId>
44
+            <version>RELEASE</version>
45
+            <scope>compile</scope>
46
+        </dependency>
47
+    </dependencies>
10 48
 
11 49
 
12 50
 </project>

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

@@ -0,0 +1 @@
1
+package io.zipcoder.polymorphism;


public class Cat extends Pets {
    public Cat(String name) {
        super(name);
    }

    @Override
    public String speak() {
        return this.getName()+ " says: Meoow Meoow";
    }


}


















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

@@ -0,0 +1 @@
1
+package io.zipcoder.polymorphism;

public class Dog extends Pets {

    public Dog(String name) {
        super(name);
    }

   @Override
   public String speak() {
       return this.getName() + " says: Wooff Wooff";
   }
}

+ 1
- 0
src/main/java/io/zipcoder/polymorphism/Elephant.java View File

@@ -0,0 +1 @@
1
+package io.zipcoder.polymorphism;

public class Elephant extends Pets {
    public Elephant(String name) {
        super(name);

    }

    @Override
    public String speak() {
        return this.getName()+ " says: Toorrrooo Toorrrooo";
    }
}

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

@@ -1,7 +1,32 @@
1 1
 package io.zipcoder.polymorphism;
2
+import java.util.ArrayList;
3
+import java.util.List;
4
+import java.util.Scanner;
2 5
 
3 6
 /**
4 7
  * Created by leon on 11/6/17.
5 8
  */
6 9
 public class MainApplication {
10
+
11
+    public static void main(String[] args) {
12
+        display();
13
+    }
14
+    public static void display() {
15
+
16
+        List<Pets> pet = new ArrayList<Pets>();
17
+        System.out.println("How many pets do you have?");
18
+        Scanner keyword = new Scanner(System.in);
19
+        int numPets = keyword.nextInt();
20
+        pet.add(new Dog("Lola"));
21
+        pet.add(new Cat("Luna"));
22
+        pet.add(new Elephant("Lana"));
23
+
24
+        for (Pets p : pet) {
25
+            String str = p.speak();
26
+            System.out.println(str);
27
+        }
28
+
29
+    }
7 30
 }
31
+
32
+

+ 1
- 0
src/main/java/io/zipcoder/polymorphism/Pets.java View File

@@ -0,0 +1 @@
1
+package io.zipcoder.polymorphism;

public abstract class Pets {
    private final String name;


    public Pets(String name){
        this.name = name;
    }


    public String getName() {
        return name;
    }

    public abstract String speak();



}




+ 42
- 4
src/test/java/io/zipcoder/polymorphism/MainApplicationTest.java View File

@@ -1,7 +1,45 @@
1 1
 package io.zipcoder.polymorphism;
2 2
 
3
-/**
4
- * Created by leon on 11/6/17.
5
- */
3
+import org.junit.Assert;
4
+import org.junit.Before;
5
+import org.junit.Test;
6
+
7
+
6 8
 public class MainApplicationTest {
7
-}
9
+    Cat c;
10
+    Dog d;
11
+    Elephant e;
12
+    private String expected;
13
+
14
+    @Before
15
+    public void setup() {
16
+
17
+        this.d= new Dog("Lola");
18
+        this.c = new Cat("Luna");
19
+        this.e = new Elephant("Lana");
20
+    }
21
+
22
+    @Test
23
+    public void testDog() {
24
+        String actual = d.speak();
25
+        System.out.println(actual);
26
+        this.expected = "Lola says: Wooff Wooff";
27
+        Assert.assertEquals(expected, actual);
28
+    }
29
+
30
+    @Test
31
+    public void testCat() {
32
+        String actual = c.speak();
33
+        System.out.println(actual);
34
+        this.expected = "Luna says: Meoow Meoow";
35
+        Assert.assertEquals(expected, actual);
36
+    }
37
+
38
+    @Test
39
+    public void testElephant() {
40
+        String actual = e.speak();
41
+        System.out.println(actual);
42
+        this.expected = "Lana says: Toorrrooo Toorrrooo";
43
+        Assert.assertEquals(expected, actual);
44
+    }
45
+}