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>
12 changes: 12 additions & 0 deletions src/main/java/io/zipcoder/pets/Bear.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.zipcoder.pets;

public class Bear extends Pet {
public Bear(String name) {
super(name);
}

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

public class Cat extends Pet {

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

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

public class Dog extends Pet {
public Dog(String name) {
super(name);
}

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

public class Pet {


private String name;
private String type;

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


public String speak() {
return "";
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
60 changes: 60 additions & 0 deletions src/main/java/io/zipcoder/polymorphism/MainApplication.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,67 @@
package io.zipcoder.polymorphism;

import io.zipcoder.pets.Bear;
import io.zipcoder.pets.Cat;
import io.zipcoder.pets.Dog;
import io.zipcoder.pets.Pet;

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

/**
* Created by leon on 11/6/17.
*/
public class MainApplication {
private static Scanner input = new Scanner(System.in);

public static void main(String[] args) {
System.out.println("How many pets do you have?");
Integer numberOfPets = input.nextInt();
ArrayList<Pet> petList = new ArrayList<Pet>();
for (int i = 1; i <= numberOfPets; i++) {
String petType = getTypeOfPet().toLowerCase();
String petName = getPetName();
if (petType.equals("dog")) {
Dog dog = new Dog(petName);
petList.add(dog);
}
if (petType.equals("cat")) {
Cat cat = new Cat(petName);
petList.add(cat);
}
if (petType.equals("bear")) {
Bear bear = new Bear(petName);
petList.add(bear);
}
if (petType.equals("pet")){
Pet pet = new Pet(petName);
petList.add(pet);
}
}
for (int i = 1; i <= petList.size(); i++)
System.out.println(petList.get(i-1).getName()+" || "+ petList.get(i-1).speak());
}



public static String getTypeOfPet(){
System.out.println("What kind of pet?");
String petType = input.next();
return petType;
}

public static String getPetName() {
System.out.println("What is the name of your pet?");
String petName = input.next();
return petName;
}
}









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

import io.zipcoder.pets.Bear;
import io.zipcoder.pets.Pet;
import org.junit.Assert;
import org.junit.Test;

public class BearTest {
@Test
public void bearInstanceOfTest(){
Bear bear= new Bear("Ay");
Assert.assertTrue(bear instanceof Pet);
}
@Test
public void bearGetNameTest(){
Bear bear= new Bear("Ay");
String actual = bear.getName();

Assert.assertEquals("Ay",actual);
}

@Test
public void bearSetNameTest(){
Bear bear= new Bear("Ay");
bear.setName("Bee");
String actual = bear.getName();

Assert.assertEquals("Bee",actual);
}
@Test
public void bearSpeakTest(){
Bear bear= new Bear("Ay");
String actual = bear.speak();
Assert.assertEquals("*chews Human*",actual);
}
}


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

import io.zipcoder.pets.Cat;
import io.zipcoder.pets.Pet;
import org.junit.Assert;
import org.junit.Test;

public class CatTest {
@Test
public void catInstanceOfTest(){
Cat cat= new Cat("Ay");
Assert.assertTrue(cat instanceof Pet);
}
@Test
public void catGetNameTest(){
Cat cat= new Cat("Ay");
String actual = cat.getName();

Assert.assertEquals("Ay",actual);
}

@Test
public void catSetNameTest(){
Cat cat= new Cat("Ay");
cat.setName("Bee");
String actual = cat.getName();

Assert.assertEquals("Bee",actual);
}
@Test
public void catSpeakTest(){
Cat cat= new Cat("Ay");
String actual = cat.speak();
Assert.assertEquals("Meow!",actual);
}
}


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

import io.zipcoder.pets.Dog;
import io.zipcoder.pets.Pet;
import org.junit.Assert;
import org.junit.Test;

public class DogTest {
@Test
public void dogInstanceOfTest(){
Dog dog= new Dog("Ay");
Assert.assertTrue(dog instanceof Pet);
}
@Test
public void dogGetNameTest(){
Dog dog= new Dog("Ay");
String actual = dog.getName();

Assert.assertEquals("Ay",actual);
}

@Test
public void dogSetNameTest(){
Dog dog= new Dog("Ay");
dog.setName("Bee");
String actual = dog.getName();

Assert.assertEquals("Bee",actual);
}
@Test
public void dogSpeakTest(){
Dog dog= new Dog("Ay");
String actual = dog.speak();
Assert.assertEquals("Woof!",actual);
}
}