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



public class Application {
public static void main(String[] args) {
PetOwner petOwner = new PetOwner();
petOwner.start();
}
}
15 changes: 15 additions & 0 deletions src/main/java/io/zipcoder/Bird.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.zipcoder;

public class Bird extends Pet{
public Bird(){

}
public Bird(String name){
super(name);
}

@Override
public String speak(){
return "Tweet Tweet";
}
}
16 changes: 16 additions & 0 deletions src/main/java/io/zipcoder/Cat.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.zipcoder;

public class Cat extends Pet {
public Cat() {

}

public Cat(String name) {
super(name);
}

@Override
public String speak() {
return "Meow";
}
}
12 changes: 12 additions & 0 deletions src/main/java/io/zipcoder/CompareAnimal.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.zipcoder;

import java.util.Comparator;

public class CompareAnimal implements Comparator<Pet>{

//compare by type
public int compare(Pet o1, Pet o2) {
return o1.getClass().getSimpleName().compareToIgnoreCase(o2.getClass().getSimpleName());
}

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

public class Dog extends Pet{
public Dog(){

}
public Dog(String name){
super(name);
}

//method
@Override
public String speak(){
String sound = "Woof";
return sound;
}

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

public class Pet implements Comparable<Pet>{
private String petName;


//constructors
public Pet(){

}
public Pet (String petName){
this.petName = petName;
}

//methods
public String getPetName(){
return this.petName;
}

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

public String speak(){
String sound = "Generic Animal Sound";
return sound;
}

public int compareTo(Pet o) {
return this.getPetName().compareToIgnoreCase(o.getPetName());
}
}
16 changes: 16 additions & 0 deletions src/main/java/io/zipcoder/PetFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.zipcoder;

public class PetFactory {
public static Pet createPet(String type, String name){
if (type.equalsIgnoreCase("Dog")){
return new Dog(name);
} else if (type.equalsIgnoreCase("Cat")){
return new Cat(name);
}else if (type.equalsIgnoreCase("Bird")){
return new Bird(name);
}else {
System.out.println("Not a pet type available!");
}
return null;
}
}
61 changes: 61 additions & 0 deletions src/main/java/io/zipcoder/PetOwner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package io.zipcoder;

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

public class PetOwner {
CompareAnimal compareAnimal = new CompareAnimal();
Scanner userInput = new Scanner(System.in);
static final ArrayList<Pet> petList = new ArrayList<Pet>();
private Integer numberOfPets;



public Integer askUserForNumberPets(){
System.out.println("Please enter the number of pets you have");
numberOfPets = Integer.parseInt(userInput.nextLine());
return numberOfPets;
}

public String askUserForPetName(){
System.out.println("What is your pet's name?");
String petName = userInput.nextLine();
return petName;
}

public String askUserForPetType(){
System.out.println("What type of pet do you have?");
String petType = userInput.nextLine();
return petType;
}

public void addPetsToList(Integer numberOfPets){
for (int i =0; i < numberOfPets; i++){
String name = askUserForPetName();
String type = askUserForPetType();
petList.add(PetFactory.createPet(type, name));
}
}

public static String printPetList(ArrayList<Pet> petList){
StringBuilder petListToString = new StringBuilder();
for (Pet pet: petList){
petListToString.append(pet.getPetName());
petListToString.append(" the ");
petListToString.append(pet.getClass().getSimpleName());
petListToString.append(" says ");
petListToString.append(pet.speak() + "\n");

}
String actualPetListToString = petListToString.toString();
System.out.println(petListToString.toString());
return actualPetListToString;
}

public void start(){
this.askUserForNumberPets();
this.addPetsToList(numberOfPets);
this.printPetList(petList);
}

}
9 changes: 9 additions & 0 deletions src/test/java/io/zipcoder/ApplicationTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
package io.zipcoder;


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

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

public class ApplicationTest {
}


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

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

public class BirdTest {

@Test
public void testSetBirdName() {
// Given
Bird testBird = new Bird();
String expected = "Tweety";

// When
testBird.setPetName(expected);
String actual = testBird.getPetName();

// Then
Assert.assertEquals(expected, actual);
}

@Test
public void testConstructorDogName() {
// Given
String expected = "Tweety";

// When
Bird testBird = new Bird(expected);

// Then
String actual = testBird.getPetName();
Assert.assertEquals(expected, actual);
}

@Test
public void testspeak() {
// Given
String expected = "Tweet Tweet";

// When
Bird testBird = new Bird();

// Then
String actual = testBird.speak();
Assert.assertEquals(expected, actual);
}

}


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

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

public class CatTest {

@Test
public void testSetCatName() {
// Given
Cat testCat = new Cat();
String expected = "Fluffy";

// When
testCat.setPetName(expected);
String actual = testCat.getPetName();

// Then
Assert.assertEquals(expected, actual);
}

@Test
public void testConstructorCatName() {
// Given
String expected = "Fluffy";

// When
Cat testCat = new Cat(expected);

// Then
String actual = testCat.getPetName();
Assert.assertEquals(expected, actual);
}

@Test
public void testspeak() {
// Given
String expected = "Meow";

// When
Cat testCat = new Cat();

// Then
String actual = testCat.speak();
Assert.assertEquals(expected, actual);
}

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

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

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

public class CompareAnimalClassTest {
@Test
public void compareTest(){
//Given
PetOwner petOwner = new PetOwner();
CompareAnimal animal = new CompareAnimal();

//When
ArrayList<Pet> petList = new ArrayList<Pet>();
Cat catTest = new Cat("Maus");
Dog dogTest = new Dog("Hemi");
Bird birdTest = new Bird("Tera");
petList.add(catTest);
petList.add(dogTest);
petList.add(birdTest);

Collections.sort(petList, animal);
//Then
String expected = "Tera the Bird\n" + "Maus the Cat\n" + "Hemi the Dog\n";
String actual = PetOwner.printPetList(petList);
Assert.assertEquals(expected, actual);
}
}
47 changes: 47 additions & 0 deletions src/test/java/io/zipcoder/DogTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package io.zipcoder;

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

public class DogTest {
@Test
public void testSetDogName() {
// Given
Dog testDog = new Dog();
String expected = "Spot";

// When
testDog.setPetName(expected);
String actual = testDog.getPetName();

// Then
Assert.assertEquals(expected, actual);
}

@Test
public void testConstructorDogName() {
// Given
String expected = "Spot";

// When
Dog testDog = new Dog(expected);

// Then
String actual = testDog.getPetName();
Assert.assertEquals(expected, actual);
}

@Test
public void testspeak() {
// Given
String expected = "Woof";

// When
Dog testDog = new Dog();

// Then
String actual = testDog.speak();
Assert.assertEquals(expected, actual);
}

}
Loading