diff --git a/pom.xml b/pom.xml index d73c078..5511364 100644 --- a/pom.xml +++ b/pom.xml @@ -7,6 +7,18 @@ io.zipcoder Interfaces 1.0-SNAPSHOT + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.6 + 1.6 + + + + diff --git a/src/main/java/io/zipcoder/Application.java b/src/main/java/io/zipcoder/Application.java index 3a257cb..58e5ed1 100644 --- a/src/main/java/io/zipcoder/Application.java +++ b/src/main/java/io/zipcoder/Application.java @@ -1,5 +1,62 @@ package io.zipcoder; +import java.util.Scanner; + public class Application { + + Scanner userInput = new Scanner(System.in); + int numberOfPets; + String petType; + String petName; + + public static void main(String[] args) { + Application app = new Application(); + app.setNumberOfPets(); + app.buildPetInventory(); + } + + public void setNumberOfPets() { + + System.out.println("Hello. How many cool pets do you have?\n(Cool pets are defined as dogs, cats or otters)"); + numberOfPets = userInput.nextInt(); + } + + + public void buildPetInventory() { + + PetInventory newInventory = new PetInventory(); + System.out.println("Tell me one pet at a time..."); + + while (numberOfPets > 0) { + System.out.println("What is your pet type?"); + petType = userInput.next(); + + System.out.println("And its name?"); + petName = userInput.next(); + + newInventory.addPet(petType, petName); + + numberOfPets--; + + if (numberOfPets > 0) { + System.out.println("Thanks. Next..."); + } + } + + System.out.println("Okay, that's it! These are your cool, loud pets:"); + System.out.println(newInventory.listPetsAndSpeak()); + } + + public String getPetType() { + return petType; + } + + public int getNumberOfPets() { + return numberOfPets; + } + + public String getPetName() { + return petName; + } } diff --git a/src/main/java/io/zipcoder/PetInventory.java b/src/main/java/io/zipcoder/PetInventory.java new file mode 100644 index 0000000..43c7de3 --- /dev/null +++ b/src/main/java/io/zipcoder/PetInventory.java @@ -0,0 +1,144 @@ +package io.zipcoder; + +import io.zipcoder.pets.*; + +import java.lang.reflect.Array; +import java.util.*; + +public class PetInventory { + + HashMap> pets; + + public PetInventory() { + this.pets = new HashMap>(); + } + + + public void addPet(String petType, String petName) { + int petTypeInt; + + if (petType.equalsIgnoreCase("cat")) { + petTypeInt = 1; + } else if (petType.equalsIgnoreCase("dog")) { + petTypeInt = 2; + } else { + petTypeInt = 3; + } + + switch (petTypeInt) { + case 1: + Cat newCat = new Cat(petName); + if (pets.containsKey(petType)) { + pets.get(petType).add(newCat); + } else { + ArrayList newCatList = new ArrayList(); + newCatList.add(newCat); + pets.put(petType, newCatList); + } + break; + case 2: + Dog newDog = new Dog(petName); + if (pets.containsKey(petType)) { + pets.get(petType).add(newDog); + } else { + ArrayList newDogList = new ArrayList(); + newDogList.add(newDog); + pets.put(petType, newDogList); + } + break; + case 3: + Otter newOtter = new Otter(petName); + if (pets.containsKey(petType)) { + pets.get(petType).add(newOtter); + } else { + ArrayList newOtterList = new ArrayList(); + newOtterList.add(newOtter); + pets.put(petType, newOtterList); + } + break; + } + + } + + public void removePet(String petName) { + + for (Map.Entry> entry : pets.entrySet()) { + for (int i = 0; i < pets.entrySet().size(); i++) { + if (entry.getValue().get(i).getName().equalsIgnoreCase(petName)) { + entry.getValue().remove(i); + } + } + } + } + + + public String getPetType(String petName) { + + for (String key : pets.keySet()) { + for (int i = 0; i < getPetsByType(key).size(); i++) { + if (getPetsByType(key).get(i).getName().equalsIgnoreCase(petName)) { + return key; + } + } + } return null; + } + + public String listPetsAndSpeak() { + + String list = ""; + Set keys = pets.keySet(); + for (String i : keys) { + for (int j = 0; j < pets.get(i).size(); j++) + list += pets.get(i).get(j).getName() + "==>" + pets.get(i).get(j).speak() + "\n"; + } + return list; + + } + + public ArrayList getPetsByType(String petType) { + return pets.get(petType); + } + + public ArrayList getAllPets() { + + ArrayList allPets = new ArrayList(); + + for (int i = 0; i < getPetsByType("cat").size(); i++) { + allPets.add(getPetsByType("cat").get(i)); + } + for (int i = 0; i < getPetsByType("dog").size(); i++) { + allPets.add(getPetsByType("dog").get(i)); + } + for (int i = 0; i < getPetsByType("otter").size(); i++) { + allPets.add(getPetsByType("otter").get(i)); + } + + return allPets; + } + + public ArrayList sortedPetsByName() { + + ArrayList allPets = getAllPets(); + + Collections.sort(allPets); + + return allPets; + } + + public ArrayList sortedPetsByType() { + + Compare comp = new Compare(); + + ArrayList allPets = getAllPets(); + + Collections.sort(allPets, comp); + + return allPets; + + } + + + + + +} 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..2d7c732 --- /dev/null +++ b/src/main/java/io/zipcoder/pets/Cat.java @@ -0,0 +1,18 @@ +package io.zipcoder.pets; + +public class Cat extends Pet { + + + public Cat(String catName) { + super(catName); + } + + + public String speak() { + + return "Meow!"; + } + + + +} diff --git a/src/main/java/io/zipcoder/pets/Compare.java b/src/main/java/io/zipcoder/pets/Compare.java new file mode 100644 index 0000000..afaa300 --- /dev/null +++ b/src/main/java/io/zipcoder/pets/Compare.java @@ -0,0 +1,23 @@ +package io.zipcoder.pets; + +import java.util.Comparator; + +public class Compare implements Comparator { + + @Override + public int compare(Pet thisPet, Pet anotherPet) { + + String thisPetClass = thisPet.getClass().getSimpleName(); + String otherPetClass = anotherPet.getClass().getSimpleName(); + int result = -(otherPetClass.compareToIgnoreCase(thisPetClass)); + boolean sameClass = (result ==0); + + if (sameClass) { + String thisPetName = thisPet.getName(); + String otherPetName = anotherPet.getName(); + result = -(otherPetName.compareToIgnoreCase(thisPetName)); + } + + return result; + } +} 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..7a99710 --- /dev/null +++ b/src/main/java/io/zipcoder/pets/Dog.java @@ -0,0 +1,18 @@ +package io.zipcoder.pets; + +public class Dog extends Pet { + + + public Dog(String dogName) { + super(dogName); + } + + + public String speak() { + + return "Woof!"; + + } + + +} diff --git a/src/main/java/io/zipcoder/pets/Otter.java b/src/main/java/io/zipcoder/pets/Otter.java new file mode 100644 index 0000000..089f96a --- /dev/null +++ b/src/main/java/io/zipcoder/pets/Otter.java @@ -0,0 +1,17 @@ +package io.zipcoder.pets; + +public class Otter extends Pet { + + + public Otter(String otterName) { + super(otterName); + } + + + public String speak() { + + return "Muahaha"; + } + + +} diff --git a/src/main/java/io/zipcoder/pets/Pet.java b/src/main/java/io/zipcoder/pets/Pet.java new file mode 100644 index 0000000..71c88a2 --- /dev/null +++ b/src/main/java/io/zipcoder/pets/Pet.java @@ -0,0 +1,44 @@ +package io.zipcoder.pets; + +abstract public class Pet implements PetBehavior, Comparable { + + private String name; + + public Pet(String aName) { + this.name = aName; + } + + public String speak() { + return null; + } + + public String getName() { + return this.name; + } + + public void setName(String aName) { + this.name = aName; + } + + @Override + public int compareTo(Pet anotherPet) { + String otherPetName = anotherPet.getName(); + int result = -(otherPetName.compareTo(name)); + boolean sameName = (result == 0); + if (sameName) { + Class otherPetClass = anotherPet.getClass(); + String otherPetClassName = otherPetClass.getSimpleName(); + + Class thisPetClass = this.getClass(); + String thisPetClassName = thisPetClass.getSimpleName(); + + result = -(otherPetClassName.compareTo(thisPetClassName)); + } + return result; + } + + + + + +} diff --git a/src/main/java/io/zipcoder/pets/PetBehavior.java b/src/main/java/io/zipcoder/pets/PetBehavior.java new file mode 100644 index 0000000..a6fb654 --- /dev/null +++ b/src/main/java/io/zipcoder/pets/PetBehavior.java @@ -0,0 +1,5 @@ +package io.zipcoder.pets; + +public interface PetBehavior { + public String speak(); +} diff --git a/src/test/java/io/zipcoder/PetInventoryTest.java b/src/test/java/io/zipcoder/PetInventoryTest.java new file mode 100644 index 0000000..90295b5 --- /dev/null +++ b/src/test/java/io/zipcoder/PetInventoryTest.java @@ -0,0 +1,143 @@ +package io.zipcoder; + +import io.zipcoder.pets.*; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.lang.reflect.Array; +import java.util.*; + +public class PetInventoryTest { + + PetInventory petListTest; + + @Before + public void setup() { + petListTest = new PetInventory(); + } + + @Test + public void addPetTest() { + Dog myDog = new Dog( "Fighter"); + ArrayList arrayListOfDogsTest = new ArrayList(); + arrayListOfDogsTest.add(myDog); + petListTest.pets.put("dog", arrayListOfDogsTest); + petListTest.addPet("dog", "Sparky"); + int expected = 2; + int actual = petListTest.pets.get("dog").size(); + Assert.assertEquals(expected, actual); + } + + @Test + public void addPetTest2() { + petListTest.addPet("cat", "first"); + petListTest.addPet("cat", "second"); + petListTest.addPet("cat", "third"); + String expected = "second"; + String actual = petListTest.pets.get("cat").get(1).getName(); + Assert.assertEquals(expected, actual); + } + + + @Test + public void removePetTest1() { + petListTest.addPet("otter", "Fire"); + petListTest.addPet("otter", "Fury"); + petListTest.removePet("Fire"); + int expected = 1; + int actual = petListTest.pets.get("otter").size(); + Assert.assertEquals(expected, actual); + } + + + @Test + public void listPetsAndSpeak() { + petListTest.addPet("cat", "Zina"); + petListTest.addPet("otter", "Otty"); + + String expected = "Zina==>Meow!\n" + + "Otty==>Muahaha\n"; + + String actual = petListTest.listPetsAndSpeak(); + Assert.assertEquals(expected, actual); + } + + @Test + public void getPetType() { + petListTest.addPet("cat", "Zina"); + petListTest.addPet("otter", "Otty"); + petListTest.addPet("cat", "Tissue"); + + String expected = "cat"; + String actual = petListTest.getPetType("Zina"); + + Assert.assertEquals(expected, actual); + } + + @Test + public void getPetsByTypeTest() { + petListTest.addPet("cat", "Zina"); + petListTest.addPet("otter", "Otty"); + petListTest.addPet("cat", "Tissue"); + String expected = "Tissue"; + String actual = petListTest.getPetsByType("cat").get(1).getName(); + Assert.assertEquals(expected, actual); + } + + @Test + public void getAllPetsTest() { + petListTest.addPet("cat", "Zina"); + petListTest.addPet("otter", "Felix"); + petListTest.addPet("dog", "Arnold"); + petListTest.addPet("otter", "Clubber"); + petListTest.addPet("cat", "Felix"); + String expected = "Clubber"; + String actual = petListTest.getAllPets().get(4).getName(); + Assert.assertEquals(expected, actual); + } + + @Test + public void sortPetsByNameTest() { + petListTest.addPet("cat", "Zina"); + petListTest.addPet("otter", "Felix"); + petListTest.addPet("dog", "Arnold"); + petListTest.addPet("otter", "Clubber"); + petListTest.addPet("cat", "Felix"); + // from: Zina, Felix(cat), Arnold, Felix (otter), Clubber + // to: Arnold, Clubber, Felix (cat), Felix (otter), Zina + String expected = "Meow!"; + String actual = petListTest.sortedPetsByName().get(2).speak(); + Assert.assertEquals(expected, actual); + } + + @Test + public void sortPetsByTypeTest1() { + petListTest.addPet("cat", "Zina"); + petListTest.addPet("otter", "Felix"); + petListTest.addPet("dog", "Arnold"); + petListTest.addPet("otter", "Clubber"); + petListTest.addPet("cat", "Felix"); + // from: Zina, Felix (cat), Arnold, Felix (otter), Clubber + // to: Felix (cat), Zina, Arnold, Clubber, Felix (otter) + + String expected = "Clubber"; + String actual = petListTest.sortedPetsByType().get(3).getName(); + Assert.assertEquals(expected, actual); + } + + @Test + public void sortPetsByTypeTest2() { + petListTest.addPet("cat", "Zina"); + petListTest.addPet("otter", "Felix"); + petListTest.addPet("dog", "Arnold"); + petListTest.addPet("otter", "Clubber"); + petListTest.addPet("cat", "Felix"); + // from: Zina, Felix (cat), Arnold, Felix (otter), Clubber + // to: Felix (cat), Zina, Arnold, Clubber, Felix (otter) + + String expected = "Zina"; + String actual = petListTest.sortedPetsByType().get(1).getName(); + 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..e22d8ef --- /dev/null +++ b/src/test/java/io/zipcoder/pets/CatTest.java @@ -0,0 +1,30 @@ +package io.zipcoder.pets; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class CatTest { + public Cat aCatTest; + + @Before + public void setup() { + aCatTest = new Cat("Zina"); + } + @Test + public void speakTest(){ + + String expected = "Meow!"; + String actual = aCatTest.speak(); + Assert.assertEquals(expected,actual); + + } + @Test + public void setCatNameTest(){ + String expected = "Gabby"; + aCatTest.setName("Gabby"); + String actual = aCatTest.getName(); + 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..9cabcaf --- /dev/null +++ b/src/test/java/io/zipcoder/pets/DogTest.java @@ -0,0 +1,30 @@ +package io.zipcoder.pets; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class DogTest { + + Dog aDogTest; + + @Before + public void setup(){ + aDogTest=new Dog("Fido"); + } + @Test + public void speakTest(){ + String expected = "Woof!"; + String actual = aDogTest.speak(); + + Assert.assertEquals(expected,actual); + } + @Test + public void setDogNameTest(){ + aDogTest.setName("aaaa"); + String expected = "aaaa"; + String actual = aDogTest.getName(); + + Assert.assertEquals(expected,actual); + } +} diff --git a/src/test/java/io/zipcoder/pets/OtterTest.java b/src/test/java/io/zipcoder/pets/OtterTest.java new file mode 100644 index 0000000..1185478 --- /dev/null +++ b/src/test/java/io/zipcoder/pets/OtterTest.java @@ -0,0 +1,30 @@ +package io.zipcoder.pets; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class OtterTest { + Otter anOtter; + + @Before + public void setup(){ + anOtter = new Otter("Sparky"); + } + + @Test + public void speakTest(){ + String expected = "Muahaha"; + String actual = anOtter.speak(); + Assert.assertEquals(expected,actual); + } + @Test + public void setOtterNameTest(){ + anOtter.setName("Hero"); + String expected = "Hero"; + String actual = anOtter.getName(); + + Assert.assertEquals(expected,actual); + } + +}