diff --git a/src/main/java/io/zipcoder/Application.java b/src/main/java/io/zipcoder/Application.java index 3a257cb..960ea66 100644 --- a/src/main/java/io/zipcoder/Application.java +++ b/src/main/java/io/zipcoder/Application.java @@ -1,5 +1,72 @@ package io.zipcoder; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Scanner; + public class Application { + + private int numberOfPetsOwned; + private Scanner userInput= new Scanner(System.in); + private ArrayList petName = new ArrayList(); + private ArrayList petType = new ArrayList(); + private ArrayList petCollector = new ArrayList<>(); + private HashMap petList = new HashMap(); + + public static void main(String[] args) { + Application app = new Application(); + app.numberOfPets(); + app.getPetCollection(); + app.allPetsSpeak(); + + + } + + public Integer numberOfPets() { + + + System.out.println("Where are my manners... Hello Human!! Its a pleasure to meet your" + + " acquaintance. Would you be so kind as to tell me how many pets do you own? " + + "(please enter an integer greater than 0)."); + int tempCount = userInput.nextInt(); + userInput.nextLine(); + if (tempCount > 0) { + numberOfPetsOwned = tempCount; + } else { + numberOfPets(); + } + return numberOfPetsOwned; + } + + public void getPetCollection() { + + for (int i = 0; i < numberOfPetsOwned; i ++) { + + String name; + String type; + + System.out.println("What is the name of pet #" + (i+1) + " ?"); + name = userInput.nextLine(); + System.out.println("Please identify the type of pet (Dog, Cat or Snake)" + "that " + name + " is."); + type = userInput.nextLine(); + this.petCollector.add(PetFactory.petCreator(type, name)); + } + + } + public void petListBuilder(ArrayList petType, ArrayList petName) { + for (int i = 0; i < petType.size(); i++) { + petList.put(petType.get(i), petName.get(i)); + } + } + public String getPetFromList (String key) { + return this.petList.get(key); + } + + public void allPetsSpeak() { + for (Pets pet: petCollector) { + System.out.println("Your " + pet.getClass().getSimpleName() + " named " + pet.getName() + " says \"" + pet.speak() + "\""); + } + } + } diff --git a/src/main/java/io/zipcoder/Cat.java b/src/main/java/io/zipcoder/Cat.java new file mode 100644 index 0000000..9d473b7 --- /dev/null +++ b/src/main/java/io/zipcoder/Cat.java @@ -0,0 +1,12 @@ +package io.zipcoder; + +public class Cat extends Pets { + + public Cat(String name) { + super(name); + } + @Override + public String speak() { + return "Hang in there."; + } +} diff --git a/src/main/java/io/zipcoder/Dog.java b/src/main/java/io/zipcoder/Dog.java new file mode 100644 index 0000000..b6b21ea --- /dev/null +++ b/src/main/java/io/zipcoder/Dog.java @@ -0,0 +1,14 @@ +package io.zipcoder; + +public class Dog extends Pets { + + public Dog(String name) { + super(name); + } + + @Override + public String speak() { + return "I'm such a wimp! I'm running from a cat!"; + } + +} diff --git a/src/main/java/io/zipcoder/PetFactory.java b/src/main/java/io/zipcoder/PetFactory.java new file mode 100644 index 0000000..71fc6ef --- /dev/null +++ b/src/main/java/io/zipcoder/PetFactory.java @@ -0,0 +1,14 @@ +package io.zipcoder; + +public class PetFactory { + + public static Pets petCreator(String type, String name) { + if(type.equals("Cat")) + return new Cat(name); + else if (type.equals("Snake")) + return new Snake(name); + else if (type.equals("Dog")) + return new Dog(name); + return null; + } +} diff --git a/src/main/java/io/zipcoder/Pets.java b/src/main/java/io/zipcoder/Pets.java new file mode 100644 index 0000000..79cae3a --- /dev/null +++ b/src/main/java/io/zipcoder/Pets.java @@ -0,0 +1,51 @@ +package io.zipcoder; + +import java.util.Comparator; + +public abstract class Pets implements Comparable , Comparator{ + + protected String name; + + protected Pets(String name) { + this.name = name; + } + + public void setName(String name) { + this.name = name; + } + + public String getName() { + + return name; + } + + public abstract String speak(); + + public int compareTo(Pets pet) { + + int compare = this.getName().compareToIgnoreCase(pet.getName()); + + if(compare != 0) { + return compare; + } else { + + compare = this.getClass().getSimpleName().compareToIgnoreCase(pet.getClass().getSimpleName()); + + return compare; + } + + } + public int compare(Pets pet1, Pets pet2) { + int compare = pet1.getClass().getSimpleName().compareToIgnoreCase((pet2.getClass().getSimpleName())); + + if(compare != 0) { + return compare; + } else { + compare = pet1.getName().compareToIgnoreCase(pet2.getName()); + return compare; + } + + } +} + + diff --git a/src/main/java/io/zipcoder/Snake.java b/src/main/java/io/zipcoder/Snake.java new file mode 100644 index 0000000..2e81e4d --- /dev/null +++ b/src/main/java/io/zipcoder/Snake.java @@ -0,0 +1,14 @@ +package io.zipcoder; + +public class Snake extends Pets { + + public Snake(String name) { + super(name); + } + @Override + public String speak() { + return "I have seen all the dead seasons, and the great trees and the old elephants, and the rocks that were " + + "bare and sharp-pointed ere the moss grew. Art thou still alive, Manling?"; + } + +} diff --git a/src/test/java/io/zipcoder/ApplicationTest.java b/src/test/java/io/zipcoder/ApplicationTest.java index b744df5..286900f 100644 --- a/src/test/java/io/zipcoder/ApplicationTest.java +++ b/src/test/java/io/zipcoder/ApplicationTest.java @@ -1,5 +1,8 @@ package io.zipcoder; +import org.junit.Test; + public class ApplicationTest { -} + + } diff --git a/src/test/java/io/zipcoder/CatTest.java b/src/test/java/io/zipcoder/CatTest.java new file mode 100644 index 0000000..9660a11 --- /dev/null +++ b/src/test/java/io/zipcoder/CatTest.java @@ -0,0 +1,25 @@ +package io.zipcoder; + +import org.junit.Assert; +import org.junit.Test; + +public class CatTest { + + @Test + public void getNamesOfCats() { + Cat cat = new Cat("Tom"); + String expected = "Tom"; + String actual = cat.getName(); + Assert.assertEquals(expected, actual); + } + + @Test + public void catSpeakTest() { + Cat cat = new Cat("Tom"); + String expected = "Hang in there."; + String actual = cat.speak(); + 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..32ae075 --- /dev/null +++ b/src/test/java/io/zipcoder/DogTest.java @@ -0,0 +1,23 @@ +package io.zipcoder; + +import org.junit.Assert; +import org.junit.Test; + +public class DogTest { + + @Test + public void getNamesOfDogs() { + Dog dog = new Dog("Chance"); + String expected = "Chance"; + String actual = dog.getName(); + Assert.assertEquals(expected, actual); + } + + @Test + public void dogSpeakTest() { + Dog dog = new Dog("Chance"); + String expected = "I'm such a wimp! I'm running from a cat!"; + String actual = dog.speak(); + Assert.assertEquals(expected, actual); + } +} diff --git a/src/test/java/io/zipcoder/PetsTest.java b/src/test/java/io/zipcoder/PetsTest.java new file mode 100644 index 0000000..9816aa2 --- /dev/null +++ b/src/test/java/io/zipcoder/PetsTest.java @@ -0,0 +1,64 @@ +package io.zipcoder; + +import org.junit.Assert; +import org.junit.Test; + +import java.lang.reflect.Array; +import java.util.ArrayList; +import java.util.Collections; + +public class PetsTest { + + @Test + public void getName() { + + String expected = "Spot"; + Pets pet = new Dog("Spot"); + String actual = pet.getName(); + Assert.assertEquals(expected, actual); + } + @Test + public void comparableTest() { + ArrayList testing = new ArrayList<>(); + Snake Fage = new Snake("Fage"); + Cat Cheshire = new Cat("Cheshire"); + Dog Gretchen = new Dog("Gretchen"); + Snake Gretchen1 = new Snake("Gretchen"); + testing.add(Fage); + testing.add(Cheshire); + testing.add(Gretchen); + testing.add(Gretchen1); + + Collections.sort(testing); + Pets[] expected = {Cheshire, Fage, Gretchen, Gretchen1}; + Pets[] actual = testing.toArray(new Pets[testing.size()]); + Assert.assertEquals(expected, actual); + + } + @Test + public void comparatorTest() { + ArrayList testing = new ArrayList<>(); + + Snake Fage = new Snake("Fage"); + Cat Cheshire = new Cat("Cheshire"); + Dog Gretchen = new Dog("Gretchen"); + Snake Gretchen1 = new Snake("Gretchen"); + Snake Franky = new Snake("Franky"); + Cat Christian = new Cat("Christian"); + Dog Zim = new Dog("Zim"); + + testing.add(Fage); + testing.add(Cheshire); + testing.add(Gretchen); + testing.add(Gretchen1); + testing.add(Franky); + testing.add(Christian); + testing.add(Zim); + + Pets[] expected = {Cheshire, Christian, Gretchen, Zim, Fage , Franky, Gretchen1}; + Collections.sort(testing, Zim); + + Pets[] actual = testing.toArray((new Pets[testing.size()])); + Assert.assertEquals(expected, actual); + } +} diff --git a/src/test/java/io/zipcoder/SnakeTest.java b/src/test/java/io/zipcoder/SnakeTest.java new file mode 100644 index 0000000..8f0f870 --- /dev/null +++ b/src/test/java/io/zipcoder/SnakeTest.java @@ -0,0 +1,28 @@ +package io.zipcoder; + +import org.junit.Assert; +import org.junit.Test; + +public class SnakeTest { + + @Test + public void getNamesOfSnakes() { + String expected = "Kaa"; + Snake snake = new Snake("Kaa"); + String actual = snake.getName(); + Assert.assertEquals(expected, actual); + } + + @Test + public void snakeSpeakTest() { + Snake snake = new Snake("Kaa"); + String expected = "I have seen all the dead seasons, and the great trees and the old elephants, " + + "and the rocks that were bare and sharp-pointed ere the moss grew. Art thou still alive, Manling?"; + String actual = snake.speak(); + Assert.assertEquals(expected, actual); + } +/* + @Test + public void ge + */ +}