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 @@ -7,6 +7,14 @@
<groupId>io.zipcoder</groupId>
<artifactId>polymorphism</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>


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

public class Cat extends Pet {

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

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

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

public class Dog extends Pet {

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

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

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

public class Fish extends Pet {

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

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

public class GuineaPig extends Pet {

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

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

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

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

/**
* Created by leon on 11/6/17.
*/
public class MainApplication {

private static ArrayList<Pet> pets;

public static void main(String[] args) {
Integer numOfPets = getInteger("How many pets do you have?");
ArrayList<Pet> pet = new ArrayList<Pet>();
pets = pet;


for(int i = 0; i < numOfPets; i++) {
String species = getStringInput("What kind of pet?");
String name = getStringInput("What is their name?");
if(species.equals("cat")) {
pets.add(new Cat(name));
}
else if(species.equals("dog")) {
pets.add(new Dog(name));
}
else if(species.equals("guineapig")){
pets.add(new GuineaPig(name));
}
else{
pets.add(new Fish(name));
}
}


printPets();

}
public static String getStringInput(String prompt){
Scanner scanner = new Scanner(System.in);
System.out.println(prompt);
String userInput = scanner.nextLine();
return userInput;
}

public static Integer getInteger(String prompt) {
Scanner scanner = new Scanner(System.in);
System.out.println(prompt);
String userInput = scanner.nextLine();
return Integer.parseInt(userInput);
}
public static void printPets(){
for(Pet p : pets) {
System.out.println(p.getName()+ " "+ p.speak());
}
}
}
25 changes: 25 additions & 0 deletions src/main/java/io/zipcoder/polymorphism/Pet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.zipcoder.polymorphism;

public abstract class Pet {

private String name;


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

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


public String getName(){
return name;
}

public abstract String speak();

}


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

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

public class CatTest
{
@Test
public void CatTest()
{
Cat name = new Cat("fluffy");
String actual = name.getName();
String expected = "fluffy";
Assert.assertEquals(expected, actual);
}

@Test
public void SpeechTest()
{
String actual = new Cat("fluffy").speak();
String expected = "meow";
Assert.assertEquals(expected, actual);
}
}
24 changes: 24 additions & 0 deletions src/test/java/io/zipcoder/polymorphism/DogTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.zipcoder.polymorphism;

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

public class DogTest
{
@Test
public void DogTest()
{
Dog name = new Dog("clifford");
String actual = name.getName();
String expected = "clifford";
Assert.assertEquals(expected, actual);
}

@Test
public void SpeechTest()
{
String actual = new Dog("cliffors").speak();
String expected = "bark";
Assert.assertEquals(expected, actual);
}
}
24 changes: 24 additions & 0 deletions src/test/java/io/zipcoder/polymorphism/FishTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.zipcoder.polymorphism;

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

public class FishTest
{
@Test
public void FishTest()
{
Fish name = new Fish("nemo");
String actual = name.getName();
String expected = "nemo";
Assert.assertEquals(expected, actual);
}

@Test
public void SpeechTest()
{
String actual = new Fish("nemo").speak();
String expected = "glub";
Assert.assertEquals(expected, actual);
}
}
24 changes: 24 additions & 0 deletions src/test/java/io/zipcoder/polymorphism/GuineaPigTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.zipcoder.polymorphism;

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

public class GuineaPigTest
{
@Test
public void GuineaPigTest()
{
GuineaPig name = new GuineaPig("whiskers");
String actual = name.getName();
String expected = "whiskers";
Assert.assertEquals(expected, actual);
}

@Test
public void SpeechTest()
{
String actual = new GuineaPig("whiskers").speak();
String expected = "wheek";
Assert.assertEquals(expected, actual);
}
}