diff --git a/pom.xml b/pom.xml index d73c078..86be1b2 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,10 @@ io.zipcoder Interfaces 1.0-SNAPSHOT - + + 1.8 + 1.8 + junit diff --git a/src/main/java/io/zipcoder/Application.java b/src/main/java/io/zipcoder/Application.java deleted file mode 100644 index 3a257cb..0000000 --- a/src/main/java/io/zipcoder/Application.java +++ /dev/null @@ -1,5 +0,0 @@ -package io.zipcoder; - - -public class Application { -} diff --git a/src/main/java/io/zipcoder/pets/Application.java b/src/main/java/io/zipcoder/pets/Application.java new file mode 100644 index 0000000..adab4bf --- /dev/null +++ b/src/main/java/io/zipcoder/pets/Application.java @@ -0,0 +1,77 @@ +package io.zipcoder.pets; + +import io.zipcoder.pets.Pets; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.Scanner; + +public class Application { + + private ArrayList petList; + + + public Application() { + this.petList = new ArrayList(); + + } + + public void setPetList(ArrayList petList) { + this.petList = petList; + } + + public ArrayList getPetList() { + return petList; + } + + public int askForPets() { + Scanner in = new Scanner(System.in); + System.out.println("Please input the number of pets you own:"); + return in.nextInt(); + } + + public ArrayList createPetList() { + int numberOfPets = askForPets(); + Scanner scan = new Scanner(System.in); + for (int i = 0; i < numberOfPets; i++) { + System.out.println("Please enter type of pet for pet # " + (i + 1)); + String type = scan.nextLine(); + System.out.println("Please enter pet's name"); + String name = scan.nextLine(); + petList.add(PetFactory.createPets(type, name)); + } + setPetList(petList); + return petList; + } + + @Override + public String toString() { + ArrayList petList = getPetList(); + StringBuilder petString = new StringBuilder(); + for (Pets pet: petList) { + petString.append("My name is ") + .append(pet.getName()) + .append(" and I say ") + .append(pet.speak()) + .append(". "); + } + return petString.toString(); + + } + + public ArrayList sortPetsByType() { + Collections.sort(petList, Comparator.comparing(Pets::getType).thenComparing(Pets::getName)); + return petList; + } + + public ArrayList sortPetsByName() { + Collections.sort(petList, Comparator.comparing(Pets::getName).thenComparing(Pets::getType)); + return petList; + } + +} + + + + diff --git a/src/main/java/io/zipcoder/pets/Cat.java b/src/main/java/io/zipcoder/pets/Cat.java new file mode 100644 index 0000000..38fdc28 --- /dev/null +++ b/src/main/java/io/zipcoder/pets/Cat.java @@ -0,0 +1,27 @@ +package io.zipcoder.pets; + +public class Cat extends Pets { + + private String type; + + public Cat(String name) { + super(name); + this.type = "cat"; + } + + + public void setType(String type) { + this.type = "cat"; + } + + @Override + public String getType(){ + return this.type; + } + + + @Override + public String speak(){ + return "Meow"; + } +} diff --git a/src/main/java/io/zipcoder/pets/Dog.java b/src/main/java/io/zipcoder/pets/Dog.java new file mode 100644 index 0000000..6bfaacb --- /dev/null +++ b/src/main/java/io/zipcoder/pets/Dog.java @@ -0,0 +1,28 @@ +package io.zipcoder.pets; + +public class Dog extends Pets { + + private String type; + + + public Dog(String name) { + super(name); + this.type = "dog"; + } + + + public void setType(String type) { + this.type = "dog"; + } + + + public String getType() { + return type; + } + + + @Override + public String speak() { + return "Woof"; + } +} diff --git a/src/main/java/io/zipcoder/pets/Main.java b/src/main/java/io/zipcoder/pets/Main.java new file mode 100644 index 0000000..a1d57cb --- /dev/null +++ b/src/main/java/io/zipcoder/pets/Main.java @@ -0,0 +1,12 @@ +package io.zipcoder.pets; + +import java.util.ArrayList; + +public class Main { + public static void main(String[] args){ + Application app = new Application(); + ArrayList pets = app.createPetList(); + String petString = app.toString(); + System.out.println(petString); + } +} diff --git a/src/main/java/io/zipcoder/pets/Parrot.java b/src/main/java/io/zipcoder/pets/Parrot.java new file mode 100644 index 0000000..610cf32 --- /dev/null +++ b/src/main/java/io/zipcoder/pets/Parrot.java @@ -0,0 +1,33 @@ +package io.zipcoder.pets; + +public class Parrot extends Pets { + + + private String type; + + public Parrot() { + this.type = "parrot"; + } + + public Parrot(String name) { + super(name); + this.type = "parrot"; + } + + + public void setType(String type) { + this.type = "parrot"; + } + + public String getType() { + return type; + } + + + @Override + public String speak() { + return "Hello"; + } + + +} diff --git a/src/main/java/io/zipcoder/pets/PetFactory.java b/src/main/java/io/zipcoder/pets/PetFactory.java new file mode 100644 index 0000000..8ea22f4 --- /dev/null +++ b/src/main/java/io/zipcoder/pets/PetFactory.java @@ -0,0 +1,16 @@ +package io.zipcoder.pets; + +public class PetFactory { + + public static Pets createPets(String pet, String name) { + if(pet.equalsIgnoreCase("dog")) { + return new Dog(name); + } else if (pet.equalsIgnoreCase("cat")) { + return new Cat(name); + } else if (pet.equalsIgnoreCase("parrot")) { + return new Parrot(name); + } + return null; + } + +} diff --git a/src/main/java/io/zipcoder/pets/Pets.java b/src/main/java/io/zipcoder/pets/Pets.java new file mode 100644 index 0000000..3723f40 --- /dev/null +++ b/src/main/java/io/zipcoder/pets/Pets.java @@ -0,0 +1,38 @@ +package io.zipcoder.pets; + +import java.util.Comparator; + +public abstract class Pets{ + + private String name; + private String type; + + public Pets() { + this.name = ""; + } + + public Pets(String name) { + this.name = name; + } + public void setType(String type) { + this.type = type; + } + + public String getType() { + return type; + } + + public void setName(String name) { + this.name = name; + } + + public String getName() { + return this.name; + } + + public abstract String speak(); + + + +} + diff --git a/src/test/java/io/zipcoder/ApplicationTest.java b/src/test/java/io/zipcoder/ApplicationTest.java deleted file mode 100644 index b744df5..0000000 --- a/src/test/java/io/zipcoder/ApplicationTest.java +++ /dev/null @@ -1,5 +0,0 @@ -package io.zipcoder; - - -public class ApplicationTest { -} diff --git a/src/test/java/io/zipcoder/pets/ApplicationTest.java b/src/test/java/io/zipcoder/pets/ApplicationTest.java new file mode 100644 index 0000000..d0fff7a --- /dev/null +++ b/src/test/java/io/zipcoder/pets/ApplicationTest.java @@ -0,0 +1,74 @@ +package io.zipcoder.pets; + +import org.junit.Test; +import org.junit.Assert; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.Comparator; + +public class ApplicationTest { + + @Test + public void testCreatePetList(){ + Application app = new Application(); + Pets dog = PetFactory.createPets("dog", "Sandy"); + String expected = "Sandy"; + ArrayList pets = new ArrayList<>(Arrays.asList(dog)); + app.setPetList(pets); + String actual = app.getPetList().get(0).getName(); + Assert.assertEquals(expected, actual); + } + + @Test + public void testPetsToString(){ + Application app = new Application(); + String expected = "My name is Sandy and I say Woof. "; + Pets dog = PetFactory.createPets("dog", "Sandy"); + ArrayList pets = new ArrayList<>(Arrays.asList(dog)); + app.setPetList(pets); + String actual = app.toString(); + Assert.assertEquals(expected, actual); + } + + + @Test + public void testCompareByName(){ + //Given + Application app = new Application(); + Pets dog = new Dog("Sandy"); + Pets dog1 = new Dog("Molly"); + Pets cat = new Cat("Sandy"); + Pets cat1 = new Cat("Andy"); + Pets parrot = new Parrot("Id"); + ArrayList pets = new ArrayList<>(Arrays.asList(dog, dog1, cat, cat1, parrot)); + ArrayList expected = new ArrayList<>(Arrays.asList(cat1, parrot, dog1, cat, dog)); + app.setPetList(pets); + //When + app.sortPetsByName(); + //Then + ArrayList actual = app.getPetList(); + Assert.assertEquals(expected, actual); + } + @Test + public void testCompareByType() { + //Given + Application app = new Application(); + Pets dog1 = new Dog("Sandy"); + Pets dog2 = new Dog("Molly"); + Pets dog3 = new Dog("Ally"); + Pets cat = new Cat("Sandy"); + Pets cat1 = new Cat("Andy"); + Pets parrot = new Parrot("Id"); + ArrayList pets = new ArrayList<>(Arrays.asList(dog1, dog2, dog3, cat, cat1, parrot)); + ArrayList expected = new ArrayList<>(Arrays.asList(cat1, cat, dog3, dog2, dog1, parrot)); + app.setPetList(pets); + //When + app.sortPetsByType(); + ArrayList actual = app.getPetList(); + //Then + Assert.assertEquals(expected, actual); + } + +} diff --git a/src/test/java/io/zipcoder/pets/CatTest.java b/src/test/java/io/zipcoder/pets/CatTest.java new file mode 100644 index 0000000..5ed235d --- /dev/null +++ b/src/test/java/io/zipcoder/pets/CatTest.java @@ -0,0 +1,32 @@ +package io.zipcoder.pets; + +import org.junit.Assert; +import org.junit.Test; + +public class CatTest { + + @Test + public void testConstructor(){ + Cat cat = new Cat("Sally"); + String expected = "Sally"; + String actual = cat.getName(); + Assert.assertEquals(expected, actual); + } + + @Test + public void testGetType(){ + Cat cat = new Cat("Sally"); + String expected = "cat"; + cat.setType("cat"); + String actual = cat.getType(); + Assert.assertEquals(expected, actual); + } + + @Test + public void testSpeak() { + Cat cat = new Cat("Sally"); + String expected = "Meow"; + String actual = cat.speak(); + Assert.assertEquals(expected, actual); + } +} diff --git a/src/test/java/io/zipcoder/pets/DogTest.java b/src/test/java/io/zipcoder/pets/DogTest.java new file mode 100644 index 0000000..e2630f4 --- /dev/null +++ b/src/test/java/io/zipcoder/pets/DogTest.java @@ -0,0 +1,31 @@ +package io.zipcoder.pets; + +import org.junit.Assert; +import org.junit.Test; + +public class DogTest { + + @Test + public void testConstructor() { + Dog dog = new Dog("Sandy"); + String expected = "Sandy"; + String actual = dog.getName(); + Assert.assertEquals(expected, actual); + } + + @Test + public void testGetType(){ + Dog dog = new Dog("Sandy"); + String expected = "dog"; + String actual = dog.getType(); + Assert.assertEquals(expected, actual); + } + + @Test + public void testSpeak() { + Dog dog = new Dog("Sandy"); + String expected = "Woof"; + String actual = dog.speak(); + Assert.assertEquals(expected, actual); + } +} diff --git a/src/test/java/io/zipcoder/pets/ParrotTest.java b/src/test/java/io/zipcoder/pets/ParrotTest.java new file mode 100644 index 0000000..c83d6fa --- /dev/null +++ b/src/test/java/io/zipcoder/pets/ParrotTest.java @@ -0,0 +1,32 @@ +package io.zipcoder.pets; + +import org.junit.Assert; +import org.junit.Test; + +public class ParrotTest { + + @Test + public void constructorTest() { + Parrot parrot = new Parrot("Id"); + String expected = "Id"; + String actual = parrot.getName(); + Assert.assertEquals(expected, actual); + } + + @Test + public void testGetType(){ + Parrot parrot = new Parrot(); + String expected = "parrot"; + String actual = parrot.getType(); + Assert.assertEquals(expected, actual); + } + @Test + public void speakTest() { + //given + Parrot parrot = new Parrot("Id"); + String expected = "Hello"; + String actual = parrot.speak(); + Assert.assertEquals(expected, actual); + + } +} diff --git a/src/test/java/io/zipcoder/pets/PetFactoryTest.java b/src/test/java/io/zipcoder/pets/PetFactoryTest.java new file mode 100644 index 0000000..609b6a9 --- /dev/null +++ b/src/test/java/io/zipcoder/pets/PetFactoryTest.java @@ -0,0 +1,16 @@ +package io.zipcoder.pets; + +import org.junit.Assert; +import org.junit.Test; + +public class PetFactoryTest { + + @Test + public void testCreatePets() { + String expected = "Sandy"; + Pets dog = PetFactory.createPets("dog", "Sandy"); + String actual = dog.getName(); + Assert.assertEquals(expected,actual); + } + +} diff --git a/src/test/java/io/zipcoder/pets/PetsTest.java b/src/test/java/io/zipcoder/pets/PetsTest.java new file mode 100644 index 0000000..72d9515 --- /dev/null +++ b/src/test/java/io/zipcoder/pets/PetsTest.java @@ -0,0 +1,29 @@ +package io.zipcoder.pets; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Arrays; + + +public class PetsTest { + + @Test + public void testConstructor() { + Pets pet = new Dog("Sandy"); + String expected = "Sandy"; + String actual = pet.getName(); + Assert.assertEquals(expected, actual); + } + + @Test + public void testGetName() { + Pets pet = new Dog("Sudafed"); + String expected = "Sudafed"; + String actual = pet.getName(); + Assert.assertEquals(expected, actual); + } + + +}