Skip to content
14 changes: 14 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,20 @@
<artifactId>Interfaces</artifactId>
<version>1.0-SNAPSHOT</version>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>(whatever version is current)</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>junit</groupId>
Expand Down
12 changes: 11 additions & 1 deletion src/main/java/io/zipcoder/Application.java
Original file line number Diff line number Diff line change
@@ -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();
}
}


14 changes: 14 additions & 0 deletions src/main/java/io/zipcoder/Pets/Capybara.java
Original file line number Diff line number Diff line change
@@ -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";
}

}
13 changes: 13 additions & 0 deletions src/main/java/io/zipcoder/Pets/Cat.java
Original file line number Diff line number Diff line change
@@ -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";
}
}
21 changes: 21 additions & 0 deletions src/main/java/io/zipcoder/Pets/Console.java
Original file line number Diff line number Diff line change
@@ -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);
}

}
14 changes: 14 additions & 0 deletions src/main/java/io/zipcoder/Pets/Dog.java
Original file line number Diff line number Diff line change
@@ -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";
}

}
41 changes: 41 additions & 0 deletions src/main/java/io/zipcoder/Pets/Pet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package io.zipcoder.Pets;

import java.util.Comparator;

public abstract class Pet implements Comparable<Pet>{

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());
}
}
7 changes: 7 additions & 0 deletions src/main/java/io/zipcoder/Pets/PetTypes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.zipcoder.Pets;

public enum PetTypes {
Cat,
Dog,
Capybara
}
81 changes: 81 additions & 0 deletions src/main/java/io/zipcoder/Questionairre.java
Original file line number Diff line number Diff line change
@@ -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<Pet> petArrayList;

public Questionairre(){
petArrayList = new ArrayList<>();
}

public ArrayList<Pet> 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;
}
}
}
13 changes: 13 additions & 0 deletions src/main/java/io/zipcoder/SortByPetType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.zipcoder;

import io.zipcoder.Pets.Pet;

import java.util.Comparator;

public class SortByPetType implements Comparator<Pet>{

public int compare(Pet o1, Pet o2) {
return o1.getClass().getSimpleName().compareTo(o2.getClass().getSimpleName());
}

}
5 changes: 0 additions & 5 deletions src/test/java/io/zipcoder/ApplicationTest.java

This file was deleted.

72 changes: 72 additions & 0 deletions src/test/java/io/zipcoder/QuestionairreTest.java
Original file line number Diff line number Diff line change
@@ -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<Pet> 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<Pet> expected = new ArrayList<>();
expected.add(captain);
expected.add(doug);
expected.add(testCat);
ArrayList<Pet> 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<Pet> expected = new ArrayList<>();
expected.add(captain);
expected.add(testCat);
expected.add(doug);
form.petListTypeSort();
Assert.assertEquals(expected, form.getPetArrayList());
}

}
36 changes: 36 additions & 0 deletions src/test/java/io/zipcoder/pets/CapybaraTest.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
Loading