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: 4 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
<groupId>io.zipcoder</groupId>
<artifactId>Interfaces</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
Expand Down
5 changes: 0 additions & 5 deletions src/main/java/io/zipcoder/Application.java

This file was deleted.

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

import io.zipcoder.pets.Pets;

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

public class Application {

private ArrayList<Pets> petList;


public Application() {
this.petList = new ArrayList<Pets>();

}

public void setPetList(ArrayList<Pets> petList) {
this.petList = petList;
}

public ArrayList<Pets> getPetList() {
return petList;
}

public int askForPets() {
Scanner in = new Scanner(System.in);
System.out.println("Please input the number of pets you own:");
return in.nextInt();
}

public ArrayList<Pets> createPetList() {
int numberOfPets = askForPets();
Scanner scan = new Scanner(System.in);
for (int i = 0; i < numberOfPets; i++) {
System.out.println("Please enter type of pet for pet # " + (i + 1));
String type = scan.nextLine();
System.out.println("Please enter pet's name");
String name = scan.nextLine();
petList.add(PetFactory.createPets(type, name));
}
setPetList(petList);
return petList;
}

@Override
public String toString() {
ArrayList<Pets> petList = getPetList();
StringBuilder petString = new StringBuilder();
for (Pets pet: petList) {
petString.append("My name is ")
.append(pet.getName())
.append(" and I say ")
.append(pet.speak())
.append(". ");
}
return petString.toString();

}

public ArrayList<Pets> sortPetsByType() {
Collections.sort(petList, Comparator.comparing(Pets::getType).thenComparing(Pets::getName));
return petList;
}

public ArrayList<Pets> sortPetsByName() {
Collections.sort(petList, Comparator.comparing(Pets::getName).thenComparing(Pets::getType));
return petList;
}

}




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

public class Cat extends Pets {

private String type;

public Cat(String name) {
super(name);
this.type = "cat";
}


public void setType(String type) {
this.type = "cat";
}

@Override
public String getType(){
return this.type;
}


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

public class Dog extends Pets {

private String type;


public Dog(String name) {
super(name);
this.type = "dog";
}


public void setType(String type) {
this.type = "dog";
}


public String getType() {
return type;
}


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

import java.util.ArrayList;

public class Main {
public static void main(String[] args){
Application app = new Application();
ArrayList<Pets> pets = app.createPetList();
String petString = app.toString();
System.out.println(petString);
}
}
33 changes: 33 additions & 0 deletions src/main/java/io/zipcoder/pets/Parrot.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package io.zipcoder.pets;

public class Parrot extends Pets {


private String type;

public Parrot() {
this.type = "parrot";
}

public Parrot(String name) {
super(name);
this.type = "parrot";
}


public void setType(String type) {
this.type = "parrot";
}

public String getType() {
return type;
}


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


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

public class PetFactory {

public static Pets createPets(String pet, String name) {
if(pet.equalsIgnoreCase("dog")) {
return new Dog(name);
} else if (pet.equalsIgnoreCase("cat")) {
return new Cat(name);
} else if (pet.equalsIgnoreCase("parrot")) {
return new Parrot(name);
}
return null;
}

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

import java.util.Comparator;

public abstract class Pets{

private String name;
private String type;

public Pets() {
this.name = "";
}

public Pets(String name) {
this.name = name;
}
public void setType(String type) {
this.type = type;
}

public String getType() {
return type;
}

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

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

public abstract String speak();



}

5 changes: 0 additions & 5 deletions src/test/java/io/zipcoder/ApplicationTest.java

This file was deleted.

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

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

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

public class ApplicationTest {

@Test
public void testCreatePetList(){
Application app = new Application();
Pets dog = PetFactory.createPets("dog", "Sandy");
String expected = "Sandy";
ArrayList<Pets> pets = new ArrayList<>(Arrays.asList(dog));
app.setPetList(pets);
String actual = app.getPetList().get(0).getName();
Assert.assertEquals(expected, actual);
}

@Test
public void testPetsToString(){
Application app = new Application();
String expected = "My name is Sandy and I say Woof. ";
Pets dog = PetFactory.createPets("dog", "Sandy");
ArrayList<Pets> pets = new ArrayList<>(Arrays.asList(dog));
app.setPetList(pets);
String actual = app.toString();
Assert.assertEquals(expected, actual);
}


@Test
public void testCompareByName(){
//Given
Application app = new Application();
Pets dog = new Dog("Sandy");
Pets dog1 = new Dog("Molly");
Pets cat = new Cat("Sandy");
Pets cat1 = new Cat("Andy");
Pets parrot = new Parrot("Id");
ArrayList<Pets> pets = new ArrayList<>(Arrays.asList(dog, dog1, cat, cat1, parrot));
ArrayList<Pets> expected = new ArrayList<>(Arrays.asList(cat1, parrot, dog1, cat, dog));
app.setPetList(pets);
//When
app.sortPetsByName();
//Then
ArrayList<Pets> actual = app.getPetList();
Assert.assertEquals(expected, actual);
}
@Test
public void testCompareByType() {
//Given
Application app = new Application();
Pets dog1 = new Dog("Sandy");
Pets dog2 = new Dog("Molly");
Pets dog3 = new Dog("Ally");
Pets cat = new Cat("Sandy");
Pets cat1 = new Cat("Andy");
Pets parrot = new Parrot("Id");
ArrayList<Pets> pets = new ArrayList<>(Arrays.asList(dog1, dog2, dog3, cat, cat1, parrot));
ArrayList<Pets> expected = new ArrayList<>(Arrays.asList(cat1, cat, dog3, dog2, dog1, parrot));
app.setPetList(pets);
//When
app.sortPetsByType();
ArrayList<Pets> actual = app.getPetList();
//Then
Assert.assertEquals(expected, actual);
}

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

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

public class CatTest {

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

@Test
public void testGetType(){
Cat cat = new Cat("Sally");
String expected = "cat";
cat.setType("cat");
String actual = cat.getType();
Assert.assertEquals(expected, actual);
}

@Test
public void testSpeak() {
Cat cat = new Cat("Sally");
String expected = "Meow";
String actual = cat.speak();
Assert.assertEquals(expected, actual);
}
}
Loading