diff --git a/pom.xml b/pom.xml index 93036ba..23875fb 100644 --- a/pom.xml +++ b/pom.xml @@ -7,6 +7,32 @@ io.zipcoder polymorphism 1.0-SNAPSHOT + + + junit + junit + 4.12 + test + + + junit + junit + 4.12 + test + + + junit + junit + 4.12 + test + + + junit + junit + 4.12 + test + + \ No newline at end of file diff --git a/src/main/java/io/zipcoder/polymorphism/Cat.java b/src/main/java/io/zipcoder/polymorphism/Cat.java new file mode 100644 index 0000000..b7ee9e5 --- /dev/null +++ b/src/main/java/io/zipcoder/polymorphism/Cat.java @@ -0,0 +1,13 @@ +package io.zipcoder.polymorphism; + +public class Cat extends Pets{ + + public Cat(String petName, String petType) { + super(petName, petType); + } + + @Override + public String speak() { + return "Meow"; + } +} diff --git a/src/main/java/io/zipcoder/polymorphism/Dog.java b/src/main/java/io/zipcoder/polymorphism/Dog.java new file mode 100644 index 0000000..a1c021c --- /dev/null +++ b/src/main/java/io/zipcoder/polymorphism/Dog.java @@ -0,0 +1,18 @@ +package io.zipcoder.polymorphism; + +import java.util.logging.Logger; + +public class Dog extends Pets { + + public Dog(String petName) { + } + public Dog(String name, String type){ + super(name, type); + + } + + @Override + public String speak() { + return "Woof"; + } +} diff --git a/src/main/java/io/zipcoder/polymorphism/MainApplication.java b/src/main/java/io/zipcoder/polymorphism/MainApplication.java index ea9281e..e25c883 100644 --- a/src/main/java/io/zipcoder/polymorphism/MainApplication.java +++ b/src/main/java/io/zipcoder/polymorphism/MainApplication.java @@ -1,7 +1,50 @@ package io.zipcoder.polymorphism; +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; +import java.util.logging.Level; +import java.util.logging.Logger; + /** * Created by leon on 11/6/17. */ public class MainApplication { + public static void main(String [] args) { + List pets = new ArrayList(); + + Logger logger = Logger.getLogger(MainApplication.class.getName()); + Scanner scan = new Scanner(System.in); + + logger.info("How many pets ? "); + int numberOfPets = scan.nextInt(); + + + for(int i = 0; i < numberOfPets; i++){ + logger.info("What is the name of pet " + (i +1) + " ?"); + String name = scan.next(); + + logger.info("What is the type of pet " + (i +1) + " ?"); + String type = scan.next(); + String correctedType = type.toLowerCase(); + + if (correctedType.equals("dog")) { + Pets pet = new Dog(name,type); + pets.add(i,pet); + }else if(correctedType.equals("cat")){ + Pets pet = new Cat(name,type); + pets.add(i,pet); + }else if(correctedType.equals("turtle")) { + Pets pet = new Turtle(name, type); + pets.add(i, pet); + }else{ + Pets pet = new Pets(); + pets.add(i,pet); + } + } + for(int i =0 ; i < pets.size(); i++ ){ + logger.info(pets.get(i).speak()); + } + + } } diff --git a/src/main/java/io/zipcoder/polymorphism/Pets.java b/src/main/java/io/zipcoder/polymorphism/Pets.java new file mode 100644 index 0000000..48c39c7 --- /dev/null +++ b/src/main/java/io/zipcoder/polymorphism/Pets.java @@ -0,0 +1,38 @@ +package io.zipcoder.polymorphism; + +import java.util.logging.Logger; + + +public class Pets { + String petName; + String petType; + + public Pets(){} + public Pets(String petName, String petType){ + this.petName = petName; + this.petType = petType; + } + + public String getPetName() { + return petName; + } + + public void setPetName(String petName) { + this.petName = petName; + } + + public String getPetType() { + return petType; + } + + public void setPetType(String petType) { + this.petType = petType; + } + + public String speak(){ return "AnimalNoise"; } + + public String toString(){ + return petName + " " + petType; + } + +} diff --git a/src/main/java/io/zipcoder/polymorphism/Turtle.java b/src/main/java/io/zipcoder/polymorphism/Turtle.java new file mode 100644 index 0000000..e2b35ee --- /dev/null +++ b/src/main/java/io/zipcoder/polymorphism/Turtle.java @@ -0,0 +1,15 @@ +package io.zipcoder.polymorphism; + +import javax.swing.event.AncestorEvent; + +public class Turtle extends Pets { + + public Turtle(String petName, String petType) { + super(petName, petType); + } + + @Override + public String speak() { + return "Meowf"; + } +} diff --git a/src/test/java/io/zipcoder/polymorphism/CatTest.java b/src/test/java/io/zipcoder/polymorphism/CatTest.java new file mode 100644 index 0000000..f47e7b9 --- /dev/null +++ b/src/test/java/io/zipcoder/polymorphism/CatTest.java @@ -0,0 +1,41 @@ +package io.zipcoder.polymorphism; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +public class CatTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void TestCatConstructor() { + String expectedName = "Bilbo"; + String expectedType = "cat"; + + Cat cat = new Cat(expectedName, expectedType); + + String actualName = cat.getPetName(); + String actualType = cat.getPetType(); + + Assert.assertEquals(expectedName, actualName); + Assert.assertEquals(expectedType, actualType); + } + + @Test + public void speak() { + Cat cat = new Cat("Bilbo", "cat"); + String expected = "Meow"; + String actual = cat.speak(); + Assert.assertEquals(expected, actual); + } +} diff --git a/src/test/java/io/zipcoder/polymorphism/DogTest.java b/src/test/java/io/zipcoder/polymorphism/DogTest.java new file mode 100644 index 0000000..9afcac9 --- /dev/null +++ b/src/test/java/io/zipcoder/polymorphism/DogTest.java @@ -0,0 +1,41 @@ +package io.zipcoder.polymorphism; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +public class DogTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void TestDogConstructor(){ + + String expectedName = "Frodo"; + String expectedType = "dog"; + Dog dog = new Dog(expectedName, expectedType); + + String actualName = dog.getPetName(); + String actualType = dog.getPetType(); + + Assert.assertEquals(expectedName, actualName); + Assert.assertEquals(expectedType, actualType); + } + + @Test + public void speak() { + Dog dog = new Dog("Frodo", "dog"); + String expected = "Woof"; + String actual = dog.speak(); + Assert.assertEquals(expected, actual); + } +} \ No newline at end of file diff --git a/src/test/java/io/zipcoder/polymorphism/MainApplicationTest.java b/src/test/java/io/zipcoder/polymorphism/MainApplicationTest.java index 7181623..d796ecb 100644 --- a/src/test/java/io/zipcoder/polymorphism/MainApplicationTest.java +++ b/src/test/java/io/zipcoder/polymorphism/MainApplicationTest.java @@ -1,7 +1,19 @@ package io.zipcoder.polymorphism; +import org.junit.Test; + +import java.util.Scanner; +import java.util.logging.Level; +import java.util.logging.Logger; + +import java.lang.reflect.Array; +import java.util.ArrayList; +import java.util.List; + /** * Created by leon on 11/6/17. */ public class MainApplicationTest { + + } diff --git a/src/test/java/io/zipcoder/polymorphism/PetsTest.java b/src/test/java/io/zipcoder/polymorphism/PetsTest.java new file mode 100644 index 0000000..f78edde --- /dev/null +++ b/src/test/java/io/zipcoder/polymorphism/PetsTest.java @@ -0,0 +1,64 @@ +package io.zipcoder.polymorphism; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; +import java.util.logging.Level; +import java.util.logging.Logger; + +import static org.junit.Assert.*; + +public class PetsTest { + Logger logger = Logger.getLogger(PetsTest.class.getName()); + Scanner scan = new Scanner(System.in); +// @Test +// public void TestPet(){ +// List pets = new ArrayList(); +// logger.info("How many pets ? "); +// int numberOfPets = scan.nextInt(); +// +// +// for(int i = 0; i < numberOfPets; i++){ +// logger.info("What is the name of pet" + i +1 + " ?"); +// String name = scan.nextLine(); +// logger.info("What is the type of pet" + i +1 + " ?"); +// String type = scan.nextLine(); +// Pets pet = new Pets(name,type); +// pets.add(i,pet); +// } +// for(int i =0 ; i < pets.size(); i++ ){ +// logger.info(String.valueOf(pets.get(i))); +// } +// +// } + + + + @Test + public void TestPetConstructor() { + String expectedName = "Bilbo"; + String expectedType = "cat"; + + Pets pet = new Pets(expectedName, expectedType); + + String actualName = pet.getPetName(); + String actualType = pet.getPetType(); + + Assert.assertEquals(expectedName, actualName); + Assert.assertEquals(expectedType, actualType); + } + + @Test + public void speak() { + Pets pet = new Pets("Bilbo", "cat"); + String expected = "AnimalNoise"; + String actual = pet.speak(); + Assert.assertEquals(expected, actual); + } + + + +} \ No newline at end of file diff --git a/src/test/java/io/zipcoder/polymorphism/TurtleTest.java b/src/test/java/io/zipcoder/polymorphism/TurtleTest.java new file mode 100644 index 0000000..5952fb5 --- /dev/null +++ b/src/test/java/io/zipcoder/polymorphism/TurtleTest.java @@ -0,0 +1,42 @@ +package io.zipcoder.polymorphism; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +public class TurtleTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void TestTurtleConstructor(){ + + + + String expectedName = "Gretchen"; + String expectedType = "turtle"; + Turtle turtle = new Turtle(expectedName, expectedType); + String actualName = turtle.getPetName(); + String actualType = turtle.getPetType(); + + Assert.assertEquals(expectedName, actualName); + Assert.assertEquals(expectedType, actualType); + } + + @Test + public void speak() { + Turtle turtle = new Turtle("Gretchen", "turtle"); + String expected = "Meowf"; + String actual = turtle.speak(); + Assert.assertEquals(expected, actual); + } +} \ No newline at end of file