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
8 changes: 8 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,13 @@
<artifactId>polymorphism</artifactId>
<version>1.0-SNAPSHOT</version>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>


</project>
53 changes: 53 additions & 0 deletions src/main/java/io/zipcoder/polymorphism/App.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package io.zipcoder.polymorphism;

import io.zipcoder.polymorphism.pets.Cat;
import io.zipcoder.polymorphism.pets.Dog;
import io.zipcoder.polymorphism.pets.Horse;
import io.zipcoder.polymorphism.pets.Pet;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class App {

List<Pet> petList = new ArrayList<Pet>();
Console menu = new Console(System.in, System.out);

public void runApp() {

int numberOfPets;

numberOfPets = this.menu.getIntegerInput("How many pets do you have?");

for (int i = 0; i < numberOfPets; i++) {
String petType = "";
String petName = "";
petType = this.menu.getStringInput("What type is pet: " + (i+1) + "?");
addPet(petType);
petName = this.menu.getStringInput("What is the pet name?");
this.petList.get(i).setName(petName);
}

printPets();


}

public void addPet(String petType){
petType = petType.toLowerCase();
if (petType.equals("cat")) this.petList.add(new Cat());
if (petType.equals("dog")) this.petList.add(new Dog());
if (petType.equals("horse")) this.petList.add(new Horse());
}

public void printPets(){
int i = 0;
for (Pet eachPet : this.petList){
String petInfo = "";
petInfo = petInfo.concat(this.petList.get(i).getPetType().concat(": " + this.petList.get(i).getName()));
this.menu.println(petInfo);
i++;
}
}
}
62 changes: 62 additions & 0 deletions src/main/java/io/zipcoder/polymorphism/Console.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package io.zipcoder.polymorphism;

import java.util.Scanner;
import java.io.PrintStream;
import java.io.InputStream;

public final class Console {

private final Scanner userInput;
private final PrintStream output;


//----------- constructor --------------------------
public Console (InputStream in, PrintStream out) {
this.userInput = new Scanner(in);
this.output = out;
}

//----------- format the prompt and display it -----------
public void println (String val, Object... args){
print(val + "\n",args);
}

public void print (String val, Object... args){
this.output.format(val,args);
}

//----------- prompt the user for input and get the string value of the answer ----------
public String getStringInput (String prompt, Object... args){
println(prompt,args);
return this.userInput.nextLine();
}

//----------- prompt the user for input and get the numeric value of the answer ----------

public Integer getIntegerInput (String prompt, Object... args){
String tempInput = getStringInput(prompt,args);
try{
Integer integerInput = Integer.parseInt(tempInput);
return integerInput;
} catch (NumberFormatException nfe){
println("[ %s ] is not a valid user input!",tempInput);
println("Try inputting an integer value!");
return getIntegerInput(prompt,args);
}
}

public Double getDoubleInput(String prompt, Object... args){
String tempInput = getStringInput(prompt,args);
try {
Double doubleInput = Double.parseDouble(tempInput);
return doubleInput;
} catch (NumberFormatException nfe){
println("[ %s ] is not a valid user input!",tempInput);
println("Try inputting a number with decimals value!");
return getDoubleInput(prompt,args);
}

}


}
4 changes: 4 additions & 0 deletions src/main/java/io/zipcoder/polymorphism/MainApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@
* Created by leon on 11/6/17.
*/
public class MainApplication {
public static void main (String [] args){
App mainApp = new App();
mainApp.runApp();
}
}
20 changes: 20 additions & 0 deletions src/main/java/io/zipcoder/polymorphism/pets/Cat.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.zipcoder.polymorphism.pets;

public class Cat extends Pet {

public Cat (String name){
super.setName(name);
super.setPetType("Cat");
}

public Cat(){
super.setName("No name cat");
super.setPetType("Cat");
}

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


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

public class Dog extends Pet {

public Dog (String name){
super.setName(name);
super.setPetType("Dog");
}

public Dog (){
super.setPetType("Dog");
super.setName("No name dog");
}

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

public class Horse extends Pet{


public Horse(String name){
super.setName(name);
super.setPetType("Horse");
}

public Horse(){
super.setName("No name horse");
super.setPetType("Horse");
}

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

public abstract class Pet {

private String name;


private String petType;

public abstract String speak();



public String getName() {
return name;
}

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

public String getPetType() {
return petType;
}

public void setPetType(String petType) {
this.petType = petType;
}

}


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

import io.zipcoder.polymorphism.pets.Cat;
import io.zipcoder.polymorphism.pets.Dog;
import io.zipcoder.polymorphism.pets.Horse;
import org.junit.Assert;
import org.junit.Test;

import static org.junit.Assert.*;

public class AppTest {
App testApp = new App();

@Test
public void addPet() {

testApp.addPet("Cat");
testApp.addPet("Dog");
int expected = 2;
int actual = testApp.petList.size();

Assert.assertEquals(expected,actual);
}

@Test
public void printPets() {
testApp.addPet("Cat");
testApp.addPet("Dog");

String expected = "No name cat";
String actual = testApp.petList.get(0).getName();
Assert.assertEquals(expected,actual);

expected = "No name dog";
actual = testApp.petList.get(1).getName();
Assert.assertEquals(expected,actual);
}

@Test
public void printHorse(){
testApp.addPet("Horse");

String expected = "No name horse";
String actual = testApp.petList.get(0).getName();

Assert.assertEquals(expected,actual);

}


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

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

import static org.junit.Assert.*;

public class CatTest {

Cat testCat;

@Test
public void speak() {

testCat = new Cat();

String expected = "meow";
String actual = testCat.speak();

Assert.assertEquals(expected,actual);
}
@Test
public void testConstructorNoName() {

testCat = new Cat();

String expected = "No name cat";
String actual = testCat.getName();

Assert.assertEquals(expected,actual);
}
@Test
public void testConstructorWithName() {

String expected = "Jiffy";
testCat = new Cat(expected);
String actual = testCat.getName();

Assert.assertEquals(expected,actual);
}

@Test
public void testGetType() {

testCat = new Cat("Jiffy");

String expected = "Cat";
String actual = testCat.getPetType();

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

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

import static org.junit.Assert.*;

public class DogTest {

@Test
public void speak() {
Dog testDog = new Dog();
String expected = "Woof";
String actual = testDog.speak();

Assert.assertEquals(expected,actual);
}

@Test
public void testConstructorNoName() {
Dog testDog = new Dog();
String expected = "No name dog";
String actual = testDog.getName();

Assert.assertEquals(expected,actual);
}

@Test
public void testConstructorWithName() {
String expected = "Scully";
Dog testDog = new Dog(expected);
String actual = testDog.getName();

Assert.assertEquals(expected,actual);
}

@Test
public void testGetType() {

Dog testDog = new Dog("Scully");

String expected = "Dog";
String actual = testDog.getPetType();

Assert.assertEquals(expected, actual);
}

}
Loading