diff --git a/pom.xml b/pom.xml index 62dbb86..3c9cce4 100644 --- a/pom.xml +++ b/pom.xml @@ -5,6 +5,18 @@ io.zipcoder polymorphism-1 0.0.1-SNAPSHOT + + + + org.apache.maven.plugins + maven-compiler-plugin + + 14 + 14 + + + + jar interfaces-1 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..c7fc9b9 --- /dev/null +++ b/src/main/java/io/zipcoder/polymorphism/Cat.java @@ -0,0 +1,11 @@ +package io.zipcoder.polymorphism; + +public class Cat extends Pet{ + public Cat(String name) { + super(name); + } + + public String speak(){ + return "Purrrr"; + } +} 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..5ae611b --- /dev/null +++ b/src/main/java/io/zipcoder/polymorphism/Dog.java @@ -0,0 +1,17 @@ +package io.zipcoder.polymorphism; + +public class Dog extends Pet{ + String breed; + + public Dog(String name) { + super(name); + } + public Dog (String name, String breed){ + super(name); + this.breed = breed; + } + + public String speak(){ + return "Woof woof!"; + } +} diff --git a/src/main/java/io/zipcoder/polymorphism/MainApplication.java b/src/main/java/io/zipcoder/polymorphism/MainApplication.java index 668c627..7ccc0c6 100644 --- a/src/main/java/io/zipcoder/polymorphism/MainApplication.java +++ b/src/main/java/io/zipcoder/polymorphism/MainApplication.java @@ -1,4 +1,101 @@ package io.zipcoder.polymorphism; +import com.sun.tools.javac.Main; + +import java.util.ArrayList; +import java.util.Scanner; + public class MainApplication { + ArrayList petList = new ArrayList<>(); + public static void main(String[] args) { + MainApplication app = new MainApplication(); + Scanner in = new Scanner(System.in); + System.out.println("How many pets do you have?"); + int value = in.nextInt(); + while (value > 0) { + System.out.println("Is it a dog, cat, parrot, or other"); + String type = in.next().toLowerCase(); + switch (type){ + case "dog": + System.out.println("What's your dog's name?"); + String name = in.next(); + app.addDog(name); + System.out.println("Next pet..."); + break; + case "cat": + System.out.println("What's your cat's name?"); + String catName = in.next(); + app.addCat(catName); + System.out.println("Next pet..."); + break; + case "parrot": + System.out.println("What's your parrot's name"); + String birdName = in.next(); + app.addParrot(birdName); + System.out.println("Next pet..."); + break; + default: + System.out.println("Name it"); + String petName = in.next(); + app.addPet(petName); + System.out.println("Next pet..."); + break; + } + value--; + + } + System.out.println(app.printListWithSound()); + } + + public int getValue(String input){ + return Integer.parseInt(input); + } + + public String printList(){ + StringBuilder print = new StringBuilder(); + for (Pet pet : petList) { + print.append(pet.getName()); + print.append("\n"); + } + return print.toString(); + } + + public void addPet(String name){ + Pet pet = new Pet(name); + petList.add(pet); + + } + + public void addDog(String name){ + Pet pet = new Dog(name); + petList.add(pet); + + } + + public void addCat(String name){ + Pet pet = new Cat(name); + petList.add(pet); + + } + + public void addParrot(String name){ + Pet pet = new Parrot(name); + petList.add(pet); + + } + + public int getSize() { + return petList.size(); + } + + public String printListWithSound(){ + StringBuilder print = new StringBuilder(); + for (Pet pet : petList) { + print.append(pet.getName()); + print.append(" goes "); + print.append(pet.speak()); + print.append("\n"); + } + return print.toString(); + } } diff --git a/src/main/java/io/zipcoder/polymorphism/Parrot.java b/src/main/java/io/zipcoder/polymorphism/Parrot.java new file mode 100644 index 0000000..7358a9d --- /dev/null +++ b/src/main/java/io/zipcoder/polymorphism/Parrot.java @@ -0,0 +1,11 @@ +package io.zipcoder.polymorphism; + +public class Parrot extends Pet{ + public Parrot(String name) { + super(name); + } + + public String speak(){ + return "I do what I want"; + } +} 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..162d28d --- /dev/null +++ b/src/main/java/io/zipcoder/polymorphism/Pet.java @@ -0,0 +1,22 @@ +package io.zipcoder.polymorphism; + +public class Pet { + + String name; + public Pet(String name) { + this.name = name; + } + + + public String getName() { + return this.name; + } + + public void setName(String newName) { + this.name = newName; + } + + public String speak(){ + return "screech"; + } +} 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..23e0d72 --- /dev/null +++ b/src/test/java/io/zipcoder/polymorphism/CatTest.java @@ -0,0 +1,35 @@ +package io.zipcoder.polymorphism; + +import org.junit.Assert; +import org.junit.Test; + +public class CatTest { + + @Test + public void catConstructorTest(){ + //given + String name = "Zazzy"; + + //when + Pet cat = new Cat(name); + String actual = cat.getName(); + + //then + Assert.assertEquals(name, actual); + } + + @Test + public void catSpeakTest(){ + //given + String name = "Zazzy"; + String expected = "Purrrr"; + + //when + Pet cat = new Cat(name); + String sound = cat.speak(); + + //then + Assert.assertEquals(expected, sound); + + } +} 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..d4140df --- /dev/null +++ b/src/test/java/io/zipcoder/polymorphism/DogTest.java @@ -0,0 +1,35 @@ +package io.zipcoder.polymorphism; + +import org.junit.Assert; +import org.junit.Test; + +public class DogTest { + + @Test + public void dogConstructorTest(){ + //given + String name = "JoJo"; + + //when + Pet dog = new Dog(name); + String actual = dog.getName(); + + //then + Assert.assertEquals(name, actual); + } + + @Test + public void dogSpeakTest(){ + //given + String name = "JoJo"; + String expected = "Woof woof!"; + + //when + Pet dog = new Dog(name); + String sound = dog.speak(); + + //then + Assert.assertEquals(expected, sound); + + } +} diff --git a/src/test/java/io/zipcoder/polymorphism/MainApplicationTest.java b/src/test/java/io/zipcoder/polymorphism/MainApplicationTest.java index 7daa131..1a6fb3a 100644 --- a/src/test/java/io/zipcoder/polymorphism/MainApplicationTest.java +++ b/src/test/java/io/zipcoder/polymorphism/MainApplicationTest.java @@ -1,8 +1,138 @@ package io.zipcoder.polymorphism; +import org.junit.Assert; import org.junit.Test; +import java.util.ArrayList; +import java.util.Arrays; + public class MainApplicationTest { @Test - public void testMe(){} + public void testMe(){ + //given + String input = "4"; + Integer expected = 4; + + //when + MainApplication app = new MainApplication(); + Integer value = app.getValue(input); + + //then + Assert.assertEquals(expected, value); + + } + + @Test + public void printListTest(){ + //given + Pet pet1 = new Pet("Lola"); + Pet pet2 = new Pet ("Bromley"); + Pet pet3 = new Pet("Sammie"); + ArrayList petList = new ArrayList<>(); + + //when + petList.add(pet1); + petList.add(pet2); + petList.add(pet3); + String pet1Name = petList.get(0).getName(); + String sound = petList.get(0).speak(); + + //then + Assert.assertEquals("Lola", pet1Name); + Assert.assertEquals("screech", sound); + + } + + @Test + public void printTest2(){ + //given + Pet pet1 = new Pet("Lola"); + Pet pet2 = new Pet ("Bromley"); + Pet pet3 = new Pet("Sammie"); + MainApplication app = new MainApplication(); + + //when + app.petList.add(pet1); + app.petList.add(pet2); + app.petList.add(pet3); + + //then + String print = app.printList(); + System.out.println(print); + } + + @Test + public void addPetTest(){ + //given + String name = "Scabbers"; + MainApplication app = new MainApplication(); + int expected = 1; + + //when + app.addPet(name); + int actual = app.getSize(); + + //then + Assert.assertEquals(expected, actual); + } + + @Test + public void printDogTest(){ + //given + String name = "Bronco"; + MainApplication app = new MainApplication(); + String expected = "Woof woof!"; + + //when + app.addDog(name); + String petName = app.petList.get(0).getName(); + String sound = app.petList.get(0).speak(); + + //then + Assert.assertEquals(name, petName); + Assert.assertEquals(expected, sound); + + } + + @Test + public void printDogAndCatTest(){ + //given + String name = "Bronco"; + String catName = "Persia"; + MainApplication app = new MainApplication(); + String expected = "Purrrr"; + + //when + app.addDog(name); + app.addCat(catName); + String petName = app.petList.get(1).getName(); + String sound = app.petList.get(1).speak(); + + //then + Assert.assertEquals(catName, petName); + Assert.assertEquals(expected, sound); + + } + + @Test + public void printParrotTest(){ + //given + String name = "Bronco"; + String catName = "Persia"; + String parrotName = "Charlie"; + MainApplication app = new MainApplication(); + String expected = "I do what I want"; + + //when + app.addDog(name); + app.addCat(catName); + app.addParrot(parrotName); + String petName = app.petList.get(2).getName(); + String sound = app.petList.get(2).speak(); + + //then + Assert.assertEquals(parrotName, petName); + Assert.assertEquals(expected, sound); + + } } diff --git a/src/test/java/io/zipcoder/polymorphism/ParrotTest.java b/src/test/java/io/zipcoder/polymorphism/ParrotTest.java new file mode 100644 index 0000000..22dc384 --- /dev/null +++ b/src/test/java/io/zipcoder/polymorphism/ParrotTest.java @@ -0,0 +1,35 @@ +package io.zipcoder.polymorphism; + +import org.junit.Assert; +import org.junit.Test; + +public class ParrotTest { + + @Test + public void parrotConstructorTest(){ + //given + String name = "Jared"; + + //when + Pet parrot = new Parrot(name); + String actual = parrot.getName(); + + //then + Assert.assertEquals(name, actual); + } + + @Test + public void parrotSpeakTest(){ + //given + String name = "Zazzy"; + String expected = "I do what I want"; + + //when + Pet parrot = new Parrot(name); + String sound = parrot.speak(); + + //then + Assert.assertEquals(expected, sound); + + } +} 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..efefd3a --- /dev/null +++ b/src/test/java/io/zipcoder/polymorphism/PetTest.java @@ -0,0 +1,50 @@ +package io.zipcoder.polymorphism; + +import org.junit.Assert; +import org.junit.Test; + +public class PetTest { + @Test + public void getNameTest(){ + //given + String expectedName = "Ralph"; + Pet pet = new Pet(expectedName); + + //when + String actualName = pet.getName(); + + //then + Assert.assertEquals(expectedName, actualName); + + } + + @Test + public void setNameTest(){ + //given + String initialName = "Ralph"; + String newName = "Biggie"; + + //when + Pet pet = new Pet(initialName); + pet.setName(newName); + + //then + String actualName = pet.getName(); + Assert.assertEquals(newName, actualName); + + } + + @Test + public void speakTest() { + //given + String newName = "Biggie"; + String expected = "screech"; + + //when + Pet pet = new Pet(newName); + + //then + String speak = pet.speak(); + Assert.assertEquals(expected, speak); + } +}