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


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

public class Application {
}

private int numberOfPets;
protected ArrayList<Pet> pets = new ArrayList<Pet>();
Scanner input = new Scanner(System.in);

public int storePetInformation(){
System.out.println("How many pets do you own?");
numberOfPets = input.nextInt();
for(int i = 0; i<numberOfPets; i++){
pets.add(userInputEachPet());
}
return numberOfPets;
}

public Pet userInputEachPet(){
System.out.println("Type of pet?");
String typeOfPet = input.next();

System.out.println("What is the pet's name?");
String nameOfPet = input.next();
Pet pet = new Pet(nameOfPet);
if(typeOfPet.equalsIgnoreCase("Cat")){
pet = new Cat(nameOfPet);
}
else if (typeOfPet.equalsIgnoreCase("Dog")){
pet = new Dog(nameOfPet);
}
else if(typeOfPet.equalsIgnoreCase("Bird")){
pet = new Bird(nameOfPet);
}
return pet;
}

public String petNameAndSpeak(ArrayList<Pet> pets) {
StringBuilder petList = new StringBuilder();
for(int i = 0; i < pets.size(); i++) {
petList.append(pets.get(i).getName() + " " + pets.get(i).speak()+"\n");
}
System.out.println(petList.toString().trim());
return petList.toString().trim();
}
public void addPet(Pet pet) {
pets.add(pet);
}
public static void main(String [] args){
Application application = new Application();
application.storePetInformation();
application.petNameAndSpeak(application.pets);
}

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

public class Bird extends Pet {
public Bird(String name) {
super(name);
}

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

public class Cat extends Pet {
public Cat(String name) {
super(name);
}

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

public class Dog extends Pet {
public Dog(String name) {
super(name);
}

@Override
public String speak() {
return "Woof";
}
}
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;

import java.util.Comparator;

public class Pet implements Comparable<Pet>, Comparator<Pet>{

private String name;

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

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String speak() {
return "";
}

public int compareTo(Pet o) {
return 0;
}

public int compare(Pet o1, Pet o2) {
return 0;
}
}
21 changes: 21 additions & 0 deletions src/test/java/io/zipcoder/ApplicationTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
package io.zipcoder;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;

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

public class ApplicationTest {
@Test
public void petNameAndSpeakTest() {

Application app = new Application();
Bird bird = new Bird("Ugly Bird");
Cat cat = new Cat ("Fitzgerald");
Dog dog = new Dog("Oreo");
app.pets.add(bird);
app.pets.add(cat);
app.pets.add(dog);

String expected = "Ugly Bird Squak\nFitzgerald Meow\nOreo Bark";
String actual = app.petNameAndSpeak(app.pets);
}

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

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

import static org.junit.Assert.*;


public class BirdTest {

@Test
public void speak() {
Bird bird = new Bird("Birdie Bird");
String expected = "Squak";
String actual = bird.speak();
Assert.assertEquals(expected, actual);
}
@Test
public void setNameTest() {
Bird bird = new Bird("");
String expected = "Ugly Bird";
bird.setName("Ugly Bird");
String actual = bird.getName();
Assert.assertEquals(expected, actual);
}
}
26 changes: 26 additions & 0 deletions src/test/java/io/zipcoder/CatTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.zipcoder;

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

import static org.junit.Assert.*;

public class CatTest {

@Test
public void speak() {
Cat cat = new Cat("Fitzgerald");
String expected = "Meow";
String actual = cat.speak();
Assert.assertEquals(expected, actual);
}
@Test
public void setNameTest() {
Cat cat = new Cat("");
String expected = "Fitzgerald";
cat.setName("Fitzgerald");
String actual = cat.getName();
Assert.assertEquals(expected, actual);
}

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

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

import static org.junit.Assert.*;

public class DogTest {

@Test
public void speak() {
Dog dog = new Dog("Oreo");
String expected = "Woof";
String actual = dog.speak();
Assert.assertEquals(expected, actual);
}
@Test
public void setNameTest() {
Dog dog = new Dog("");
String expected = "Oreo";
dog.setName("Oreo");
String actual = dog.getName();
Assert.assertEquals(expected, actual);
}
}
16 changes: 16 additions & 0 deletions src/test/java/io/zipcoder/PetTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.zipcoder;

import org.junit.Test;

import static org.junit.Assert.*;

public class PetTest {

@Test
public void getName() {
Cat cat = new Cat("Fitzgerald");
String actual = cat.getName();
String expected = "Fitzgerald";
assertEquals(expected, actual);
}
}