From 8901039ff54962d449d9e04b6d287f19400fda1f Mon Sep 17 00:00:00 2001 From: Vincent Gasbarro Date: Mon, 26 Feb 2018 15:39:20 -0500 Subject: [PATCH 1/6] method stubs --- src/main/java/io/zipcoder/Application.java | 43 +++++++++++++++++++++ src/main/java/io/zipcoder/PetInventory.java | 36 +++++++++++++++++ src/main/java/io/zipcoder/pets/Cat.java | 21 ++++++++++ src/main/java/io/zipcoder/pets/Dog.java | 22 +++++++++++ src/main/java/io/zipcoder/pets/Otter.java | 20 ++++++++++ src/main/java/io/zipcoder/pets/Pet.java | 19 +++++++++ 6 files changed, 161 insertions(+) create mode 100644 src/main/java/io/zipcoder/PetInventory.java create mode 100644 src/main/java/io/zipcoder/pets/Cat.java create mode 100644 src/main/java/io/zipcoder/pets/Dog.java create mode 100644 src/main/java/io/zipcoder/pets/Otter.java create mode 100644 src/main/java/io/zipcoder/pets/Pet.java diff --git a/src/main/java/io/zipcoder/Application.java b/src/main/java/io/zipcoder/Application.java index 3a257cb..d601b04 100644 --- a/src/main/java/io/zipcoder/Application.java +++ b/src/main/java/io/zipcoder/Application.java @@ -1,5 +1,48 @@ package io.zipcoder; +import java.util.Scanner; + public class Application { + + Scanner scanner; + int numberOfPets; + String petType; + String petName; + + + public int getNumberOfPets() { + return 0; + } + + public void setNumberOfPets(int numberOfPets) { + + } + + public String getPetType() { + return null; + } + + public void setPetType(String somePetType) { + + } + + public String getPetName() { + return null; + } + + public void setPetName(String aPetName) { + + } + + + + + public static void main(String[] args) { + + } + + + + } diff --git a/src/main/java/io/zipcoder/PetInventory.java b/src/main/java/io/zipcoder/PetInventory.java new file mode 100644 index 0000000..56ca064 --- /dev/null +++ b/src/main/java/io/zipcoder/PetInventory.java @@ -0,0 +1,36 @@ +package io.zipcoder; + +import io.zipcoder.pets.Pet; + +import java.util.ArrayList; +import java.util.HashMap; + +public class PetInventory { + + HashMap pets; + + public void addPet(String petType, String petName) { + + } + + public void removePet(Pet aPet) { + + } + + public String listPets() { + return null; + } + + public String getPetType(String petName) { + return null; + } + + public ArrayList lookUpByType(String petType) { + return null; + } + + public Pet getPet(String petName) { + return null; + } + +} 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..683c0a8 --- /dev/null +++ b/src/main/java/io/zipcoder/pets/Cat.java @@ -0,0 +1,21 @@ +package io.zipcoder.pets; + +public class Cat extends Pet { + + private String catName; + + + public String speak() { + return null; + } + + public void setCatName(String aCatName) { + + } + + public String getCatName() { + return null; + } + + +} 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..fe85eee --- /dev/null +++ b/src/main/java/io/zipcoder/pets/Dog.java @@ -0,0 +1,22 @@ +package io.zipcoder.pets; + +public class Dog extends Pet { + + private String dogName; + + public String speak() { + return null; + } + + public void setDogName(String aDogName) { + + } + + public String getDogName() { + return null; + } + + + + +} 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..5a14ce1 --- /dev/null +++ b/src/main/java/io/zipcoder/pets/Otter.java @@ -0,0 +1,20 @@ +package io.zipcoder.pets; + +public class Otter extends Pet { + + private String otterName; + + + public String speak() { + return null; + } + + public void setOtterName(String anOtterName) { + + } + + public String getOtterName() { + return null; + } + +} 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..ff45137 --- /dev/null +++ b/src/main/java/io/zipcoder/pets/Pet.java @@ -0,0 +1,19 @@ +package io.zipcoder.pets; + +abstract public class Pet { + + private String name; + + public String speak() { + return null; + } + + public String getName() { + return null; + } + + public void setName(String aPetName) { + + } + +} From dd48ab9747849f67e5eaeb0bd6a0632e83c50126 Mon Sep 17 00:00:00 2001 From: Kibret Tecle Date: Mon, 26 Feb 2018 20:24:06 -0500 Subject: [PATCH 2/6] Tested Completed --- src/main/java/io/zipcoder/PetInventory.java | 11 +- src/main/java/io/zipcoder/pets/Cat.java | 11 +- src/main/java/io/zipcoder/pets/Dog.java | 16 ++- src/main/java/io/zipcoder/pets/Otter.java | 12 +- src/main/java/io/zipcoder/pets/Pet.java | 11 +- .../java/io/zipcoder/pets/PetBehavior.java | 5 + .../java/io/zipcoder/PetInventoryTest.java | 128 ++++++++++++++++++ src/test/java/io/zipcoder/pets/CatTest.java | 30 ++++ src/test/java/io/zipcoder/pets/DogTest.java | 30 ++++ src/test/java/io/zipcoder/pets/OtterTest.java | 30 ++++ 10 files changed, 270 insertions(+), 14 deletions(-) create mode 100644 src/main/java/io/zipcoder/pets/PetBehavior.java create mode 100644 src/test/java/io/zipcoder/PetInventoryTest.java create mode 100644 src/test/java/io/zipcoder/pets/CatTest.java create mode 100644 src/test/java/io/zipcoder/pets/DogTest.java create mode 100644 src/test/java/io/zipcoder/pets/OtterTest.java diff --git a/src/main/java/io/zipcoder/PetInventory.java b/src/main/java/io/zipcoder/PetInventory.java index 56ca064..8c922a2 100644 --- a/src/main/java/io/zipcoder/PetInventory.java +++ b/src/main/java/io/zipcoder/PetInventory.java @@ -7,13 +7,18 @@ public class PetInventory { - HashMap pets; + HashMap> pets; + + public PetInventory(){ + this.pets = new HashMap>(); + + } public void addPet(String petType, String petName) { } - public void removePet(Pet aPet) { + public void removePet(String petName) { } @@ -25,7 +30,7 @@ public String getPetType(String petName) { return null; } - public ArrayList lookUpByType(String petType) { + public ArrayList listOfPetsByName() { return null; } diff --git a/src/main/java/io/zipcoder/pets/Cat.java b/src/main/java/io/zipcoder/pets/Cat.java index 683c0a8..3ee85f9 100644 --- a/src/main/java/io/zipcoder/pets/Cat.java +++ b/src/main/java/io/zipcoder/pets/Cat.java @@ -4,17 +4,24 @@ public class Cat extends Pet { private String catName; + public Cat(String catName) { + this.catName = catName; + } + public String speak() { - return null; + + return "Meow!"; } public void setCatName(String aCatName) { + this.catName=aCatName; } public String getCatName() { - return null; + + return this.catName; } diff --git a/src/main/java/io/zipcoder/pets/Dog.java b/src/main/java/io/zipcoder/pets/Dog.java index fe85eee..b12028a 100644 --- a/src/main/java/io/zipcoder/pets/Dog.java +++ b/src/main/java/io/zipcoder/pets/Dog.java @@ -1,19 +1,25 @@ package io.zipcoder.pets; public class Dog extends Pet { - private String dogName; - public String speak() { - return null; + public Dog(String dogName) { + this.dogName = dogName; } - public void setDogName(String aDogName) { + + public String speak(){ + return "Woof!"; + } + + public void setDogName(String aDogName) { + this.dogName = aDogName; } public String getDogName() { - return null; + + return dogName; } diff --git a/src/main/java/io/zipcoder/pets/Otter.java b/src/main/java/io/zipcoder/pets/Otter.java index 5a14ce1..4974add 100644 --- a/src/main/java/io/zipcoder/pets/Otter.java +++ b/src/main/java/io/zipcoder/pets/Otter.java @@ -4,17 +4,23 @@ public class Otter extends Pet { private String otterName; + public Otter(String otterName) { + this.otterName = otterName; + } + public String speak() { - return null; + + return "Hello"; } public void setOtterName(String anOtterName) { - + this.otterName=anOtterName; } public String getOtterName() { - return null; + + return this.otterName; } } diff --git a/src/main/java/io/zipcoder/pets/Pet.java b/src/main/java/io/zipcoder/pets/Pet.java index ff45137..ca096e1 100644 --- a/src/main/java/io/zipcoder/pets/Pet.java +++ b/src/main/java/io/zipcoder/pets/Pet.java @@ -1,6 +1,6 @@ package io.zipcoder.pets; -abstract public class Pet { +abstract public class Pet implements PetBehavior,Comparable{ private String name; @@ -15,5 +15,14 @@ public String getName() { public void setName(String aPetName) { } + public int compareTo(Pet anotherPet){ + if (getName().compareTo(anotherPet.getName()) > 0) { + return 1; + } else if (getName().compareTo(anotherPet.getName()) < 0) { + return -1; + } else { + return getClass().getSimpleName().compareTo(anotherPet.getClass().getSimpleName()); + } + } } 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..5f06320 --- /dev/null +++ b/src/test/java/io/zipcoder/PetInventoryTest.java @@ -0,0 +1,128 @@ +package io.zipcoder; + +import io.zipcoder.pets.Cat; +import io.zipcoder.pets.Dog; +import io.zipcoder.pets.Otter; +import io.zipcoder.pets.Pet; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; + +public class PetInventoryTest { + + PetInventory petListTest; + + @Before + public void setup(){ + + petListTest= new PetInventory(); + + } + + @Test + public void addPetTest(){ + Dog myDog = new Dog("Fighter"); + ArrayListarrayListOfDogsTest = new ArrayList(); + arrayListOfDogsTest.add(myDog); + petListTest.pets.put("Dog",arrayListOfDogsTest); + petListTest.addPet("Dog","Sparky"); + Pet expected = myDog; + Pet actual = petListTest.pets.get("Dog").get(0); + + Assert.assertEquals(expected,actual); + } + + @Test + public void removePetTest(){ + Cat myCat = new Cat("Zina"); + Cat anotherCat = new Cat("Kitty"); + ArrayListcatsTest = new ArrayList(); + catsTest.add(myCat); + catsTest.add(anotherCat); + petListTest.pets.put("Cat",catsTest); + + petListTest.removePet("kitty"); + + int expected =1; + int actual = petListTest.pets.get("Cat").size(); + + Assert.assertEquals(expected,actual); + + } + @Test + public void listPetsTest(){ + Cat myCat = new Cat("Zina"); + Otter myOtter = new Otter("Kitty"); + ArrayListcatsTest = new ArrayList(); + ArrayListotterTest = new ArrayList(); + catsTest.add(myCat); + otterTest.add(myOtter); + + petListTest.pets.put("Cat",catsTest); + petListTest.pets.put("Kitty",otterTest); + + String expected = "Zina==>Meow\n"+ + "Kitty==>Muahaha"; + String actual = petListTest.listPets(); + + Assert.assertEquals(expected,actual); + + + } + @Test + public void getPetType(){ + Cat myCat = new Cat("Zina"); + Otter myOtter = new Otter("Kitty"); + ArrayListcatsTest = new ArrayList(); + ArrayListotterTest = new ArrayList(); + catsTest.add(myCat); + otterTest.add(myOtter); + + petListTest.pets.put("Cat",catsTest); + petListTest.pets.put("Kitty",otterTest); + + String expected = "Cat"; + String actual = petListTest.getPetType("Zina"); + + Assert.assertEquals(expected,actual); + + } + + @Test + + public void listOfPetsByNameTest(){ + + Dog myDog = new Dog("Fighter"); + ArrayListarrayListOfDogsTest = new ArrayList(); + arrayListOfDogsTest.add(myDog); + + Cat myCat = new Cat("Zina"); + Otter myOtter = new Otter("Kitty"); + ArrayListcatsTest = new ArrayList(); + ArrayListotterTest = new ArrayList(); + catsTest.add(myCat); + otterTest.add(myOtter); + + petListTest.pets.put("Cat",catsTest); + petListTest.pets.put("Kitty",otterTest); + petListTest.pets.put("Dog",arrayListOfDogsTest); + + ArrayList expected = new ArrayList(); + expected.add("Fighter"); + expected.add("Kitty"); + expected.add("Zina"); + + ArrayList actual = petListTest.listOfPetsByName(); + + 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..94428ee --- /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.setCatName("Gabby"); + String actual = aCatTest.getCatName(); + 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..7ad9ed1 --- /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.setDogName("aaaa"); + String expected = "aaaa"; + String actual = aDogTest.getDogName(); + + 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..0420aac --- /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 = "Hello"; + String actual = anOtter.speak(); + Assert.assertEquals(expected,actual); + } + @Test + public void setOtterNameTest(){ + anOtter.setOtterName("Hero"); + String expected = "Hero"; + String actual = anOtter.getOtterName(); + + Assert.assertEquals(expected,actual); + } + +} From 87a022f04b7c0cffe2f775d76cc8fcf17a24339f Mon Sep 17 00:00:00 2001 From: Vincent Gasbarro Date: Mon, 26 Feb 2018 23:18:59 -0500 Subject: [PATCH 3/6] some updates --- src/main/java/io/zipcoder/PetInventory.java | 70 +++++++++++++++++-- src/main/java/io/zipcoder/pets/Cat.java | 4 +- src/main/java/io/zipcoder/pets/Dog.java | 4 +- src/main/java/io/zipcoder/pets/Otter.java | 6 +- src/main/java/io/zipcoder/pets/Pet.java | 1 + .../java/io/zipcoder/PetInventoryTest.java | 32 ++++----- src/test/java/io/zipcoder/pets/CatTest.java | 4 +- src/test/java/io/zipcoder/pets/DogTest.java | 4 +- src/test/java/io/zipcoder/pets/OtterTest.java | 4 +- 9 files changed, 94 insertions(+), 35 deletions(-) diff --git a/src/main/java/io/zipcoder/PetInventory.java b/src/main/java/io/zipcoder/PetInventory.java index 8c922a2..cc6722e 100644 --- a/src/main/java/io/zipcoder/PetInventory.java +++ b/src/main/java/io/zipcoder/PetInventory.java @@ -1,29 +1,91 @@ package io.zipcoder; +import io.zipcoder.pets.Cat; +import io.zipcoder.pets.Dog; +import io.zipcoder.pets.Otter; import io.zipcoder.pets.Pet; import java.util.ArrayList; import java.util.HashMap; +import java.util.Map; +import java.util.Set; public class PetInventory { HashMap> pets; - public PetInventory(){ + 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("cat")) { + pets.get("cat").add(newCat); + } else { + ArrayList newCatList = new ArrayList(); + newCatList.add(newCat); + pets.put("cat", newCatList); + } + break; + case 2: + Dog newDog = new Dog(petName); + if (pets.containsKey("dog")) { + pets.get("dog").add(newDog); + } else { + ArrayList newDogList = new ArrayList(); + newDogList.add(newDog); + pets.put("dog", newDogList); + } + break; + case 3: + Otter newOtter = new Otter(petName); + if (pets.containsKey("otter")) { + pets.get("otter").add(newOtter); + } else { + ArrayList newOtterList = new ArrayList(); + newOtterList.add(newOtter); + pets.put("otter", 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 listPets() { - return null; + + 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 String getPetType(String petName) { @@ -34,8 +96,6 @@ public ArrayList listOfPetsByName() { return null; } - public Pet getPet(String petName) { - return null; - } + } diff --git a/src/main/java/io/zipcoder/pets/Cat.java b/src/main/java/io/zipcoder/pets/Cat.java index 3ee85f9..622e696 100644 --- a/src/main/java/io/zipcoder/pets/Cat.java +++ b/src/main/java/io/zipcoder/pets/Cat.java @@ -14,12 +14,12 @@ public String speak() { return "Meow!"; } - public void setCatName(String aCatName) { + public void setName(String aCatName) { this.catName=aCatName; } - public String getCatName() { + public String getName() { return this.catName; } diff --git a/src/main/java/io/zipcoder/pets/Dog.java b/src/main/java/io/zipcoder/pets/Dog.java index b12028a..770c715 100644 --- a/src/main/java/io/zipcoder/pets/Dog.java +++ b/src/main/java/io/zipcoder/pets/Dog.java @@ -13,11 +13,11 @@ public String speak(){ return "Woof!"; } - public void setDogName(String aDogName) { + public void setName(String aDogName) { this.dogName = aDogName; } - public String getDogName() { + public String getName() { return dogName; } diff --git a/src/main/java/io/zipcoder/pets/Otter.java b/src/main/java/io/zipcoder/pets/Otter.java index 4974add..01d87e1 100644 --- a/src/main/java/io/zipcoder/pets/Otter.java +++ b/src/main/java/io/zipcoder/pets/Otter.java @@ -11,14 +11,14 @@ public Otter(String otterName) { public String speak() { - return "Hello"; + return "Muahaha"; } - public void setOtterName(String anOtterName) { + public void setName(String anOtterName) { this.otterName=anOtterName; } - public String getOtterName() { + public String getName() { return this.otterName; } diff --git a/src/main/java/io/zipcoder/pets/Pet.java b/src/main/java/io/zipcoder/pets/Pet.java index ca096e1..af9c981 100644 --- a/src/main/java/io/zipcoder/pets/Pet.java +++ b/src/main/java/io/zipcoder/pets/Pet.java @@ -4,6 +4,7 @@ abstract public class Pet implements PetBehavior,Comparable{ private String name; + public String speak() { return null; } diff --git a/src/test/java/io/zipcoder/PetInventoryTest.java b/src/test/java/io/zipcoder/PetInventoryTest.java index 5f06320..afea13e 100644 --- a/src/test/java/io/zipcoder/PetInventoryTest.java +++ b/src/test/java/io/zipcoder/PetInventoryTest.java @@ -28,11 +28,10 @@ public void addPetTest(){ Dog myDog = new Dog("Fighter"); ArrayListarrayListOfDogsTest = new ArrayList(); arrayListOfDogsTest.add(myDog); - petListTest.pets.put("Dog",arrayListOfDogsTest); - petListTest.addPet("Dog","Sparky"); - Pet expected = myDog; - Pet actual = petListTest.pets.get("Dog").get(0); - + petListTest.pets.put("dog", arrayListOfDogsTest); + petListTest.addPet("dog","Sparky"); + int expected = 2; + int actual = petListTest.pets.get("dog").size(); Assert.assertEquals(expected,actual); } @@ -43,16 +42,14 @@ public void removePetTest(){ ArrayListcatsTest = new ArrayList(); catsTest.add(myCat); catsTest.add(anotherCat); - petListTest.pets.put("Cat",catsTest); - - petListTest.removePet("kitty"); - - int expected =1; - int actual = petListTest.pets.get("Cat").size(); - + petListTest.pets.put("cat",catsTest); + petListTest.removePet("Kitty"); + int expected = 1; + int actual = petListTest.pets.get("cat").size(); + System.out.println(petListTest.listPets()); Assert.assertEquals(expected,actual); - } + @Test public void listPetsTest(){ Cat myCat = new Cat("Zina"); @@ -62,11 +59,12 @@ public void listPetsTest(){ catsTest.add(myCat); otterTest.add(myOtter); - petListTest.pets.put("Cat",catsTest); - petListTest.pets.put("Kitty",otterTest); + petListTest.addPet("cat", "Zina"); + petListTest.addPet("otter", "Kitty"); + + String expected = "Zina==>Meow!\n"+ + "Kitty==>Muahaha\n"; - String expected = "Zina==>Meow\n"+ - "Kitty==>Muahaha"; String actual = petListTest.listPets(); Assert.assertEquals(expected,actual); diff --git a/src/test/java/io/zipcoder/pets/CatTest.java b/src/test/java/io/zipcoder/pets/CatTest.java index 94428ee..e22d8ef 100644 --- a/src/test/java/io/zipcoder/pets/CatTest.java +++ b/src/test/java/io/zipcoder/pets/CatTest.java @@ -22,8 +22,8 @@ public void speakTest(){ @Test public void setCatNameTest(){ String expected = "Gabby"; - aCatTest.setCatName("Gabby"); - String actual = aCatTest.getCatName(); + 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 index 7ad9ed1..9cabcaf 100644 --- a/src/test/java/io/zipcoder/pets/DogTest.java +++ b/src/test/java/io/zipcoder/pets/DogTest.java @@ -21,9 +21,9 @@ public void speakTest(){ } @Test public void setDogNameTest(){ - aDogTest.setDogName("aaaa"); + aDogTest.setName("aaaa"); String expected = "aaaa"; - String actual = aDogTest.getDogName(); + 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 index 0420aac..386bbc3 100644 --- a/src/test/java/io/zipcoder/pets/OtterTest.java +++ b/src/test/java/io/zipcoder/pets/OtterTest.java @@ -20,9 +20,9 @@ public void speakTest(){ } @Test public void setOtterNameTest(){ - anOtter.setOtterName("Hero"); + anOtter.setName("Hero"); String expected = "Hero"; - String actual = anOtter.getOtterName(); + String actual = anOtter.getName(); Assert.assertEquals(expected,actual); } From 3752e6690e06c3bfb9ab3c1aeace9cfc147e19b4 Mon Sep 17 00:00:00 2001 From: Vincent Gasbarro Date: Tue, 27 Feb 2018 14:15:14 -0500 Subject: [PATCH 4/6] app runs --- src/main/java/io/zipcoder/Application.java | 51 ++++--- src/main/java/io/zipcoder/PetInventory.java | 49 ++++--- src/main/java/io/zipcoder/pets/Cat.java | 2 - src/main/java/io/zipcoder/pets/Dog.java | 4 +- src/main/java/io/zipcoder/pets/Pet.java | 2 + .../java/io/zipcoder/PetInventoryTest.java | 138 ++++++++---------- src/test/java/io/zipcoder/pets/OtterTest.java | 2 +- 7 files changed, 133 insertions(+), 115 deletions(-) diff --git a/src/main/java/io/zipcoder/Application.java b/src/main/java/io/zipcoder/Application.java index d601b04..c1e2566 100644 --- a/src/main/java/io/zipcoder/Application.java +++ b/src/main/java/io/zipcoder/Application.java @@ -5,44 +5,61 @@ public class Application { - Scanner scanner; + Scanner userInput = new Scanner(System.in); int numberOfPets; String petType; String petName; - public int getNumberOfPets() { - return 0; - } - - public void setNumberOfPets(int numberOfPets) { + public static void main(String[] args) { + Application app = new Application(); + app.setNumberOfPets(); + app.buildPetInventory(); } - public String getPetType() { - return null; - } - public void setPetType(String somePetType) { + 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 String getPetName() { - return null; - } - public void setPetName(String aPetName) { + 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); - public static void main(String[] args) { + 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 index cc6722e..dc387fb 100644 --- a/src/main/java/io/zipcoder/PetInventory.java +++ b/src/main/java/io/zipcoder/PetInventory.java @@ -5,6 +5,7 @@ import io.zipcoder.pets.Otter; import io.zipcoder.pets.Pet; +import java.lang.reflect.Array; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; @@ -34,32 +35,32 @@ public void addPet(String petType, String petName) { switch (petTypeInt) { case 1: Cat newCat = new Cat(petName); - if (pets.containsKey("cat")) { - pets.get("cat").add(newCat); + if (pets.containsKey(petType)) { + pets.get(petType).add(newCat); } else { ArrayList newCatList = new ArrayList(); newCatList.add(newCat); - pets.put("cat", newCatList); + pets.put(petType, newCatList); } break; case 2: Dog newDog = new Dog(petName); - if (pets.containsKey("dog")) { - pets.get("dog").add(newDog); + if (pets.containsKey(petType)) { + pets.get(petType).add(newDog); } else { ArrayList newDogList = new ArrayList(); newDogList.add(newDog); - pets.put("dog", newDogList); + pets.put(petType, newDogList); } break; case 3: Otter newOtter = new Otter(petName); - if (pets.containsKey("otter")) { - pets.get("otter").add(newOtter); + if (pets.containsKey(petType)) { + pets.get(petType).add(newOtter); } else { ArrayList newOtterList = new ArrayList(); newOtterList.add(newOtter); - pets.put("otter", newOtterList); + pets.put(petType, newOtterList); } break; } @@ -69,33 +70,45 @@ public void addPet(String petType, String petName) { 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); + for (int i = 0; i < pets.entrySet().size(); i++) { + if (entry.getValue().get(i).getName().equalsIgnoreCase(petName)) { + entry.getValue().remove(i); + } } } } - public String listPets() { + + 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"; + list += pets.get(i).get(j).getName() + "==>" + pets.get(i).get(j).speak() + "\n"; } return list; } - public String getPetType(String petName) { - return null; - } - public ArrayList listOfPetsByName() { return null; } + public ArrayList getPetsByType(String petType) { + return pets.get(petType); + } } diff --git a/src/main/java/io/zipcoder/pets/Cat.java b/src/main/java/io/zipcoder/pets/Cat.java index 622e696..523bb52 100644 --- a/src/main/java/io/zipcoder/pets/Cat.java +++ b/src/main/java/io/zipcoder/pets/Cat.java @@ -16,7 +16,6 @@ public String speak() { public void setName(String aCatName) { this.catName=aCatName; - } public String getName() { @@ -24,5 +23,4 @@ public String getName() { return this.catName; } - } diff --git a/src/main/java/io/zipcoder/pets/Dog.java b/src/main/java/io/zipcoder/pets/Dog.java index 770c715..8205968 100644 --- a/src/main/java/io/zipcoder/pets/Dog.java +++ b/src/main/java/io/zipcoder/pets/Dog.java @@ -3,6 +3,7 @@ public class Dog extends Pet { private String dogName; + public Dog(String dogName) { this.dogName = dogName; } @@ -22,7 +23,4 @@ public String getName() { return dogName; } - - - } diff --git a/src/main/java/io/zipcoder/pets/Pet.java b/src/main/java/io/zipcoder/pets/Pet.java index af9c981..b614782 100644 --- a/src/main/java/io/zipcoder/pets/Pet.java +++ b/src/main/java/io/zipcoder/pets/Pet.java @@ -16,6 +16,7 @@ public String getName() { public void setName(String aPetName) { } + public int compareTo(Pet anotherPet){ if (getName().compareTo(anotherPet.getName()) > 0) { return 1; @@ -26,4 +27,5 @@ public int compareTo(Pet anotherPet){ } } + } diff --git a/src/test/java/io/zipcoder/PetInventoryTest.java b/src/test/java/io/zipcoder/PetInventoryTest.java index afea13e..d059c46 100644 --- a/src/test/java/io/zipcoder/PetInventoryTest.java +++ b/src/test/java/io/zipcoder/PetInventoryTest.java @@ -17,110 +17,100 @@ public class PetInventoryTest { PetInventory petListTest; @Before - public void setup(){ + public void setup() { - petListTest= new PetInventory(); + petListTest = new PetInventory(); } @Test - public void addPetTest(){ - Dog myDog = new Dog("Fighter"); - ArrayListarrayListOfDogsTest = new ArrayList(); + public void addPetTest() { + Dog myDog = new Dog( "Fighter"); + ArrayList arrayListOfDogsTest = new ArrayList(); arrayListOfDogsTest.add(myDog); petListTest.pets.put("dog", arrayListOfDogsTest); - petListTest.addPet("dog","Sparky"); + petListTest.addPet("dog", "Sparky"); int expected = 2; int actual = petListTest.pets.get("dog").size(); - Assert.assertEquals(expected,actual); + Assert.assertEquals(expected, actual); } @Test - public void removePetTest(){ - Cat myCat = new Cat("Zina"); - Cat anotherCat = new Cat("Kitty"); - ArrayListcatsTest = new ArrayList(); - catsTest.add(myCat); - catsTest.add(anotherCat); - petListTest.pets.put("cat",catsTest); - petListTest.removePet("Kitty"); - int expected = 1; - int actual = petListTest.pets.get("cat").size(); - System.out.println(petListTest.listPets()); - Assert.assertEquals(expected,actual); + 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 listPetsTest(){ - Cat myCat = new Cat("Zina"); - Otter myOtter = new Otter("Kitty"); - ArrayListcatsTest = new ArrayList(); - ArrayListotterTest = new ArrayList(); - catsTest.add(myCat); - otterTest.add(myOtter); - - petListTest.addPet("cat", "Zina"); - petListTest.addPet("otter", "Kitty"); - - String expected = "Zina==>Meow!\n"+ - "Kitty==>Muahaha\n"; - - String actual = petListTest.listPets(); - - 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 getPetType(){ - Cat myCat = new Cat("Zina"); - Otter myOtter = new Otter("Kitty"); - ArrayListcatsTest = new ArrayList(); - ArrayListotterTest = new ArrayList(); - catsTest.add(myCat); - otterTest.add(myOtter); - petListTest.pets.put("Cat",catsTest); - petListTest.pets.put("Kitty",otterTest); - String expected = "Cat"; - String actual = petListTest.getPetType("Zina"); - - Assert.assertEquals(expected,actual); + @Test + public void listPetsAndSpeak() { + petListTest.addPet("cat", "Zina"); + petListTest.addPet("otter", "Otty"); - } + String expected = "Zina==>Meow!\n" + + "Otty==>Muahaha\n"; - @Test + String actual = petListTest.listPetsAndSpeak(); + Assert.assertEquals(expected, actual); + } - public void listOfPetsByNameTest(){ + @Test + public void getPetType() { + petListTest.addPet("cat", "Zina"); + petListTest.addPet("otter", "Otty"); + petListTest.addPet("cat", "Tissue"); - Dog myDog = new Dog("Fighter"); - ArrayListarrayListOfDogsTest = new ArrayList(); - arrayListOfDogsTest.add(myDog); + String expected = "cat"; + String actual = petListTest.getPetType("Zina"); - Cat myCat = new Cat("Zina"); - Otter myOtter = new Otter("Kitty"); - ArrayListcatsTest = new ArrayList(); - ArrayListotterTest = new ArrayList(); - catsTest.add(myCat); - otterTest.add(myOtter); + Assert.assertEquals(expected, actual); + } - petListTest.pets.put("Cat",catsTest); - petListTest.pets.put("Kitty",otterTest); - petListTest.pets.put("Dog",arrayListOfDogsTest); + @Test + public void listOfPetsByNameTest() { + Dog myDog = new Dog("Fighter"); + ArrayList arrayListOfDogsTest = new ArrayList(); + arrayListOfDogsTest.add(myDog); + Cat myCat = new Cat("Zina"); + Otter myOtter = new Otter("Otty"); + ArrayList catsTest = new ArrayList(); + ArrayList otterTest = new ArrayList(); + catsTest.add(myCat); + otterTest.add(myOtter); - ArrayList expected = new ArrayList(); - expected.add("Fighter"); - expected.add("Kitty"); - expected.add("Zina"); + petListTest.pets.put("Cat", catsTest); + petListTest.pets.put("Otty", otterTest); + petListTest.pets.put("Dog", arrayListOfDogsTest); - ArrayList actual = petListTest.listOfPetsByName(); + ArrayList expected = new ArrayList(); + expected.add("Fighter"); + expected.add("Otty"); + expected.add("Zina"); - Assert.assertEquals(expected,actual); + ArrayList actual = petListTest.listOfPetsByName(); + Assert.assertEquals(expected, actual); + } - } + @Test + public void listOfPetsByTypeTest() { + } } diff --git a/src/test/java/io/zipcoder/pets/OtterTest.java b/src/test/java/io/zipcoder/pets/OtterTest.java index 386bbc3..1185478 100644 --- a/src/test/java/io/zipcoder/pets/OtterTest.java +++ b/src/test/java/io/zipcoder/pets/OtterTest.java @@ -14,7 +14,7 @@ public void setup(){ @Test public void speakTest(){ - String expected = "Hello"; + String expected = "Muahaha"; String actual = anOtter.speak(); Assert.assertEquals(expected,actual); } From ce44d92b7b79fad5f669bd9adcc5e74856f355c0 Mon Sep 17 00:00:00 2001 From: Vincent Gasbarro Date: Tue, 27 Feb 2018 15:47:58 -0500 Subject: [PATCH 5/6] still working on comparable --- pom.xml | 12 ++++ src/main/java/io/zipcoder/Application.java | 3 - src/main/java/io/zipcoder/PetInventory.java | 38 +++++++--- src/main/java/io/zipcoder/pets/Pet.java | 17 ++++- .../java/io/zipcoder/PetInventoryTest.java | 70 ++++++++++++------- 5 files changed, 100 insertions(+), 40 deletions(-) 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 c1e2566..58e5ed1 100644 --- a/src/main/java/io/zipcoder/Application.java +++ b/src/main/java/io/zipcoder/Application.java @@ -10,15 +10,12 @@ public class Application { 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)"); diff --git a/src/main/java/io/zipcoder/PetInventory.java b/src/main/java/io/zipcoder/PetInventory.java index dc387fb..2b971f9 100644 --- a/src/main/java/io/zipcoder/PetInventory.java +++ b/src/main/java/io/zipcoder/PetInventory.java @@ -6,10 +6,7 @@ import io.zipcoder.pets.Pet; import java.lang.reflect.Array; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Map; -import java.util.Set; +import java.util.*; public class PetInventory { @@ -17,7 +14,6 @@ public class PetInventory { public PetInventory() { this.pets = new HashMap>(); - } @@ -102,13 +98,37 @@ public String listPetsAndSpeak() { } - public ArrayList listOfPetsByName() { - return null; - } - 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 void sortPetsByName() { + Collections.sort(getAllPets()); + } + + public void sortPetsByType() { + Collections.sort(getAllPets()); + } + + + + } diff --git a/src/main/java/io/zipcoder/pets/Pet.java b/src/main/java/io/zipcoder/pets/Pet.java index b614782..d834edf 100644 --- a/src/main/java/io/zipcoder/pets/Pet.java +++ b/src/main/java/io/zipcoder/pets/Pet.java @@ -1,6 +1,6 @@ package io.zipcoder.pets; -abstract public class Pet implements PetBehavior,Comparable{ +abstract public class Pet implements PetBehavior, Comparable{ private String name; @@ -10,21 +10,32 @@ public String speak() { } public String getName() { - return null; + return this.name; } public void setName(String aPetName) { } + @Override public int compareTo(Pet anotherPet){ + +// if(this.getName().compareTo(anotherPet.getName()) == 0) { +// return this.getClass().getSimpleName().compareTo(anotherPet.getClass().getSimpleName()); +// } else { +// return this.getName().compareTo(anotherPet.getName()); +// } + + if (getName().compareTo(anotherPet.getName()) > 0) { return 1; } else if (getName().compareTo(anotherPet.getName()) < 0) { return -1; } else { - return getClass().getSimpleName().compareTo(anotherPet.getClass().getSimpleName()); + return getClass().getSimpleName().compareTo(getClass().getSimpleName()); } + + } diff --git a/src/test/java/io/zipcoder/PetInventoryTest.java b/src/test/java/io/zipcoder/PetInventoryTest.java index d059c46..1a42644 100644 --- a/src/test/java/io/zipcoder/PetInventoryTest.java +++ b/src/test/java/io/zipcoder/PetInventoryTest.java @@ -8,9 +8,8 @@ import org.junit.Before; import org.junit.Test; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Map; +import java.lang.reflect.Array; +import java.util.*; public class PetInventoryTest { @@ -18,9 +17,7 @@ public class PetInventoryTest { @Before public void setup() { - petListTest = new PetInventory(); - } @Test @@ -82,35 +79,58 @@ public void getPetType() { } @Test - public void listOfPetsByNameTest() { - Dog myDog = new Dog("Fighter"); - ArrayList arrayListOfDogsTest = new ArrayList(); - arrayListOfDogsTest.add(myDog); - Cat myCat = new Cat("Zina"); - Otter myOtter = new Otter("Otty"); - ArrayList catsTest = new ArrayList(); - ArrayList otterTest = new ArrayList(); - catsTest.add(myCat); - otterTest.add(myOtter); + 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); + } - petListTest.pets.put("Cat", catsTest); - petListTest.pets.put("Otty", otterTest); - petListTest.pets.put("Dog", arrayListOfDogsTest); + @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); + } - ArrayList expected = new ArrayList(); - expected.add("Fighter"); - expected.add("Otty"); - expected.add("Zina"); + @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 - ArrayList actual = petListTest.listOfPetsByName(); + petListTest.sortPetsByName(); + String expected = "Meow!"; + String actual = petListTest.getAllPets().get(2).speak(); Assert.assertEquals(expected, actual); } @Test - public void listOfPetsByTypeTest() { - + public void sortPetsByTypeTest() { + 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) + petListTest.sortPetsByType(); + String expected = "Clubber"; + String actual = petListTest.getAllPets().get(3).getName(); + Assert.assertEquals(expected, actual); } } From 702b13632f0ec49d65c797324bf368cfe9ba3570 Mon Sep 17 00:00:00 2001 From: Vincent Gasbarro Date: Tue, 27 Feb 2018 19:48:33 -0500 Subject: [PATCH 6/6] finished comparing --- src/main/java/io/zipcoder/PetInventory.java | 26 ++++++++---- src/main/java/io/zipcoder/pets/Cat.java | 10 +---- src/main/java/io/zipcoder/pets/Compare.java | 23 +++++++++++ src/main/java/io/zipcoder/pets/Dog.java | 14 ++----- src/main/java/io/zipcoder/pets/Otter.java | 11 +---- src/main/java/io/zipcoder/pets/Pet.java | 40 ++++++++++--------- .../java/io/zipcoder/PetInventoryTest.java | 31 ++++++++------ 7 files changed, 86 insertions(+), 69 deletions(-) create mode 100644 src/main/java/io/zipcoder/pets/Compare.java diff --git a/src/main/java/io/zipcoder/PetInventory.java b/src/main/java/io/zipcoder/PetInventory.java index 2b971f9..43c7de3 100644 --- a/src/main/java/io/zipcoder/PetInventory.java +++ b/src/main/java/io/zipcoder/PetInventory.java @@ -1,9 +1,6 @@ package io.zipcoder; -import io.zipcoder.pets.Cat; -import io.zipcoder.pets.Dog; -import io.zipcoder.pets.Otter; -import io.zipcoder.pets.Pet; +import io.zipcoder.pets.*; import java.lang.reflect.Array; import java.util.*; @@ -119,12 +116,25 @@ public ArrayList getAllPets() { return allPets; } - public void sortPetsByName() { - Collections.sort(getAllPets()); + public ArrayList sortedPetsByName() { + + ArrayList allPets = getAllPets(); + + Collections.sort(allPets); + + return allPets; } - public void sortPetsByType() { - Collections.sort(getAllPets()); + 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 index 523bb52..2d7c732 100644 --- a/src/main/java/io/zipcoder/pets/Cat.java +++ b/src/main/java/io/zipcoder/pets/Cat.java @@ -2,10 +2,9 @@ public class Cat extends Pet { - private String catName; public Cat(String catName) { - this.catName = catName; + super(catName); } @@ -14,13 +13,6 @@ public String speak() { return "Meow!"; } - public void setName(String aCatName) { - this.catName=aCatName; - } - - public String getName() { - return this.catName; - } } 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 index 8205968..7a99710 100644 --- a/src/main/java/io/zipcoder/pets/Dog.java +++ b/src/main/java/io/zipcoder/pets/Dog.java @@ -1,26 +1,18 @@ package io.zipcoder.pets; public class Dog extends Pet { - private String dogName; public Dog(String dogName) { - this.dogName = dogName; + super(dogName); } + public String speak() { - public String speak(){ - return "Woof!"; - } + return "Woof!"; - public void setName(String aDogName) { - this.dogName = aDogName; } - public String getName() { - - return dogName; - } } diff --git a/src/main/java/io/zipcoder/pets/Otter.java b/src/main/java/io/zipcoder/pets/Otter.java index 01d87e1..089f96a 100644 --- a/src/main/java/io/zipcoder/pets/Otter.java +++ b/src/main/java/io/zipcoder/pets/Otter.java @@ -2,10 +2,9 @@ public class Otter extends Pet { - private String otterName; public Otter(String otterName) { - this.otterName = otterName; + super(otterName); } @@ -14,13 +13,5 @@ public String speak() { return "Muahaha"; } - public void setName(String anOtterName) { - this.otterName=anOtterName; - } - - public String getName() { - - return this.otterName; - } } diff --git a/src/main/java/io/zipcoder/pets/Pet.java b/src/main/java/io/zipcoder/pets/Pet.java index d834edf..71c88a2 100644 --- a/src/main/java/io/zipcoder/pets/Pet.java +++ b/src/main/java/io/zipcoder/pets/Pet.java @@ -1,9 +1,12 @@ package io.zipcoder.pets; -abstract public class Pet implements PetBehavior, Comparable{ +abstract public class Pet implements PetBehavior, Comparable { private String name; + public Pet(String aName) { + this.name = aName; + } public String speak() { return null; @@ -13,30 +16,29 @@ public String getName() { return this.name; } - public void setName(String aPetName) { - + public void setName(String aName) { + this.name = aName; } @Override - public int compareTo(Pet anotherPet){ - -// if(this.getName().compareTo(anotherPet.getName()) == 0) { -// return this.getClass().getSimpleName().compareTo(anotherPet.getClass().getSimpleName()); -// } else { -// return this.getName().compareTo(anotherPet.getName()); -// } - - - if (getName().compareTo(anotherPet.getName()) > 0) { - return 1; - } else if (getName().compareTo(anotherPet.getName()) < 0) { - return -1; - } else { - return getClass().getSimpleName().compareTo(getClass().getSimpleName()); + 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/test/java/io/zipcoder/PetInventoryTest.java b/src/test/java/io/zipcoder/PetInventoryTest.java index 1a42644..90295b5 100644 --- a/src/test/java/io/zipcoder/PetInventoryTest.java +++ b/src/test/java/io/zipcoder/PetInventoryTest.java @@ -1,9 +1,6 @@ package io.zipcoder; -import io.zipcoder.pets.Cat; -import io.zipcoder.pets.Dog; -import io.zipcoder.pets.Otter; -import io.zipcoder.pets.Pet; +import io.zipcoder.pets.*; import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -109,16 +106,13 @@ public void sortPetsByNameTest() { petListTest.addPet("cat", "Felix"); // from: Zina, Felix(cat), Arnold, Felix (otter), Clubber // to: Arnold, Clubber, Felix (cat), Felix (otter), Zina - - petListTest.sortPetsByName(); - String expected = "Meow!"; - String actual = petListTest.getAllPets().get(2).speak(); + String actual = petListTest.sortedPetsByName().get(2).speak(); Assert.assertEquals(expected, actual); } @Test - public void sortPetsByTypeTest() { + public void sortPetsByTypeTest1() { petListTest.addPet("cat", "Zina"); petListTest.addPet("otter", "Felix"); petListTest.addPet("dog", "Arnold"); @@ -127,10 +121,23 @@ public void sortPetsByTypeTest() { // from: Zina, Felix (cat), Arnold, Felix (otter), Clubber // to: Felix (cat), Zina, Arnold, Clubber, Felix (otter) - petListTest.sortPetsByType(); - String expected = "Clubber"; - String actual = petListTest.getAllPets().get(3).getName(); + 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); } }