diff --git a/pom.xml b/pom.xml index d73c078..7ba99c0 100644 --- a/pom.xml +++ b/pom.xml @@ -8,6 +8,20 @@ Interfaces 1.0-SNAPSHOT + + + + org.apache.maven.plugins + maven-compiler-plugin + (whatever version is current) + + 1.7 + 1.7 + + + + + junit diff --git a/src/main/java/io/zipcoder/Application.java b/src/main/java/io/zipcoder/Application.java index 3a257cb..36b690e 100644 --- a/src/main/java/io/zipcoder/Application.java +++ b/src/main/java/io/zipcoder/Application.java @@ -1,5 +1,15 @@ package io.zipcoder; - public class Application { + + public static void main(String[] args) { + Questionairre form = new Questionairre(); + form.numOfPetsPrompt(); + form.setPetArrayList(); + form.speakNSay(); + form.petListTypeSort(); + form.petListNameSort(); + } } + + diff --git a/src/main/java/io/zipcoder/Pets/Capybara.java b/src/main/java/io/zipcoder/Pets/Capybara.java new file mode 100644 index 0000000..64a3534 --- /dev/null +++ b/src/main/java/io/zipcoder/Pets/Capybara.java @@ -0,0 +1,14 @@ +package io.zipcoder.Pets; + +public class Capybara extends Pet{ + + public Capybara(String name, int age){ + this.name = name; + this.age = age; + } + + public String speak() { + return "It's me, the Capybara"; + } + +} 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..4c26e42 --- /dev/null +++ b/src/main/java/io/zipcoder/Pets/Cat.java @@ -0,0 +1,13 @@ +package io.zipcoder.Pets; + +public class Cat extends Pet { + + public Cat(String name, int age){ + this.name = name; + this.age = age; + } + + public String speak() { + return "Meow"; + } +} diff --git a/src/main/java/io/zipcoder/Pets/Console.java b/src/main/java/io/zipcoder/Pets/Console.java new file mode 100644 index 0000000..198c382 --- /dev/null +++ b/src/main/java/io/zipcoder/Pets/Console.java @@ -0,0 +1,21 @@ +package io.zipcoder.Pets; + +import java.util.Scanner; + +public class Console { + + static Scanner myScanner= new Scanner(System.in); + + public static String getString(){ + return myScanner.next(); + } + + public static int getInt(){ + return myScanner.nextInt(); + } + + public static void print(String whatever) { + System.out.println(whatever); + } + +} 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..1799ada --- /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, int age){ + this.name = name; + this.age = age; + } + + public String speak() { + return "Woof"; + } + +} 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..b775346 --- /dev/null +++ b/src/main/java/io/zipcoder/Pets/Pet.java @@ -0,0 +1,41 @@ +package io.zipcoder.Pets; + +import java.util.Comparator; + +public abstract class Pet implements Comparable{ + + public String name; + public Integer age; + + public Pet(String name, int age){ + this.name = name; + this.age = age; + } + + public Pet(){ + this.name = ""; + this.age = Integer.MAX_VALUE; + } + + public String getName() { + return name; + } + + public Integer getAge() { + return age; + } + + public void setName(String newName) { + this.name = newName; + } + + public void setAge(int age) { + this.age = age; + } + + public abstract String speak(); + + public int compareTo(Pet otherPet){ + return this.getName().compareTo(otherPet.getName()); + } +} diff --git a/src/main/java/io/zipcoder/Pets/PetTypes.java b/src/main/java/io/zipcoder/Pets/PetTypes.java new file mode 100644 index 0000000..c291699 --- /dev/null +++ b/src/main/java/io/zipcoder/Pets/PetTypes.java @@ -0,0 +1,7 @@ +package io.zipcoder.Pets; + +public enum PetTypes { + Cat, + Dog, + Capybara +} diff --git a/src/main/java/io/zipcoder/Questionairre.java b/src/main/java/io/zipcoder/Questionairre.java new file mode 100644 index 0000000..2d17df1 --- /dev/null +++ b/src/main/java/io/zipcoder/Questionairre.java @@ -0,0 +1,81 @@ +package io.zipcoder; + +import io.zipcoder.Pets.*; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; + +public class Questionairre { + + protected int numOfPets; + protected ArrayList petArrayList; + + public Questionairre(){ + petArrayList = new ArrayList<>(); + } + + public ArrayList getPetArrayList() { + return petArrayList; + } + + public void numOfPetsPrompt() { + Console.print("Hello dear user. How many pets do you have?"); + numOfPets = Console.getInt(); + } + + public void setPetArrayList(){ + Console.print("Thanks! What kind of pets do you have? Please specify using only the following:"); + for (int i = 0; i < numOfPets; i++) { + Console.print(Arrays.toString(PetTypes.values())); + String usersKindOfPets = Console.getString(); + Console.print("What's their name?"); + String name = Console.getString(); + Console.print("How old is this pet?"); + int age = Console.getInt(); + petTypeSwitch(usersKindOfPets, name, age); + } + } + + public String speakNSay() { + String petList = ""; + for (Pet currentPet:petArrayList) { + petList += ("The " + currentPet.getClass().getSimpleName() + " named " + currentPet.getName() + + " says " + currentPet.speak() + ". "); + } + System.out.println(petList); + return petList; + } + + public void petListTypeSort() { + Collections.sort(petArrayList, new SortByPetType()); + for (Pet pet:petArrayList) { + System.out.println(pet.getName()); + } + } + + public void petListNameSort() { + Collections.sort(petArrayList); + for (Pet pet:petArrayList){ + System.out.println(pet.getName()); + } + } + + public void petTypeSwitch(String usersKindOfPets, String name, int age) { + Pet tempPet; + switch (usersKindOfPets.toLowerCase()) { + case "cat": + tempPet = new Cat(name, age); + petArrayList.add(tempPet); + break; + case "dog": + tempPet = new Dog(name, age); + petArrayList.add(tempPet); + break; + case "capybara": + tempPet = new Capybara(name, age); + petArrayList.add(tempPet); + break; + } + } +} diff --git a/src/main/java/io/zipcoder/SortByPetType.java b/src/main/java/io/zipcoder/SortByPetType.java new file mode 100644 index 0000000..c3eb961 --- /dev/null +++ b/src/main/java/io/zipcoder/SortByPetType.java @@ -0,0 +1,13 @@ +package io.zipcoder; + +import io.zipcoder.Pets.Pet; + +import java.util.Comparator; + +public class SortByPetType implements Comparator{ + + public int compare(Pet o1, Pet o2) { + return o1.getClass().getSimpleName().compareTo(o2.getClass().getSimpleName()); + } + +} 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/QuestionairreTest.java b/src/test/java/io/zipcoder/QuestionairreTest.java new file mode 100644 index 0000000..a0f2a74 --- /dev/null +++ b/src/test/java/io/zipcoder/QuestionairreTest.java @@ -0,0 +1,72 @@ +package io.zipcoder; + +import io.zipcoder.Pets.Capybara; +import io.zipcoder.Pets.Cat; +import io.zipcoder.Pets.Dog; +import io.zipcoder.Pets.Pet; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Collections; + +public class QuestionairreTest { + Questionairre form; + ArrayList testList; + Cat testCat; + + @Before + public void setup(){ + form = new Questionairre(); + testList = new ArrayList<>(); + testCat = new Cat("testCat", 4); + } + + @Test + public void speakNSayTest(){ + form.petArrayList.add(testCat); + String actual = form.speakNSay(); + String expected = "The Cat named testCat says Meow. "; + Assert.assertEquals(actual, expected); + } + + @Test + public void petTypeSwitchTest(){ + form.petTypeSwitch("dog", "doug", 5); + Pet expected = form.petArrayList.get(0); + Assert.assertEquals(expected.getName(), "doug"); + } + + @Test + public void petListSortByNameTest(){ + Dog doug = new Dog("doug", 3); + Capybara captain = new Capybara("captain", 7); + form.petArrayList.add(testCat); + form.petArrayList.add(doug); + form.petArrayList.add(captain); + ArrayList expected = new ArrayList<>(); + expected.add(captain); + expected.add(doug); + expected.add(testCat); + ArrayList actual = form.getPetArrayList(); + Collections.sort(actual); + Assert.assertEquals(expected, form.getPetArrayList()); + } + + @Test + public void petListSortByTypeTest(){ + Dog doug = new Dog("doug", 3); + Capybara captain = new Capybara("captain", 7); + form.petArrayList.add(testCat); + form.petArrayList.add(doug); + form.petArrayList.add(captain); + ArrayList expected = new ArrayList<>(); + expected.add(captain); + expected.add(testCat); + expected.add(doug); + form.petListTypeSort(); + Assert.assertEquals(expected, form.getPetArrayList()); + } + +} diff --git a/src/test/java/io/zipcoder/pets/CapybaraTest.java b/src/test/java/io/zipcoder/pets/CapybaraTest.java new file mode 100644 index 0000000..9298372 --- /dev/null +++ b/src/test/java/io/zipcoder/pets/CapybaraTest.java @@ -0,0 +1,36 @@ +package io.zipcoder.pets; + +import io.zipcoder.Pets.Capybara; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class CapybaraTest { + + Capybara testCapy; + + @Before + public void setup(){ + testCapy = new Capybara("Frederick", 2); + } + + @Test + public void setCapyNameAndAgeTest (){ + + String expectedName = "Frederick"; + String actualName = testCapy.getName(); + + int expectedAge = 2; + int actualAge = testCapy.getAge(); + + Assert.assertEquals(expectedName, actualName); + Assert.assertEquals(expectedAge, actualAge); + } + + @Test + public void speakTest(){ + String expected = "It's me, the Capybara"; + String actual = testCapy.speak(); + 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..932f2a6 --- /dev/null +++ b/src/test/java/io/zipcoder/pets/CatTest.java @@ -0,0 +1,36 @@ +package io.zipcoder.pets; + +import io.zipcoder.Pets.Cat; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class CatTest { + + Cat testCat; + + @Before + public void setup(){ + testCat = new Cat("Bubbles", 2); + } + + + @Test + public void setCatNameAndAgeTest (){ + String expectedName = "Bubbles"; + String actualName = testCat.getName(); + int expectedAge = 2; + int actualAge = testCat.getAge(); + + Assert.assertEquals(expectedName, actualName); + Assert.assertEquals(expectedAge, actualAge); + } + + @Test + public void speakTest(){ + Cat testCat = new Cat("Bubbles", 2); + String expected = "Meow"; + String actual = testCat.speak(); + 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..a413658 --- /dev/null +++ b/src/test/java/io/zipcoder/pets/DogTest.java @@ -0,0 +1,38 @@ +package io.zipcoder.pets; + +import io.zipcoder.Pets.Dog; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class DogTest { + + Dog testDog; + + @Before + public void setup(){ + testDog = new Dog("testDog", 2); + } + + @Test + public void setDogNameAndAgeTest(){ + String expectedName = "testDog"; + String actualName = testDog.getName(); + + int expectedAge = 2; + int actualAge = testDog.getAge(); + + Assert.assertEquals(expectedName, actualName); + Assert.assertEquals(expectedAge, actualAge); + + } + + @Test + public void speakTest(){ + Dog testDog = new Dog("Spike",2); + String expected = "Woof"; + String actual = testDog.speak(); + Assert.assertEquals(expected, actual); + } + +}