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
12 changes: 12 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@
<groupId>io.zipcoder</groupId>
<artifactId>Interfaces</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
Expand Down
54 changes: 54 additions & 0 deletions 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;

public class Application {

public ArrayList<Pet> collectionOfPets = new ArrayList<>();

Scanner scan = new Scanner(System.in);

public void userDataOfPets(){
System.out.println("How many pets do you have?");
int numberOfPets = (Integer.valueOf(scan.nextLine()));
for (int i = 0; i < numberOfPets; i++){
String name;
String petType;
System.out.println("What is the name of pet " + (i +1) +" ?");
name = scan.nextLine();
System.out.println("What kind of pet is it "+ (i +1) +" ?");
petType = scan.nextLine();
addPet(name, petType);

}
}
public void addPet(String petName, String petType) {
Pet pet = null;
if (petType.equalsIgnoreCase("Dog")) {
collectionOfPets.add(new Dog(petName));

} else if (petType.equalsIgnoreCase("Cat")) {
collectionOfPets.add(new Cat(petName));
} else if (petType.equalsIgnoreCase("Unicorn")) {
collectionOfPets.add(new Unicorn(petName));
} else {
pet = null;
}

}
public String printList() {
StringBuilder sb = new StringBuilder();
for (Pet pet : collectionOfPets) {
sb.append(pet.getName());
sb.append(" ");
sb.append(pet.getClass().getSimpleName());
sb.append(", ");

}
return String.valueOf(sb);
}



public static void main(String[] args) {
Application application = new Application();
application.userDataOfPets();
application.printList();
}
}
18 changes: 18 additions & 0 deletions src/main/java/io/zipcoder/Cat.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.zipcoder;

public class Cat extends Pet {
private String petName;

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

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

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

public class Dog extends Pet {

private String petName;

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

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

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


import java.util.Comparator;

public abstract class Pet implements Comparable<Pet> {

private String petName;

public abstract String speak();

public String getName() {
return petName;
}

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


public int compareTo(Pet o) {
int compare = this.getName().compareTo(o.getName());
int compareClass = this.getClass().getSimpleName().compareTo(o.getClass().getSimpleName());
if (compare == 0) {
return compareClass;
}

return compare;
}

@Override
public String toString() {
return super.toString();
}

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

import java.util.Comparator;

public class PetCompare implements Comparator<Pet> {

public int compare(Pet o1, Pet o2) {
int compare = o1.getClass().getSimpleName().compareToIgnoreCase(o2.getClass().getSimpleName());
if(compare != 0) {
return o1.getName().compareToIgnoreCase(o1.getName());
}
return compare;
}}
23 changes: 23 additions & 0 deletions src/main/java/io/zipcoder/Unicorn.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.zipcoder;

public class Unicorn extends Pet {
private String petName;

public Unicorn(String petName) {
this.petName = petName;

}

public String getName() {

return this.petName;
}


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


}
42 changes: 41 additions & 1 deletion src/test/java/io/zipcoder/ApplicationTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,45 @@
package io.zipcoder;

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

import static org.junit.Assert.*;

public class ApplicationTest {
}

@Test
public void userDataOfPets() {
}

@Test
public void addPet() {
//Given
Application application = new Application();
//When
application.addPet("Hugo", "Dog");
String expected = "Hugo Dog, ";
String actual = application.printList();
//Then
Assert.assertEquals(expected, actual);

}

@Test
public void printListTest() {
//Given
Application application = new Application();

//When
application.addPet("Hugo", "Dog");
application.addPet("Bob", "Cat");

String expected = "Hugo Dog, Bob Cat, ";
String actual = application.printList();

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

}


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

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

import static org.junit.Assert.*;

public class CatTest {


@Test
public void speakTest() {
//Given
Cat cat = new Cat("Sammy");

//When
String expected = "meow";
String actual = cat.speak();

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

@Test
public void setNameTest() {
//Given
Cat cat = new Cat("Kitty");

//When
String expected = "Kitty";
cat.setPetName(expected);
String actual = cat.getName();

//Then
Assert.assertEquals(expected, actual);
}
}
37 changes: 37 additions & 0 deletions src/test/java/io/zipcoder/DogTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package io.zipcoder;

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

import static org.junit.Assert.*;

public class DogTest {


@Test
public void speakTest() {
//Given
Dog dog = new Dog("Spike");

//When
String expected = "woof";
String actual = dog.speak();

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

@Test
public void setNameTest() {
//Given
Dog dog = new Dog("Sally");

//When
String expected = "Sally";
dog.setPetName(expected);
String actual = dog.getName();

//Then
Assert.assertEquals(expected, actual);
}
}
78 changes: 78 additions & 0 deletions src/test/java/io/zipcoder/PetTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package io.zipcoder;

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

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

import static org.junit.Assert.*;

public class PetTest {

@Test
public void setNameTest() {
//Given
Dog dog = new Dog("Molly");

//When
String expected = "Molly";
dog.setPetName(expected);
String actual = dog.getName();

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

@Test
public void compareToTest() {
//Given
ArrayList<Pet> tempList = new ArrayList<>();
Dog bun = new Dog("Bun");
Dog mun = new Dog("Mun");
Cat fun = new Cat("Fun");
Cat hun = new Cat("Hun");
Unicorn run = new Unicorn("Run");

tempList.add(bun);
tempList.add(mun);
tempList.add(fun);
tempList.add(hun);
tempList.add(run);

//When
Pet[] expected = {bun, fun, hun, mun, run};
Collections.sort(tempList);
Pet[] actual = tempList.toArray(new Pet[tempList.size()]);

//When
Assert.assertTrue(Arrays.equals(expected,actual));

}

@Test
public void compareTest(){
//Given
Application app = new Application();

Dog bun = new Dog("Bun");
Cat fun = new Cat("Fun");
Unicorn run = new Unicorn("Run");
Unicorn sun = new Unicorn("Sun");

app.collectionOfPets.add(bun);
app.collectionOfPets.add(fun);
app.collectionOfPets.add(run);
app.collectionOfPets.add(sun);

//When
Collections.sort(app.collectionOfPets,new PetCompare());
Pet[] expected = {bun, fun, run, sun};
Pet[] actual = app.collectionOfPets.toArray(new Pet[app.collectionOfPets.size()]);

//When
Assert.assertTrue(Arrays.equals(expected,actual));

}
}
Loading