diff --git a/src/main/java/io/zipcoder/Application.java b/src/main/java/io/zipcoder/Application.java index 3a257cb..de1cf5b 100644 --- a/src/main/java/io/zipcoder/Application.java +++ b/src/main/java/io/zipcoder/Application.java @@ -1,5 +1,10 @@ package io.zipcoder; + public class Application { + public static void main(String[] args) { + PetOwner petOwner = new PetOwner(); + petOwner.start(); + } } diff --git a/src/main/java/io/zipcoder/Bird.java b/src/main/java/io/zipcoder/Bird.java new file mode 100644 index 0000000..7fac9ec --- /dev/null +++ b/src/main/java/io/zipcoder/Bird.java @@ -0,0 +1,15 @@ +package io.zipcoder; + +public class Bird extends Pet{ + public Bird(){ + + } + public Bird(String name){ + super(name); + } + + @Override + public String speak(){ + return "Tweet Tweet"; + } +} diff --git a/src/main/java/io/zipcoder/Cat.java b/src/main/java/io/zipcoder/Cat.java new file mode 100644 index 0000000..66285ba --- /dev/null +++ b/src/main/java/io/zipcoder/Cat.java @@ -0,0 +1,16 @@ +package io.zipcoder; + +public class Cat extends Pet { + public Cat() { + + } + + public Cat(String name) { + super(name); + } + + @Override + public String speak() { + return "Meow"; + } +} diff --git a/src/main/java/io/zipcoder/CompareAnimal.java b/src/main/java/io/zipcoder/CompareAnimal.java new file mode 100644 index 0000000..b05ef98 --- /dev/null +++ b/src/main/java/io/zipcoder/CompareAnimal.java @@ -0,0 +1,12 @@ +package io.zipcoder; + +import java.util.Comparator; + +public class CompareAnimal implements Comparator{ + + //compare by type + public int compare(Pet o1, Pet o2) { + return o1.getClass().getSimpleName().compareToIgnoreCase(o2.getClass().getSimpleName()); + } + +} diff --git a/src/main/java/io/zipcoder/Dog.java b/src/main/java/io/zipcoder/Dog.java new file mode 100644 index 0000000..7ab32e3 --- /dev/null +++ b/src/main/java/io/zipcoder/Dog.java @@ -0,0 +1,18 @@ +package io.zipcoder; + +public class Dog extends Pet{ + public Dog(){ + + } + public Dog(String name){ + super(name); + } + + //method + @Override + public String speak(){ + String sound = "Woof"; + return sound; + } + +} diff --git a/src/main/java/io/zipcoder/Pet.java b/src/main/java/io/zipcoder/Pet.java new file mode 100644 index 0000000..94fee43 --- /dev/null +++ b/src/main/java/io/zipcoder/Pet.java @@ -0,0 +1,32 @@ +package io.zipcoder; + +public class Pet implements Comparable{ +private String petName; + + + //constructors + public Pet(){ + + } + public Pet (String petName){ + this.petName = petName; + } + + //methods + public String getPetName(){ + return this.petName; + } + + public void setPetName(String petName){ + this.petName = petName; + } + + public String speak(){ + String sound = "Generic Animal Sound"; + return sound; + } + + public int compareTo(Pet o) { + return this.getPetName().compareToIgnoreCase(o.getPetName()); + } +} diff --git a/src/main/java/io/zipcoder/PetFactory.java b/src/main/java/io/zipcoder/PetFactory.java new file mode 100644 index 0000000..42c6612 --- /dev/null +++ b/src/main/java/io/zipcoder/PetFactory.java @@ -0,0 +1,16 @@ +package io.zipcoder; + +public class PetFactory { + public static Pet createPet(String type, String name){ + if (type.equalsIgnoreCase("Dog")){ + return new Dog(name); + } else if (type.equalsIgnoreCase("Cat")){ + return new Cat(name); + }else if (type.equalsIgnoreCase("Bird")){ + return new Bird(name); + }else { + System.out.println("Not a pet type available!"); + } + return null; + } +} diff --git a/src/main/java/io/zipcoder/PetOwner.java b/src/main/java/io/zipcoder/PetOwner.java new file mode 100644 index 0000000..a8cfd1e --- /dev/null +++ b/src/main/java/io/zipcoder/PetOwner.java @@ -0,0 +1,61 @@ +package io.zipcoder; + +import java.util.ArrayList; +import java.util.Scanner; + +public class PetOwner { + CompareAnimal compareAnimal = new CompareAnimal(); + Scanner userInput = new Scanner(System.in); + static final ArrayList petList = new ArrayList(); + private Integer numberOfPets; + + + + public Integer askUserForNumberPets(){ + System.out.println("Please enter the number of pets you have"); + numberOfPets = Integer.parseInt(userInput.nextLine()); + return numberOfPets; + } + + public String askUserForPetName(){ + System.out.println("What is your pet's name?"); + String petName = userInput.nextLine(); + return petName; + } + + public String askUserForPetType(){ + System.out.println("What type of pet do you have?"); + String petType = userInput.nextLine(); + return petType; + } + + public void addPetsToList(Integer numberOfPets){ + for (int i =0; i < numberOfPets; i++){ + String name = askUserForPetName(); + String type = askUserForPetType(); + petList.add(PetFactory.createPet(type, name)); + } + } + + public static String printPetList(ArrayList petList){ + StringBuilder petListToString = new StringBuilder(); + for (Pet pet: petList){ + petListToString.append(pet.getPetName()); + petListToString.append(" the "); + petListToString.append(pet.getClass().getSimpleName()); + petListToString.append(" says "); + petListToString.append(pet.speak() + "\n"); + + } + String actualPetListToString = petListToString.toString(); + System.out.println(petListToString.toString()); + return actualPetListToString; + } + + public void start(){ + this.askUserForNumberPets(); + this.addPetsToList(numberOfPets); + this.printPetList(petList); + } + +} diff --git a/src/test/java/io/zipcoder/ApplicationTest.java b/src/test/java/io/zipcoder/ApplicationTest.java index b744df5..a8e42ba 100644 --- a/src/test/java/io/zipcoder/ApplicationTest.java +++ b/src/test/java/io/zipcoder/ApplicationTest.java @@ -1,5 +1,14 @@ package io.zipcoder; +import org.junit.Assert; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; + public class ApplicationTest { } + + diff --git a/src/test/java/io/zipcoder/BirdTest.java b/src/test/java/io/zipcoder/BirdTest.java new file mode 100644 index 0000000..fb0eaf1 --- /dev/null +++ b/src/test/java/io/zipcoder/BirdTest.java @@ -0,0 +1,50 @@ +package io.zipcoder; + +import org.junit.Assert; +import org.junit.Test; + +public class BirdTest { + + @Test + public void testSetBirdName() { + // Given + Bird testBird = new Bird(); + String expected = "Tweety"; + + // When + testBird.setPetName(expected); + String actual = testBird.getPetName(); + + // Then + Assert.assertEquals(expected, actual); + } + + @Test + public void testConstructorDogName() { + // Given + String expected = "Tweety"; + + // When + Bird testBird = new Bird(expected); + + // Then + String actual = testBird.getPetName(); + Assert.assertEquals(expected, actual); + } + + @Test + public void testspeak() { + // Given + String expected = "Tweet Tweet"; + + // When + Bird testBird = new Bird(); + + // Then + String actual = testBird.speak(); + Assert.assertEquals(expected, actual); + } + + } + + diff --git a/src/test/java/io/zipcoder/CatTest.java b/src/test/java/io/zipcoder/CatTest.java new file mode 100644 index 0000000..eab0bed --- /dev/null +++ b/src/test/java/io/zipcoder/CatTest.java @@ -0,0 +1,48 @@ +package io.zipcoder; + +import org.junit.Assert; +import org.junit.Test; + +public class CatTest { + + @Test + public void testSetCatName() { + // Given + Cat testCat = new Cat(); + String expected = "Fluffy"; + + // When + testCat.setPetName(expected); + String actual = testCat.getPetName(); + + // Then + Assert.assertEquals(expected, actual); + } + + @Test + public void testConstructorCatName() { + // Given + String expected = "Fluffy"; + + // When + Cat testCat = new Cat(expected); + + // Then + String actual = testCat.getPetName(); + Assert.assertEquals(expected, actual); + } + + @Test + public void testspeak() { + // Given + String expected = "Meow"; + + // When + Cat testCat = new Cat(); + + // Then + String actual = testCat.speak(); + Assert.assertEquals(expected, actual); + } + + } diff --git a/src/test/java/io/zipcoder/CompareAnimalClassTest.java b/src/test/java/io/zipcoder/CompareAnimalClassTest.java new file mode 100644 index 0000000..472e4d4 --- /dev/null +++ b/src/test/java/io/zipcoder/CompareAnimalClassTest.java @@ -0,0 +1,32 @@ +package io.zipcoder; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; + +public class CompareAnimalClassTest { + @Test + public void compareTest(){ + //Given + PetOwner petOwner = new PetOwner(); + CompareAnimal animal = new CompareAnimal(); + + //When + ArrayList petList = new ArrayList(); + Cat catTest = new Cat("Maus"); + Dog dogTest = new Dog("Hemi"); + Bird birdTest = new Bird("Tera"); + petList.add(catTest); + petList.add(dogTest); + petList.add(birdTest); + + Collections.sort(petList, animal); + //Then + String expected = "Tera the Bird\n" + "Maus the Cat\n" + "Hemi the Dog\n"; + String actual = PetOwner.printPetList(petList); + Assert.assertEquals(expected, actual); + } +} diff --git a/src/test/java/io/zipcoder/DogTest.java b/src/test/java/io/zipcoder/DogTest.java new file mode 100644 index 0000000..dce6deb --- /dev/null +++ b/src/test/java/io/zipcoder/DogTest.java @@ -0,0 +1,47 @@ +package io.zipcoder; + +import org.junit.Assert; +import org.junit.Test; + +public class DogTest { + @Test + public void testSetDogName() { + // Given + Dog testDog = new Dog(); + String expected = "Spot"; + + // When + testDog.setPetName(expected); + String actual = testDog.getPetName(); + + // Then + Assert.assertEquals(expected, actual); + } + + @Test + public void testConstructorDogName() { + // Given + String expected = "Spot"; + + // When + Dog testDog = new Dog(expected); + + // Then + String actual = testDog.getPetName(); + Assert.assertEquals(expected, actual); + } + + @Test + public void testspeak() { + // Given + String expected = "Woof"; + + // When + Dog testDog = new Dog(); + + // Then + String actual = testDog.speak(); + Assert.assertEquals(expected, actual); + } + +} diff --git a/src/test/java/io/zipcoder/PetFactoryTest.java b/src/test/java/io/zipcoder/PetFactoryTest.java new file mode 100644 index 0000000..19395d7 --- /dev/null +++ b/src/test/java/io/zipcoder/PetFactoryTest.java @@ -0,0 +1,45 @@ +package io.zipcoder; + +import org.junit.Assert; +import org.junit.Test; + +public class PetFactoryTest { + @Test + public void testCreatePet(){ + //given + String name = "Spot"; + String type = "Dog"; + + //when + Pet testAnimal = PetFactory.createPet(type, name); + + Assert.assertEquals(testAnimal.getPetName(),name); + Assert.assertTrue(testAnimal instanceof Dog); + } + + @Test + public void testCreatePet2(){ + //given + String name = "Fluffy"; + String type = "Cat"; + + //when + Pet testAnimal = PetFactory.createPet(type, name); + + Assert.assertEquals(testAnimal.getPetName(),name); + Assert.assertTrue(testAnimal instanceof Cat); + } + + @Test + public void testCreatePet3(){ + //given + String name = "Tweety"; + String type = "Bird"; + + //when + Pet testAnimal = PetFactory.createPet(type, name); + + Assert.assertEquals(testAnimal.getPetName(),name); + Assert.assertTrue(testAnimal instanceof Bird); + } +} diff --git a/src/test/java/io/zipcoder/PetOwnerTest.java b/src/test/java/io/zipcoder/PetOwnerTest.java new file mode 100644 index 0000000..5b27d45 --- /dev/null +++ b/src/test/java/io/zipcoder/PetOwnerTest.java @@ -0,0 +1,13 @@ +package io.zipcoder; + +import org.junit.Test; + +public class PetOwnerTest { + @Test + public void petListToStringTest(){ + //Given + + //When + //Then + } +} diff --git a/src/test/java/io/zipcoder/PetTest.java b/src/test/java/io/zipcoder/PetTest.java new file mode 100644 index 0000000..6737379 --- /dev/null +++ b/src/test/java/io/zipcoder/PetTest.java @@ -0,0 +1,71 @@ +package io.zipcoder; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Collections; + +import static org.junit.Assert.*; + +public class PetTest { + @Test + public void testSetPetName() { + // Given + Pet testPet = new Pet(); + String expected = "Spot"; + + // When + testPet.setPetName(expected); + String actual = testPet.getPetName(); + + // Then + Assert.assertEquals(expected, actual); + } + + @Test + public void testConstructorPetName() { + // Given + String expected = "Spot"; + + // When + Pet testPet = new Pet(expected); + + // Then + String actual = testPet.getPetName(); + Assert.assertEquals(expected, actual); + } + + @Test + public void testspeak() { + // Given + String expected = "Generic Animal Sound"; + + // When + Pet testPet = new Pet(); + + // Then + String actual = testPet.speak(); + Assert.assertEquals(expected, actual); + } + @Test + public void compareToTest(){ + //Given + PetOwner petOwner = new PetOwner(); + + //When + ArrayList petList = new ArrayList(); + Cat catTest = new Cat("Maus"); + Dog dogTest = new Dog("Hemi"); + Bird birdTest = new Bird("Tera"); + petList.add(catTest); + petList.add(dogTest); + petList.add(birdTest); + + Collections.sort(petList); + //Then + String expected = "Hemi the Dog says Woof\n" + "Maus the Cat says Meow\n" + "Tera the Bird says Tweet Tweet\n"; + String actual = PetOwner.printPetList(petList); + Assert.assertEquals(expected, actual); + } +} \ No newline at end of file