Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@
<groupId>io.zipcoder</groupId>
<artifactId>Interfaces</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
Expand Down
5 changes: 0 additions & 5 deletions src/main/java/io/zipcoder/Application.java

This file was deleted.

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


import io.zipcoder.io.zipcoder.pets.Cat;
import io.zipcoder.io.zipcoder.pets.Dog;
import io.zipcoder.io.zipcoder.pets.Pet;
import io.zipcoder.io.zipcoder.pets.Unicorn;

import java.util.ArrayList;
import java.util.Collections;

public class PetUtils {

ArrayList<Pet> pets = new ArrayList<Pet>();
private int numberOfPets;

public ArrayList<Pet> getPets() {

return pets;
}

public String printList() {

return pets.toString();
}


public int getNumberOfPets() {


return pets.size();

}

public void addPet(String petName, String petType) {

Pet pet = null;
if (petType.equalsIgnoreCase("Dog")) {
pet = new Dog();
pet.setPetName(petName);
pets.add(pet);
} else if (petType.equalsIgnoreCase("Cat")) {
pet = new Cat();
pet.setPetName(petName);
pets.add(pet);
} else if (petType.equalsIgnoreCase("Unicorn")) {
pet = new Unicorn();
pet.setPetName(petName);
pets.add(pet);
} else {
pet = null;
}
}

public boolean containsPet(String petName, String petType) {

boolean contains = false;

for (Pet pet : pets) {
if (pet.getPetName().equalsIgnoreCase(petName) && pet.getClass().getSimpleName().equals(petType)) {
contains = true;
break;
}
}
return contains;
}

public void setNumberOfPets(int numberOfPets) {

this.numberOfPets = numberOfPets;
}


public static void main(String[] args) {


}
}

34 changes: 34 additions & 0 deletions src/main/java/io/zipcoder/io/zipcoder/pets/Application.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package io.zipcoder.io.zipcoder.pets;

import io.zipcoder.PetUtils;

import java.util.ArrayList;
import java.util.Scanner;

public class Application {

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
PetUtils pet = new PetUtils();


System.out.println("Hi, how many pets do you have?");
int numOfPets = Integer.parseInt(scanner.nextLine());
for(int i = 0; i < numOfPets; i++) {
String petType;
String petName;
System.out.println("Aww, what kind of pet is # " + (i+1) + "?");
petType = scanner.nextLine();

System.out.println("Cute! Whats your " + petType + "'s name?");
petName = scanner.nextLine();
pet.addPet(petName, petType);
}

System.out.println("So, let me get this right, you have the following animals: ");

System.out.println(pet.printList());

}

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

public class Cat extends Pet {


@Override
public String speak() {

return "meow";
}


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

import java.util.Comparator;

public class Compare implements Comparator<Pet> {

public int compare(Pet o1, Pet o2) {

int comparable = o1.getClass().getSimpleName().compareTo(o2.getClass().getSimpleName());

if(comparable == 0) {
return 0;
}else if(comparable > 0) {
return 1;
}else {
return -1;
}
}
}
14 changes: 14 additions & 0 deletions src/main/java/io/zipcoder/io/zipcoder/pets/Dog.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.zipcoder.io.zipcoder.pets;

public class Dog extends Pet {



@Override
public String speak() {

return "woof";
}


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

import io.zipcoder.PetUtils;

import java.util.Comparator;
import java.util.List;
import java.util.TreeMap;

public abstract class Pet implements Comparable<Pet> {

private String petName;

public abstract String speak();

public String getPetName() {
return petName;
}

public void setPetName(String petName) {
this.petName = petName;
}

@Override
public String toString(){

return "[" + petName + ", " + this.getClass().getSimpleName()+ "]";

}
public int compareTo(Pet o) {

int result = this.getPetName().compareTo(o.getPetName());
int classCompare = this.getClass().getSimpleName().compareTo(o.getClass().getSimpleName());

if(result == 0) {
return classCompare;
}else {
return result;
}
}


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

public class Unicorn extends Pet {



@Override
public String speak() {
return "* sparkles * glitter *";
}
}
5 changes: 0 additions & 5 deletions src/test/java/io/zipcoder/ApplicationTest.java

This file was deleted.

81 changes: 81 additions & 0 deletions src/test/java/io/zipcoder/PetUtilsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package io.zipcoder;

import io.zipcoder.io.zipcoder.pets.Compare;
import io.zipcoder.io.zipcoder.pets.Pet;
import org.junit.Assert;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Collections;

public class PetUtilsTest {

@Test
public void printList() {
//Given
PetUtils utils = new PetUtils();
//When
utils.addPet("Hugo", "Dog");
utils.addPet("Bob", "Cat");
//Then
String expected = ("[[Hugo, Dog], [Bob, Cat]]");
String actual = utils.printList();

Assert.assertEquals(expected, actual);
}

@Test
public void setNumberOfPets() {
//Given
PetUtils petUtils = new PetUtils();
petUtils.addPet("Hugo", "Dog");
petUtils.addPet("Bob", "Cat");
petUtils.addPet("John", "Unicorn");

int expected = 3;
petUtils.setNumberOfPets(expected);
//When
int actual = petUtils.getNumberOfPets();
//Then
Assert.assertEquals(expected, actual);
}

@Test
public void addPet() {
//Given
PetUtils utils = new PetUtils();

utils.addPet("Hugo", "Dog");
String expected = ("[Hugo, Dog]");
String actual = utils.pets.get(0).toString();
Assert.assertEquals(expected, actual);
}

@Test
public void containsPet() {

PetUtils utils = new PetUtils();

utils.addPet("Bob", "Cat");

Assert.assertTrue(utils.containsPet("Bob", "Cat"));
}

@Test
public void compare(){
//Given
Compare compare = new Compare();
PetUtils petUtils = new PetUtils();

petUtils.addPet("Hugo", "Dog");
petUtils.addPet("Bob", "Cat");
petUtils.addPet("John", "Unicorn");

Collections.sort(petUtils.getPets(), compare);
String expected = "[[Bob, Cat], [Hugo, Dog], [John, Unicorn]]";
String actual = petUtils.getPets().toString();

Assert.assertEquals(expected, actual);
}

}
32 changes: 32 additions & 0 deletions src/test/java/io/zipcoder/io/zipcoder/pets/CatTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package io.zipcoder.io.zipcoder.pets;

import org.junit.Assert;
import org.junit.Test;

import static org.junit.Assert.*;

public class CatTest {

@Test
public void speak() {
//Given
Cat cat = new Cat();
//When
String expected = "meow";
String actual = cat.speak();
//Then
Assert.assertEquals(expected,actual);
}

@Test
public void setPetName(){
//Given
Cat cat = new Cat();
String expected = "Hugo";
cat.setPetName(expected);
//When
String actual = cat.getPetName();
//Then
Assert.assertEquals(expected, actual);
}
}
Loading