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

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

public class Application {
private ArrayList<Pet> pets = new ArrayList<>();
private Scanner scanner = new Scanner(System.in);

public static void main(String[] args) {
Application application = new Application();
application.start();
}


public void start() {
System.out.println("How many pets do you have?");
String numberOfPetsString = scanner.nextLine();
int numberOfPets = Integer.parseInt(numberOfPetsString);

for(int i=0;i<numberOfPets; i++) {
System.out.println("What is the type of pet?");
String typeOfPet = scanner.nextLine();

System.out.println("What is the name of the pet?");
String petName = scanner.nextLine();

Pet pet = createPet(typeOfPet, petName);
this.add(pet);
}
printPets();
}

public void printPets() {
for(Pet pet : pets) {
String petName = pet.getName();
String petType = pet.getClass().getSimpleName();
System.out.println("Pet name = " + petName);
System.out.println("Pet type = " + petType);
}
}

//method stubs do not return logic
public Pet[] getPets() {
int size = pets.size();
Pet[] petArray = new Pet[size];
for(int i=0; i<pets.size(); i++) {
Pet currentPet = pets.get(i);
petArray[i] = currentPet;
}
return petArray;
}


public void add(Pet pet) {
pets.add(pet);
}

public Pet createPet(String petType, String petName) {
Pet pet = null;
switch (petType.toLowerCase()) {
case "bird":
pet = new Bird(petName);
break;

case "dog":
pet = new Dog(petName);
break;

case "snake":
pet = new Snake(petName);
break;

case "cat":
pet = new Cat(petName);
break;
}
return pet;
}

}

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

public class Bird extends Pet {

public Bird()
{

}

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

public String speak()
{
return "chirp";
}

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

public String getName()
{
return this.name;
}
}
25 changes: 25 additions & 0 deletions src/main/java/io/zipcoder/Cat.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.zipcoder;

public class Cat extends Pet {

public Cat(){}

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

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

}

public String getName()
{
return this.name;
}

public String speak() {
return "Hang in there.";
}
}
32 changes: 32 additions & 0 deletions src/main/java/io/zipcoder/Compare.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package io.zipcoder;

import java.util.Comparator;
import java.util.Date;

public class Compare implements Comparator<Pet> {

public int compare(Pet pet1, Pet pet2) {
Integer result = null;

String pet1Name = pet1.getName();
String pet2Name = pet2.getName();

int nameComparison = pet1Name.compareTo(pet2Name);
result = nameComparison;

boolean sameName = (nameComparison == 0);

if (sameName) {
Class pet1Class = pet1.getClass();
Class pet2Class = pet2.getClass();

String pet1ClassSimpleName = pet1Class.getSimpleName();
String pet2ClassSimpleName = pet2Class.getSimpleName();

int classComparison = pet1ClassSimpleName.compareTo(pet2ClassSimpleName);
result = classComparison;
}

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

public class Dog extends Pet {


public Dog() {

}

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

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

public String getName() {
return name;
}


public String speak() {
return "I'm such a wimp! I'm running from a cat!";
}
}
28 changes: 28 additions & 0 deletions src/main/java/io/zipcoder/Pet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package io.zipcoder;

import java.lang.Comparable;

public abstract class Pet {

protected String name;

//Constructors
protected Pet(){}

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


public void setName(String name) {

}

public String getName() {

return this.name;
}

public abstract String speak();
}
26 changes: 26 additions & 0 deletions src/main/java/io/zipcoder/Snake.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.zipcoder;

public class Snake extends Pet {


public Snake() {
}

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


public String getName() {
return name;
}

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

public String speak() {
return "I have seen all the dead seasons, and the great trees and the old elephants, and the rocks that were "
+ "bare and sharp-pointed ere the moss grew. Art thou still alive, Manling?";
}
}
60 changes: 60 additions & 0 deletions src/test/java/io/zipcoder/ApplicationTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,65 @@
package io.zipcoder;

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

public class ApplicationTest {

@Test
public void testAddPets() {
//Given
Application application = new Application();
Pet pet1 = new Cat();

//When
application.add(pet1);
Pet[] petArray = application.getPets();


//Then
Pet firstPet = petArray[0];
Assert.assertEquals(pet1, firstPet);
}

@Test
public void testGetPets() {
// Given
Application application = new Application();
Pet pet1 = new Dog();
Pet pet2 = new Cat();
Pet pet3 = new Snake();
Pet[] expectedPets = new Pet[]{pet1, pet2, pet3};

application.add(pet1);
application.add(pet2);
application.add(pet3);

// When
Pet[] actualPets = application.getPets();

// Then
Assert.assertEquals(expectedPets, actualPets);
}


@Test
public void testCreatePet() {
//Given
Application application = new Application();
String petType = "Dog";
String petName = "Milo";

// When
Pet pet = application.createPet(petType, petName);

//Then
Assert.assertTrue(pet instanceof Dog);
}

@Test
public void testSomething2() {

}


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

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

public class BirdTest {

@Test
public void testBirdConstructor()
{
Bird bird = new Bird("Jacob");

String actual = bird.getName();

String expected = "Jacob";

Assert.assertEquals(expected, actual);


}


@Test
public void testSpeakBird()
{
Bird bird = new Bird();
String actual = bird.speak();
String expected = "chirp";
Assert.assertEquals(expected,actual);

}

@Test
public void testSetBirdName()
{
Bird bird = new Bird();
bird.setName("Robin");
String expected = "Robin";
String actual = bird.getName();
Assert.assertEquals(expected,actual);
}
}
Loading