diff --git a/pom.xml b/pom.xml index 93036ba..6236042 100644 --- a/pom.xml +++ b/pom.xml @@ -8,5 +8,13 @@ polymorphism 1.0-SNAPSHOT + + + junit + junit + 4.12 + + + \ No newline at end of file diff --git a/src/main/java/io/zipcoder/polymorphism/App.java b/src/main/java/io/zipcoder/polymorphism/App.java new file mode 100644 index 0000000..37a016a --- /dev/null +++ b/src/main/java/io/zipcoder/polymorphism/App.java @@ -0,0 +1,53 @@ +package io.zipcoder.polymorphism; + +import io.zipcoder.polymorphism.pets.Cat; +import io.zipcoder.polymorphism.pets.Dog; +import io.zipcoder.polymorphism.pets.Horse; +import io.zipcoder.polymorphism.pets.Pet; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class App { + + List petList = new ArrayList(); + Console menu = new Console(System.in, System.out); + + public void runApp() { + + int numberOfPets; + + numberOfPets = this.menu.getIntegerInput("How many pets do you have?"); + + for (int i = 0; i < numberOfPets; i++) { + String petType = ""; + String petName = ""; + petType = this.menu.getStringInput("What type is pet: " + (i+1) + "?"); + addPet(petType); + petName = this.menu.getStringInput("What is the pet name?"); + this.petList.get(i).setName(petName); + } + + printPets(); + + + } + + public void addPet(String petType){ + petType = petType.toLowerCase(); + if (petType.equals("cat")) this.petList.add(new Cat()); + if (petType.equals("dog")) this.petList.add(new Dog()); + if (petType.equals("horse")) this.petList.add(new Horse()); + } + + public void printPets(){ + int i = 0; + for (Pet eachPet : this.petList){ + String petInfo = ""; + petInfo = petInfo.concat(this.petList.get(i).getPetType().concat(": " + this.petList.get(i).getName())); + this.menu.println(petInfo); + i++; + } + } +} 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..4fc12e7 --- /dev/null +++ b/src/main/java/io/zipcoder/polymorphism/Console.java @@ -0,0 +1,62 @@ +package io.zipcoder.polymorphism; + +import java.util.Scanner; +import java.io.PrintStream; +import java.io.InputStream; + +public final class Console { + + private final Scanner userInput; + private final PrintStream output; + + +//----------- constructor -------------------------- + public Console (InputStream in, PrintStream out) { + this.userInput = new Scanner(in); + this.output = out; + } + +//----------- format the prompt and display it ----------- +public void println (String val, Object... args){ + print(val + "\n",args); +} + + public void print (String val, Object... args){ + this.output.format(val,args); + } + +//----------- prompt the user for input and get the string value of the answer ---------- + public String getStringInput (String prompt, Object... args){ + println(prompt,args); + return this.userInput.nextLine(); + } + +//----------- prompt the user for input and get the numeric value of the answer ---------- + + public Integer getIntegerInput (String prompt, Object... args){ + String tempInput = getStringInput(prompt,args); + try{ + Integer integerInput = Integer.parseInt(tempInput); + return integerInput; + } catch (NumberFormatException nfe){ + println("[ %s ] is not a valid user input!",tempInput); + println("Try inputting an integer value!"); + return getIntegerInput(prompt,args); + } + } + + public Double getDoubleInput(String prompt, Object... args){ + String tempInput = getStringInput(prompt,args); + try { + Double doubleInput = Double.parseDouble(tempInput); + return doubleInput; + } catch (NumberFormatException nfe){ + println("[ %s ] is not a valid user input!",tempInput); + println("Try inputting a number with decimals value!"); + return getDoubleInput(prompt,args); + } + + } + + +} diff --git a/src/main/java/io/zipcoder/polymorphism/MainApplication.java b/src/main/java/io/zipcoder/polymorphism/MainApplication.java index ea9281e..4f456bb 100644 --- a/src/main/java/io/zipcoder/polymorphism/MainApplication.java +++ b/src/main/java/io/zipcoder/polymorphism/MainApplication.java @@ -4,4 +4,8 @@ * Created by leon on 11/6/17. */ public class MainApplication { + public static void main (String [] args){ + App mainApp = new App(); + mainApp.runApp(); + } } diff --git a/src/main/java/io/zipcoder/polymorphism/pets/Cat.java b/src/main/java/io/zipcoder/polymorphism/pets/Cat.java new file mode 100644 index 0000000..3e7bc63 --- /dev/null +++ b/src/main/java/io/zipcoder/polymorphism/pets/Cat.java @@ -0,0 +1,20 @@ +package io.zipcoder.polymorphism.pets; + +public class Cat extends Pet { + + public Cat (String name){ + super.setName(name); + super.setPetType("Cat"); + } + + public Cat(){ + super.setName("No name cat"); + super.setPetType("Cat"); + } + + public String speak(){ + return "meow"; + } + + +} diff --git a/src/main/java/io/zipcoder/polymorphism/pets/Dog.java b/src/main/java/io/zipcoder/polymorphism/pets/Dog.java new file mode 100644 index 0000000..5dd4754 --- /dev/null +++ b/src/main/java/io/zipcoder/polymorphism/pets/Dog.java @@ -0,0 +1,18 @@ +package io.zipcoder.polymorphism.pets; + +public class Dog extends Pet { + + public Dog (String name){ + super.setName(name); + super.setPetType("Dog"); + } + + public Dog (){ + super.setPetType("Dog"); + super.setName("No name dog"); + } + + public String speak(){ + return "Woof"; + } +} diff --git a/src/main/java/io/zipcoder/polymorphism/pets/Horse.java b/src/main/java/io/zipcoder/polymorphism/pets/Horse.java new file mode 100644 index 0000000..8359cc8 --- /dev/null +++ b/src/main/java/io/zipcoder/polymorphism/pets/Horse.java @@ -0,0 +1,19 @@ +package io.zipcoder.polymorphism.pets; + +public class Horse extends Pet{ + + + public Horse(String name){ + super.setName(name); + super.setPetType("Horse"); + } + + public Horse(){ + super.setName("No name horse"); + super.setPetType("Horse"); + } + + public String speak(){ + return "Neigh"; + } +} diff --git a/src/main/java/io/zipcoder/polymorphism/pets/Pet.java b/src/main/java/io/zipcoder/polymorphism/pets/Pet.java new file mode 100644 index 0000000..51d564d --- /dev/null +++ b/src/main/java/io/zipcoder/polymorphism/pets/Pet.java @@ -0,0 +1,32 @@ +package io.zipcoder.polymorphism.pets; + +public abstract class Pet { + + private String name; + + + private String petType; + + public abstract String speak(); + + + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getPetType() { + return petType; + } + + public void setPetType(String petType) { + this.petType = petType; + } + +} + + diff --git a/src/test/java/io/zipcoder/polymorphism/AppTest.java b/src/test/java/io/zipcoder/polymorphism/AppTest.java new file mode 100644 index 0000000..4d441d9 --- /dev/null +++ b/src/test/java/io/zipcoder/polymorphism/AppTest.java @@ -0,0 +1,51 @@ +package io.zipcoder.polymorphism; + +import io.zipcoder.polymorphism.pets.Cat; +import io.zipcoder.polymorphism.pets.Dog; +import io.zipcoder.polymorphism.pets.Horse; +import org.junit.Assert; +import org.junit.Test; + +import static org.junit.Assert.*; + +public class AppTest { + App testApp = new App(); + + @Test + public void addPet() { + + testApp.addPet("Cat"); + testApp.addPet("Dog"); + int expected = 2; + int actual = testApp.petList.size(); + + Assert.assertEquals(expected,actual); + } + + @Test + public void printPets() { + testApp.addPet("Cat"); + testApp.addPet("Dog"); + + String expected = "No name cat"; + String actual = testApp.petList.get(0).getName(); + Assert.assertEquals(expected,actual); + + expected = "No name dog"; + actual = testApp.petList.get(1).getName(); + Assert.assertEquals(expected,actual); + } + + @Test + public void printHorse(){ + testApp.addPet("Horse"); + + String expected = "No name horse"; + String actual = testApp.petList.get(0).getName(); + + Assert.assertEquals(expected,actual); + + } + + +} \ No newline at end of file diff --git a/src/test/java/io/zipcoder/polymorphism/pets/CatTest.java b/src/test/java/io/zipcoder/polymorphism/pets/CatTest.java new file mode 100644 index 0000000..25a7078 --- /dev/null +++ b/src/test/java/io/zipcoder/polymorphism/pets/CatTest.java @@ -0,0 +1,52 @@ +package io.zipcoder.polymorphism.pets; + +import org.junit.Assert; +import org.junit.Test; + +import static org.junit.Assert.*; + +public class CatTest { + + Cat testCat; + + @Test + public void speak() { + + testCat = new Cat(); + + String expected = "meow"; + String actual = testCat.speak(); + + Assert.assertEquals(expected,actual); + } + @Test + public void testConstructorNoName() { + + testCat = new Cat(); + + String expected = "No name cat"; + String actual = testCat.getName(); + + Assert.assertEquals(expected,actual); + } + @Test + public void testConstructorWithName() { + + String expected = "Jiffy"; + testCat = new Cat(expected); + String actual = testCat.getName(); + + Assert.assertEquals(expected,actual); + } + + @Test + public void testGetType() { + + testCat = new Cat("Jiffy"); + + String expected = "Cat"; + String actual = testCat.getPetType(); + + Assert.assertEquals(expected, actual); + } +} \ No newline at end of file diff --git a/src/test/java/io/zipcoder/polymorphism/pets/DogTest.java b/src/test/java/io/zipcoder/polymorphism/pets/DogTest.java new file mode 100644 index 0000000..b1f30e5 --- /dev/null +++ b/src/test/java/io/zipcoder/polymorphism/pets/DogTest.java @@ -0,0 +1,48 @@ +package io.zipcoder.polymorphism.pets; + +import org.junit.Assert; +import org.junit.Test; + +import static org.junit.Assert.*; + +public class DogTest { + + @Test + public void speak() { + Dog testDog = new Dog(); + String expected = "Woof"; + String actual = testDog.speak(); + + Assert.assertEquals(expected,actual); + } + + @Test + public void testConstructorNoName() { + Dog testDog = new Dog(); + String expected = "No name dog"; + String actual = testDog.getName(); + + Assert.assertEquals(expected,actual); + } + + @Test + public void testConstructorWithName() { + String expected = "Scully"; + Dog testDog = new Dog(expected); + String actual = testDog.getName(); + + Assert.assertEquals(expected,actual); + } + + @Test + public void testGetType() { + + Dog testDog = new Dog("Scully"); + + String expected = "Dog"; + String actual = testDog.getPetType(); + + Assert.assertEquals(expected, actual); + } + +} \ No newline at end of file diff --git a/src/test/java/io/zipcoder/polymorphism/pets/HorseTest.java b/src/test/java/io/zipcoder/polymorphism/pets/HorseTest.java new file mode 100644 index 0000000..38a696f --- /dev/null +++ b/src/test/java/io/zipcoder/polymorphism/pets/HorseTest.java @@ -0,0 +1,55 @@ +package io.zipcoder.polymorphism.pets; + +import org.junit.Assert; +import org.junit.Test; + +import javax.print.DocFlavor; + +import static org.junit.Assert.*; + +public class HorseTest { + + @Test + public void speak() { + + Horse testHorse = new Horse(); + + String expected = "Neigh"; + String actual = testHorse.speak(); + + Assert.assertEquals(expected,actual); + } + + @Test + public void testConstructorNoName() { + + Horse testHorse = new Horse(); + + String expected = "No name horse"; + String actual = testHorse.getName(); + + Assert.assertEquals(expected,actual); + } + + @Test + public void testConstructorWithName() { + + String expected = "Billy"; + Horse testHorse = new Horse(expected); + String actual = testHorse.getName(); + + Assert.assertEquals(expected,actual); + } + + + @Test + public void testGetType() { + + Horse testHorse = new Horse("Billy"); + + String expected = "Horse"; + String actual = testHorse.getPetType(); + + Assert.assertEquals(expected, actual); + } +} \ No newline at end of file