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


public class Application {


private int numberOfPets = 0;
private ArrayList<String> typeOfPet = new ArrayList<String>();
private ArrayList<String> nameOfPet = new ArrayList<String>();
private ArrayList<Pets> allPets = new ArrayList<Pets>();

private HashMap<String, String> allPetCollection = new HashMap<String, String>();

Scanner userInput = new Scanner(System.in);


public void getPetInfo() {
System.out.println("Hey fool! How many pets do you have?");
numberOfPets = userInput.nextInt();
userInput.nextLine();

if (numberOfPets > 0) {
for (int i = 0; i < numberOfPets; i++) {
String name;
String type;
System.out.println("What type of pet is pet #: " + (i +1) + "?\n Only enter Dog, Cat or Snake");
type = userInput.nextLine();
System.out.println("What is your " + type + "'s name?");
name = userInput.nextLine();

this.allPets.add(PetFactory.createPet(type,name));
}
} else getPetInfo();

}

public void buildPetMap (ArrayList<String> typeOfPet, ArrayList<String> name) {
for (int i = 0; i < typeOfPet.size(); i++) {
allPetCollection.put(typeOfPet.get(i), nameOfPet.get(i));
}
}

public String getPetFromMap(String key){
return this.allPetCollection.get(key);
}


public void setNumberOfPets() {
this.numberOfPets =numberOfPets;
}

public Integer getNumberOfPets() {
return this.numberOfPets;
}

public void allPetsSpeak() {
for (Pets pet : allPets) {
System.out.println("Your " + pet.getClass().getSimpleName() + " named " + pet.getName() + " says " + pet.speak() +"!");
}
}


public static void main(String[] args) {

Application app = new Application();
app.getPetInfo();
app.allPetsSpeak();
}




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

public class Cat extends Pets {

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

}


@Override
public String speak() {

return "meow";
}

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

public class Dog extends Pets {




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


}





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

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

public class PetFactory {

public static Pets 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("Snake"))
return new Snake(name);

return null;
}
}

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

import java.util.Comparator;

public abstract class Pets implements Comparable<Pets>, Comparator<Pets> {

public String name;


public Pets(String name) {
this.name = name;

}


public abstract String speak();


public String getName() {
return name;
}

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

public int compareTo(Pets pet) {
int compare = this.getName().compareToIgnoreCase(pet.getName());

if (compare != 0) {
return compare;
} else {
compare = this.getClass().getSimpleName().compareToIgnoreCase(pet.getClass().getSimpleName());
return compare;
}
}

public int compare(Pets pet1, Pets pet2) {
int compare = pet1.getClass().getSimpleName().compareToIgnoreCase(pet2.getClass().getSimpleName());
if (compare != 0) {
return compare;
} else {
compare = pet1.getName().compareToIgnoreCase(pet2.getName());
return compare;

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

public class Snake extends Pets {

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

@Override
public String speak() {

return "hiss";
}


}
32 changes: 32 additions & 0 deletions src/test/java/io/zipcoder/CatTest.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 static org.junit.Assert.*;

public class CatTest {

@Test
public void getNameOfCatsTest() {

Cat cat = new Cat("Felix");

String expected = "Felix";
String actual = cat.getName();

Assert.assertEquals(expected, actual);

}

@Test
public void catSpeakTest() {
Cat cat = new Cat("Felix");
String expected = "meow";
String actual = cat.speak();

Assert.assertEquals(expected, actual);

}

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

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

import static org.junit.Assert.*;

public class DogTest {

@Test
public void getNameOfDogsTest() {

Dog dog = new Dog("Butch");

String expected = "Butch";
String actual = dog.getName();

Assert.assertEquals(expected, actual);

}

@Test
public void dogSpeakTest() {
Dog dog = new Dog("");
String expected = "bark";
String actual = dog.speak();

Assert.assertEquals(expected, actual);

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

import org.junit.Test;

import static org.junit.Assert.*;

public class PetsTest {

@Test
public void getPetNameTest() {


}


@Test
public void speakTest() {

}


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

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

import static org.junit.Assert.*;

public class SnakeTest {

@Before
public void setUp() {
}


@Test
public void getNameOfSnakesTest() {

Snake snake = new Snake("Luke");

String expected = "Luke";
String actual = snake.getName();

Assert.assertEquals(expected, actual);

}

@Test
public void snakeSpeakTest() {
Snake snake = new Snake("Luke");
String expected = "hiss";
String actual = snake.speak();

Assert.assertEquals(expected, actual);


}


}