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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 0 additions & 5 deletions src/main/java/io/zipcoder/Application.java

This file was deleted.

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


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

public class Application {
public ArrayList<Pet> pets = new ArrayList<Pet>();
int numPet;

public String showPetList(ArrayList<Pet> 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");
}
}
}



12 changes: 12 additions & 0 deletions src/main/java/io/zipcoder/pets/Cat.java
Original file line number Diff line number Diff line change
@@ -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!";
}
}
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){
super(name);
}

@Override
public String speak() {
return "Woof!";
}

}
12 changes: 12 additions & 0 deletions src/main/java/io/zipcoder/pets/Fish.java
Original file line number Diff line number Diff line change
@@ -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";
}
}
31 changes: 31 additions & 0 deletions src/main/java/io/zipcoder/pets/Pet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package io.zipcoder.pets;

public abstract class Pet implements Comparable<Pet>{
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;
}
}
14 changes: 14 additions & 0 deletions src/main/java/io/zipcoder/pets/PetComparator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.zipcoder.pets;

import java.util.Comparator;

public class PetComparator implements Comparator<Pet> {
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;
}
}

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

This file was deleted.

40 changes: 40 additions & 0 deletions src/test/java/io/zipcoder/pets/ApplicationTest.java
Original file line number Diff line number Diff line change
@@ -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<Pet> petTest = new ArrayList<Pet>();
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);
}
}

24 changes: 24 additions & 0 deletions src/test/java/io/zipcoder/pets/CatTest.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
26 changes: 26 additions & 0 deletions src/test/java/io/zipcoder/pets/DogTest.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
24 changes: 24 additions & 0 deletions src/test/java/io/zipcoder/pets/FishTest.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
34 changes: 34 additions & 0 deletions src/test/java/io/zipcoder/pets/PetComparatorTest.java
Original file line number Diff line number Diff line change
@@ -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<Pet> petTest = new ArrayList<Pet>();
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);
}
}
30 changes: 30 additions & 0 deletions src/test/java/io/zipcoder/pets/PetTest.java
Original file line number Diff line number Diff line change
@@ -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<Pet> petTest = new ArrayList<Pet>();
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);
}
}