diff --git a/pom.xml b/pom.xml
index 93036ba..90c23e3 100644
--- a/pom.xml
+++ b/pom.xml
@@ -7,6 +7,14 @@
io.zipcoder
polymorphism
1.0-SNAPSHOT
+
+
+ junit
+ junit
+ 4.12
+ test
+
+
\ No newline at end of file
diff --git a/src/main/java/io/zipcoder/polymorphism/Bird.java b/src/main/java/io/zipcoder/polymorphism/Bird.java
new file mode 100644
index 0000000..d457cfe
--- /dev/null
+++ b/src/main/java/io/zipcoder/polymorphism/Bird.java
@@ -0,0 +1,12 @@
+package io.zipcoder.polymorphism;
+
+public class Bird extends Pet {
+ public Bird(String name) {
+ super(name, "bird");
+ }
+ public String speak() {
+ String sound = "chirp!";
+// Console.println(sound);
+ return sound;
+ }
+}
\ 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..d18a510
--- /dev/null
+++ b/src/main/java/io/zipcoder/polymorphism/Cat.java
@@ -0,0 +1,12 @@
+package io.zipcoder.polymorphism;
+
+public class Cat extends Pet {
+ public Cat(String name) {
+ super(name, "cat");
+ }
+ public String speak() {
+ String sound = "meow!";
+// Console.println(sound);
+ return sound;
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/io/zipcoder/polymorphism/Console.java b/src/main/java/io/zipcoder/polymorphism/Console.java
new file mode 100644
index 0000000..f2611e2
--- /dev/null
+++ b/src/main/java/io/zipcoder/polymorphism/Console.java
@@ -0,0 +1,34 @@
+package io.zipcoder.polymorphism;
+
+import java.util.Scanner;
+
+public class Console {
+ public static void print(String output, Object... args) {
+ System.out.printf(output, args);
+ }
+
+ public static void println(String output, Object... args) {
+ print(output + "\n",args);
+ }
+
+ public static String getStringInput(String prompt) {
+ Scanner in = new Scanner(System.in);
+ print(prompt);
+ String userInput = in.nextLine();
+ return userInput;
+ }
+
+ public static Integer getIntegerInput(String prompt) {
+ Scanner scanner = new Scanner(System.in);
+ String userInput = "";
+ Boolean check = false;
+ do {
+ print(prompt);
+ userInput = scanner.nextLine();
+ check = userInput.matches("^\\d{0,9}$");
+ if (!check) println("Error: please enter a valid number");
+ } while (!check);
+
+ return Integer.valueOf(userInput);
+ }
+}
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..5a5c6f5
--- /dev/null
+++ b/src/main/java/io/zipcoder/polymorphism/Dog.java
@@ -0,0 +1,12 @@
+package io.zipcoder.polymorphism;
+
+public class Dog extends Pet {
+ public Dog(String name) {
+ super(name, "dog");
+ }
+ public String speak() {
+ String sound = "woof!";
+// Console.println(sound);
+ return sound;
+ }
+}
diff --git a/src/main/java/io/zipcoder/polymorphism/MainApplication.java b/src/main/java/io/zipcoder/polymorphism/MainApplication.java
index ea9281e..0ce7033 100644
--- a/src/main/java/io/zipcoder/polymorphism/MainApplication.java
+++ b/src/main/java/io/zipcoder/polymorphism/MainApplication.java
@@ -4,4 +4,31 @@
* Created by leon on 11/6/17.
*/
public class MainApplication {
+ public static void main(String[] args) {
+ petApp();
+ }
+
+ public static void petApp() {
+ Integer numberOfPets = getNumberOfPets();
+ Console.println("Can you tell me some more about your %s pets?", numberOfPets);
+
+ String[] petTypes = new String[numberOfPets];
+ String[] petNames = new String[numberOfPets];
+ for (int i = 0; i < numberOfPets; i++) {
+ petNames[i] = Console.getStringInput(String.format("What is pet %s's name? ", i+1));
+ petTypes[i] = Console.getStringInput(String.format("What kind of pet is %s? ", petNames[i]));
+ }
+ PetWarehouse petWarehouse = new PetWarehouse(numberOfPets, petTypes, petNames);
+ Console.print(petWarehouse.displayPetInfo());
+ }
+
+ public static Integer getNumberOfPets() {
+ return Console.getIntegerInput("How many pets do you have? ");
+ }
+
+
+
+
+
+
}
diff --git a/src/main/java/io/zipcoder/polymorphism/Pet.java b/src/main/java/io/zipcoder/polymorphism/Pet.java
new file mode 100644
index 0000000..5a1b056
--- /dev/null
+++ b/src/main/java/io/zipcoder/polymorphism/Pet.java
@@ -0,0 +1,27 @@
+package io.zipcoder.polymorphism;
+
+abstract public class Pet {
+ private String name;
+ private String type;
+
+ public Pet(String name, String type) {
+ this.name = name;
+ this.type = type;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+ public String getName() {
+ return this.name;
+ }
+
+ public void setType(String type) {
+ this.type = type;
+ }
+ public String getType() {
+ return this.type;
+ }
+
+ abstract public String speak();
+}
diff --git a/src/main/java/io/zipcoder/polymorphism/PetWarehouse.java b/src/main/java/io/zipcoder/polymorphism/PetWarehouse.java
new file mode 100644
index 0000000..19ea643
--- /dev/null
+++ b/src/main/java/io/zipcoder/polymorphism/PetWarehouse.java
@@ -0,0 +1,58 @@
+package io.zipcoder.polymorphism;
+
+public class PetWarehouse {
+ private Pet[] pets;
+
+ public PetWarehouse() {}
+
+ public PetWarehouse(Pet... pets) {
+ this.pets = pets;
+ }
+ public PetWarehouse(Integer numberOfPets, String[] petTypes, String[] petNames) {
+ this.pets = createPets(numberOfPets, petTypes, petNames);
+ }
+
+ public Pet[] createPets(Integer numberOfPets, String[] petTypes, String[] petNames) {
+ Pet[] pets = new Pet[numberOfPets];
+ for (int i = 0; i < numberOfPets; i++) {
+ pets[i] = createPetFromType(petTypes[i], petNames[i]);
+ }
+ return pets;
+ }
+
+ public Pet createPetFromType(String petType, String petName) {
+ Pet pet;
+ if (petType.equals("dog")) {
+ pet = new Dog(petName);
+ }
+ else if (petType.equals("cat")) {
+ pet = new Cat(petName);
+ }
+ else if (petType.equals("bird")) {
+ pet = new Bird(petName);
+ }
+ else if (petType.equals("snake")) {
+ pet = new Snake(petName);
+ }
+ else {
+ pet = new UnknownPet(petName, petType);
+ }
+ return pet;
+ }
+
+ public Pet[] getPets() {
+ return this.pets;
+ }
+
+ public String displayPetInfo() {
+ StringBuilder output = new StringBuilder();
+ Console.println("\n\n\n");
+ output.append(String.format("You have %s pets.", pets.length));
+
+ for (int i = 0; i < pets.length; i++) {
+ output.append(String.format("\nPet %s is a %s named %s.", i+1, pets[i].getType(), pets[i].getName()));
+ output.append("\n" + pets[i].speak());
+ }
+ return output.toString();
+ }
+}
diff --git a/src/main/java/io/zipcoder/polymorphism/Snake.java b/src/main/java/io/zipcoder/polymorphism/Snake.java
new file mode 100644
index 0000000..eab03c0
--- /dev/null
+++ b/src/main/java/io/zipcoder/polymorphism/Snake.java
@@ -0,0 +1,12 @@
+package io.zipcoder.polymorphism;
+
+public class Snake extends Pet {
+ public Snake(String name) {
+ super(name, "snake");
+ }
+ public String speak() {
+ String sound = "slither!";
+// Console.println(sound);
+ return sound;
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/io/zipcoder/polymorphism/UnknownPet.java b/src/main/java/io/zipcoder/polymorphism/UnknownPet.java
new file mode 100644
index 0000000..982a48d
--- /dev/null
+++ b/src/main/java/io/zipcoder/polymorphism/UnknownPet.java
@@ -0,0 +1,13 @@
+package io.zipcoder.polymorphism;
+
+public class UnknownPet extends Pet {
+ public UnknownPet(String name, String type) {
+ super(name, type);
+ }
+
+ public String speak() {
+ String sound = String.format("(%s noises)!", getType());
+// Console.println(sound);
+ return sound;
+ }
+}
diff --git a/src/test/java/io/zipcoder/polymorphism/BirdTest.java b/src/test/java/io/zipcoder/polymorphism/BirdTest.java
new file mode 100644
index 0000000..82cdff1
--- /dev/null
+++ b/src/test/java/io/zipcoder/polymorphism/BirdTest.java
@@ -0,0 +1,25 @@
+package io.zipcoder.polymorphism;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class BirdTest {
+ @Test
+ public void testSpeak() {
+ Bird bird = new Bird("Tucker");
+ String actual = bird.speak();
+ String expected = "chirp!";
+ Assert.assertEquals(expected, actual);
+ }
+
+ @Test
+ public void testConstructor1() {
+ Bird bird = new Bird("Bort");
+ Assert.assertEquals("Bort", bird.getName());
+ }
+ @Test
+ public void testConstructor2() {
+ Bird bird = new Bird("Tucker");
+ Assert.assertEquals("bird", bird.getType());
+ }
+}
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..9192464
--- /dev/null
+++ b/src/test/java/io/zipcoder/polymorphism/CatTest.java
@@ -0,0 +1,25 @@
+package io.zipcoder.polymorphism;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class CatTest {
+ @Test
+ public void testSpeak() {
+ Cat cat = new Cat("Charlie");
+ String actual = cat.speak();
+ String expected = "meow!";
+ Assert.assertEquals(expected, actual);
+ }
+
+ @Test
+ public void testConstructor1() {
+ Cat cat = new Cat("Charlie");
+ Assert.assertEquals("Charlie", cat.getName());
+ }
+ @Test
+ public void testConstructor2() {
+ Cat cat = new Cat("Charlie");
+ Assert.assertEquals("cat", cat.getType());
+ }
+}
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..29453b2
--- /dev/null
+++ b/src/test/java/io/zipcoder/polymorphism/DogTest.java
@@ -0,0 +1,25 @@
+package io.zipcoder.polymorphism;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class DogTest {
+ @Test
+ public void testSpeak() {
+ Dog dog = new Dog("Tucker");
+ String actual = dog.speak();
+ String expected = "woof!";
+ Assert.assertEquals(expected, actual);
+ }
+
+ @Test
+ public void testConstructor1() {
+ Dog dog = new Dog("Tucker");
+ Assert.assertEquals("Tucker", dog.getName());
+ }
+ @Test
+ public void testConstructor2() {
+ Dog dog = new Dog("Tucker");
+ Assert.assertEquals("dog", dog.getType());
+ }
+}
diff --git a/src/test/java/io/zipcoder/polymorphism/MainApplicationTest.java b/src/test/java/io/zipcoder/polymorphism/MainApplicationTest.java
index 7181623..c4b7095 100644
--- a/src/test/java/io/zipcoder/polymorphism/MainApplicationTest.java
+++ b/src/test/java/io/zipcoder/polymorphism/MainApplicationTest.java
@@ -1,7 +1,22 @@
package io.zipcoder.polymorphism;
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+
/**
* Created by leon on 11/6/17.
*/
public class MainApplicationTest {
+ @Test
+ public void testGetNumberOfPets() {
+ String input = "4";
+ InputStream in = new ByteArrayInputStream(input.getBytes());
+ System.setIn(in);
+ Integer actual = MainApplication.getNumberOfPets();
+ Integer expected = 4;
+ Assert.assertEquals(expected, actual);
+ }
}
diff --git a/src/test/java/io/zipcoder/polymorphism/PetTest.java b/src/test/java/io/zipcoder/polymorphism/PetTest.java
new file mode 100644
index 0000000..1db9eb0
--- /dev/null
+++ b/src/test/java/io/zipcoder/polymorphism/PetTest.java
@@ -0,0 +1,52 @@
+package io.zipcoder.polymorphism;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class PetTest {
+ @Test
+ public void dogConstructorTest() {
+ Dog dog = new Dog("Tucker");
+ String expectedName = "Tucker";
+ String actualName = dog.getName();
+ String expectedType = "dog";
+ String actualType = dog.getType();
+ Assert.assertEquals(expectedName, actualName);
+ Assert.assertEquals(expectedType, actualType);
+ }
+
+ @Test
+ public void catConstructorTest() {
+ Cat cat = new Cat("Tom");
+ String expectedName = "Tom";
+ String actualName = cat.getName();
+ String expectedType = "cat";
+ String actualType = cat.getType();
+ Assert.assertEquals(expectedName, actualName);
+ Assert.assertEquals(expectedType, actualType);
+ }
+
+ @Test
+ public void setNameTest() {
+ Dog dog = new Dog("Tucker");
+ dog.setName("Bill");
+ String expected = "Bill";
+ String actual = dog.getName();
+ Assert.assertEquals(expected, actual);
+ }
+
+ @Test
+ public void speakTest() {
+ Dog dog = new Dog("Tucker");
+ dog.speak();
+
+ }
+ @Test
+ public void testSetType() {
+ Dog dog = new Dog("Tucker");
+ dog.setType("orange");
+ String actual = dog.getType();
+ String expected = "orange";
+ Assert.assertEquals(expected, actual);
+ }
+}
diff --git a/src/test/java/io/zipcoder/polymorphism/PetWarehouseTest.java b/src/test/java/io/zipcoder/polymorphism/PetWarehouseTest.java
new file mode 100644
index 0000000..4cc3a85
--- /dev/null
+++ b/src/test/java/io/zipcoder/polymorphism/PetWarehouseTest.java
@@ -0,0 +1,79 @@
+package io.zipcoder.polymorphism;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class PetWarehouseTest {
+ @Test
+ public void testCreatePetFromType1() {
+ PetWarehouse house = new PetWarehouse();
+ String name = "Tucker";
+ String type = "dog";
+ Pet pet = house.createPetFromType(type, name);
+ Assert.assertTrue(pet instanceof Dog);
+ }
+ @Test
+ public void testCreatePetFromType2() {
+ PetWarehouse house = new PetWarehouse();
+ String name = "Charlie";
+ String type = "cat";
+ Pet pet = house.createPetFromType(type, name);
+ Assert.assertTrue(pet instanceof Cat);
+ }
+ @Test
+ public void testCreatePetFromType3() {
+ PetWarehouse house = new PetWarehouse();
+ String name = "Lamar";
+ String type = "snake";
+ Pet pet = house.createPetFromType(type, name);
+ Assert.assertTrue(pet instanceof Snake);
+ }
+ @Test
+ public void testCreatePetFromType4() {
+ PetWarehouse house = new PetWarehouse();
+ String name = "Annabelle";
+ String type = "spider";
+ Pet pet = house.createPetFromType(type, name);
+ Assert.assertTrue(pet instanceof UnknownPet);
+ }
+
+ @Test
+ public void testCreatePets() {
+ Integer numberOfPets = 4;
+ String[] petTypes = {"dog", "cat", "snake", "spider"};
+ String[] petNames = {"Tucker", "Charlie", "Lamar", "Annabelle"};
+ PetWarehouse house = new PetWarehouse();
+ Pet[] pets = house.createPets(numberOfPets, petTypes, petNames);
+ Assert.assertEquals(4, pets.length);
+ for (int i = 0; i < numberOfPets; i++) {
+ Assert.assertEquals(petNames[i], pets[i].getName());
+ Assert.assertEquals(petTypes[i], pets[i].getType());
+ }
+ }
+
+ @Test
+ public void testConstructor() {
+ Integer numberOfPets = 4;
+ String[] petTypes = {"dog", "cat", "snake", "spider"};
+ String[] petNames = {"Tucker", "Charlie", "Lamar", "Annabelle"};
+ PetWarehouse house = new PetWarehouse(numberOfPets, petTypes, petNames);
+ Pet[] pets = house.getPets();
+ Assert.assertEquals(4, pets.length);
+ for (int i = 0; i < numberOfPets; i++) {
+ Assert.assertEquals(petNames[i], pets[i].getName());
+ Assert.assertEquals(petTypes[i], pets[i].getType());
+ }
+ }
+
+ @Test
+ public void testDisplayPetInfo() {
+ Integer numberOfPets = 4;
+ String[] petTypes = {"dog", "cat", "snake", "spider"};
+ String[] petNames = {"Tucker", "Charlie", "Lamar", "Annabelle"};
+ PetWarehouse house = new PetWarehouse(numberOfPets, petTypes, petNames);
+ String actual = house.displayPetInfo();
+ String expected = "You have 4 pets.\nPet 1 is a dog named Tucker.\nwoof!\nPet 2 is a cat named Charlie.\nmeow!\nPet 3 is a snake named Lamar.\nslither!\nPet 4 is a spider named Annabelle.\n(spider noises)!";
+ Assert.assertEquals(expected, actual);
+ }
+
+}
diff --git a/src/test/java/io/zipcoder/polymorphism/SnakeTest.java b/src/test/java/io/zipcoder/polymorphism/SnakeTest.java
new file mode 100644
index 0000000..2a94bb1
--- /dev/null
+++ b/src/test/java/io/zipcoder/polymorphism/SnakeTest.java
@@ -0,0 +1,25 @@
+package io.zipcoder.polymorphism;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class SnakeTest {
+ @Test
+ public void testSpeak() {
+ Snake snake = new Snake("Lamar");
+ String actual = snake.speak();
+ String expected = "slither!";
+ Assert.assertEquals(expected, actual);
+ }
+
+ @Test
+ public void testConstructor1() {
+ Snake snake = new Snake("Artie");
+ Assert.assertEquals("Artie", snake.getName());
+ }
+ @Test
+ public void testConstructor2() {
+ Snake snake = new Snake("Tucker");
+ Assert.assertEquals("snake", snake.getType());
+ }
+}
diff --git a/src/test/java/io/zipcoder/polymorphism/UnknownPetTest.java b/src/test/java/io/zipcoder/polymorphism/UnknownPetTest.java
new file mode 100644
index 0000000..7edef35
--- /dev/null
+++ b/src/test/java/io/zipcoder/polymorphism/UnknownPetTest.java
@@ -0,0 +1,25 @@
+package io.zipcoder.polymorphism;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class UnknownPetTest {
+ @Test
+ public void testSpeak() {
+ UnknownPet pet = new UnknownPet("Tucker", "blorp");
+ String actual = pet.speak();
+ String expected = "(blorp noises)!";
+ Assert.assertEquals(expected, actual);
+ }
+
+ @Test
+ public void testConstructor1() {
+ UnknownPet pet = new UnknownPet("EEEEEEEE", "blorp");
+ Assert.assertEquals("EEEEEEEE", pet.getName());
+ }
+ @Test
+ public void testConstructor2() {
+ UnknownPet pet = new UnknownPet("Flop", "blorp");
+ Assert.assertEquals("blorp", pet.getType());
+ }
+}