diff --git a/README.md b/README.md index 122fdcc..c2f0ab0 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ Create a program that asks the user how many pets they have. Once you know how m Create a Pet class, and a subclass for each type of pet that you want your program to support. Your classes should follow the following requirements: - You must support at least three types of pets. -- Dog must be one of the types you support. +- io.zipcoder.pets.Dog must be one of the types you support. - Cat must be one of the types you support. - The Pet class must have a `speak` method that each subclass overrides. - The Pet class must have a `name` field with setters and getters. 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..eb71fe9 --- /dev/null +++ b/src/main/java/io/zipcoder/pets/Application.java @@ -0,0 +1,54 @@ +package io.zipcoder.pets; + + +import java.util.ArrayList; +import java.util.Scanner; + +public class Application { + public ArrayList pets = new ArrayList(); + int numPet; + + public String showPetList(ArrayList pets) { + StringBuilder petList = new StringBuilder(); + for (Pet pet : pets) { + petList.append(pet.getName() + " : " + pet.speak() + "\n"); + } + return petList.toString(); + } + + public int listPets() { + Scanner petScanner = new Scanner(System.in); + System.out.println("How many pets do you have?"); + int numPet = petScanner.nextInt(); + return numPet; + } + + public void askForPetInfo() { + Scanner petScanner = new Scanner(System.in); + for (int i = 0; i < numPet; i++) { + System.out.println("What kind of pet/pets are they?"); + String typeOfPet = petScanner.nextLine(); + + System.out.println("What is the name of that pet?"); + String petName = petScanner.nextLine(); + } + } + + public void addPet(String typeOfPet, String petName) { + Pet p; + if (typeOfPet.equalsIgnoreCase("dog")) { + pets.add(new Dog(petName)); + } else if (typeOfPet.equalsIgnoreCase("cat")) { + p = new Cat(petName); + pets.add(p); + } else if (typeOfPet.equalsIgnoreCase("fish")) { + p = new Fish(petName); + pets.add(p); + } else { + System.out.println("We do not condone that type of pet"); + } + } +} + + + 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..6932d20 --- /dev/null +++ b/src/main/java/io/zipcoder/pets/Cat.java @@ -0,0 +1,12 @@ +package io.zipcoder.pets; + +public class Cat extends Pet { + public Cat(String name){ + super(name); + } + + @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..a2bc436 --- /dev/null +++ b/src/main/java/io/zipcoder/pets/Dog.java @@ -0,0 +1,14 @@ +package io.zipcoder.pets; + +public class Dog extends Pet { + + public Dog(String name){ + super(name); + } + + @Override + public String speak() { + return "Woof!"; + } + +} diff --git a/src/main/java/io/zipcoder/pets/Fish.java b/src/main/java/io/zipcoder/pets/Fish.java new file mode 100644 index 0000000..a18be53 --- /dev/null +++ b/src/main/java/io/zipcoder/pets/Fish.java @@ -0,0 +1,12 @@ +package io.zipcoder.pets; + +public class Fish extends Pet { + public Fish(String name){ + super(name); + } + + @Override + public String speak() { + return "Blub blub"; + } +} 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..62be7ee --- /dev/null +++ b/src/main/java/io/zipcoder/pets/Pet.java @@ -0,0 +1,31 @@ +package io.zipcoder.pets; + +public abstract class Pet implements Comparable{ + public String name; + + public Pet(String name){ + this.name = name; + } + + public abstract String speak(); + + public String getName(){ + return name; + } + public void setName(String name){ + this.name = name; + } + @Override + public String toString(){ + StringBuilder builder = new StringBuilder(); + builder.append(this.getClass().getSimpleName() + " : " + this.getName()); + return builder.toString(); + } + public int compareTo(Pet p) { + int compareName = this.getName().compareTo(p.getName()); + if (compareName == 0){ + return this.getClass().getSimpleName().compareTo(p.getClass().getSimpleName()); + } + return compareName; + } +} diff --git a/src/main/java/io/zipcoder/pets/PetComparator.java b/src/main/java/io/zipcoder/pets/PetComparator.java new file mode 100644 index 0000000..b9fe860 --- /dev/null +++ b/src/main/java/io/zipcoder/pets/PetComparator.java @@ -0,0 +1,14 @@ +package io.zipcoder.pets; + +import java.util.Comparator; + +public class PetComparator implements Comparator { + public int compare(Pet pet1, Pet pet2) { + int compareName = pet1.getClass().getSimpleName().compareTo(pet2.getClass().getSimpleName()); + if (compareName == 0) { + return pet1.getName().compareTo(pet2.getName()); + } + return compareName; + } +} + 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..ca9fabf --- /dev/null +++ b/src/test/java/io/zipcoder/pets/ApplicationTest.java @@ -0,0 +1,40 @@ +package io.zipcoder.pets; + +import org.junit.Test; + +import org.junit.Assert; + +import java.util.ArrayList; + +public class ApplicationTest { + + @Test + public void showPetTest(){ + Application test = new Application(); + ArrayList petTest = new ArrayList(); + Cat cat1 = new Cat("Apple"); + Dog dog1 = new Dog("Bob"); + Fish fish1 = new Fish("Cathy"); + petTest.add(cat1); + petTest.add(dog1); + petTest.add(fish1); + + String expected = "Apple : Meow!\n" + + "Bob : Woof!\n" + + "Cathy : Blub blub\n"; + String actual = test.showPetList(petTest); + + Assert.assertEquals(expected, actual); + } + @Test + public void addPetTest(){ + Application test = new Application(); + + test.addPet("Dog", "Georgie"); + test.addPet("Cat", "Tracy"); + String expected = "[Dog : Georgie, Cat : Tracy]"; + String actual = test.pets.toString(); + 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..3eebd48 --- /dev/null +++ b/src/test/java/io/zipcoder/pets/CatTest.java @@ -0,0 +1,24 @@ +package io.zipcoder.pets; + +import org.junit.Assert; +import org.junit.Test; + +public class CatTest { + + @Test + public void catSpeakTest(){ + Cat cat = new Cat("Stan"); + String sound = "Meow!"; + String expected = sound; + String actual = cat.speak(); + Assert.assertEquals(expected, actual); + } + @Test + public void catNameTest(){ + Cat cat = new Cat("Stan"); + String name = "Stan"; + String expected = name; + String actual = cat.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..d152917 --- /dev/null +++ b/src/test/java/io/zipcoder/pets/DogTest.java @@ -0,0 +1,26 @@ +package io.zipcoder.pets; + + +import org.junit.Assert; +import org.junit.Test; + +public class DogTest { + + @Test + public void dogSpeakTest(){ + Dog dog = new Dog("Roger"); + String sound = "Woof!"; + String expected = sound; + String actual = dog.speak(); + Assert.assertEquals(expected, actual); + + } + @Test + public void dogNameTest(){ + Dog dog = new Dog("Roger"); + String name = "Roger"; + String expected = name; + String actual = dog.getName(); + Assert.assertEquals(expected, actual); + } +} diff --git a/src/test/java/io/zipcoder/pets/FishTest.java b/src/test/java/io/zipcoder/pets/FishTest.java new file mode 100644 index 0000000..09200a6 --- /dev/null +++ b/src/test/java/io/zipcoder/pets/FishTest.java @@ -0,0 +1,24 @@ +package io.zipcoder.pets; + +import org.junit.Assert; +import org.junit.Test; + +public class FishTest { + + @Test + public void fishSpeakTest(){ + Fish fish = new Fish("Malcolm"); + String sound = "Blub blub"; + String expected = sound; + String actual = fish.speak(); + Assert.assertEquals(expected, actual); + } + @Test + public void fishNameTest(){ + Fish fish = new Fish("Malcolm"); + String name = "Malcolm"; + String expected = name; + String actual = fish.getName(); + Assert.assertEquals(expected, actual); + } +} diff --git a/src/test/java/io/zipcoder/pets/PetComparatorTest.java b/src/test/java/io/zipcoder/pets/PetComparatorTest.java new file mode 100644 index 0000000..2e0a25a --- /dev/null +++ b/src/test/java/io/zipcoder/pets/PetComparatorTest.java @@ -0,0 +1,34 @@ +package io.zipcoder.pets; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Collections; + +public class PetComparatorTest { + + @Test + public void petComparatorTest(){ + Application test = new Application(); + PetComparator petComparator = new PetComparator(); + + ArrayList petTest = new ArrayList(); + Cat cat1 = new Cat("Apple"); + Dog dog2 = new Dog("Apple"); + Dog dog1 = new Dog("Bob"); + Fish fish1 = new Fish("Cathy"); + petTest.add(cat1); + petTest.add(dog2); + petTest.add(dog1); + petTest.add(fish1); + + Collections.sort(petTest, petComparator ); + String expected = "Apple : Meow!\n" + + "Apple : Woof!\n" + + "Bob : Woof!\n" + + "Cathy : Blub blub\n"; + String actual = test.showPetList(petTest); + Assert.assertEquals(expected, actual); + } +} diff --git a/src/test/java/io/zipcoder/pets/PetTest.java b/src/test/java/io/zipcoder/pets/PetTest.java new file mode 100644 index 0000000..50551c4 --- /dev/null +++ b/src/test/java/io/zipcoder/pets/PetTest.java @@ -0,0 +1,30 @@ +package io.zipcoder.pets; + +import org.junit.Assert; +import org.junit.Test; +import java.util.ArrayList; +import java.util.Collections; + +public class PetTest { + + @Test + public void sortPetTest(){ + Application test = new Application(); + ArrayList petTest = new ArrayList(); + Cat cat1 = new Cat("Apple"); + Dog dog2 = new Dog("Apple"); + Dog dog1 = new Dog("Bob"); + Fish fish1 = new Fish("Cathy"); + petTest.add(cat1); + petTest.add(dog2); + petTest.add(dog1); + petTest.add(fish1); + Collections.sort(petTest); + String expected = "Apple : Meow!\n" + + "Apple : Woof!\n" + + "Bob : Woof!\n" + + "Cathy : Blub blub\n"; + String actual = test.showPetList(petTest); + Assert.assertEquals(expected, actual); + } +}