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
14 changes: 13 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,19 @@
<groupId>io.zipcoder</groupId>
<artifactId>polymorphism-1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>7</source>
<target>7</target>
</configuration>
</plugin>
</plugins>
</build>
<packaging>jar</packaging>

<name>interfaces-1</name>
<url>http://maven.apache.org</url>
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/io/zipcoder/polymorphism/Animal.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package io.zipcoder.polymorphism;

public interface Animal {
String speak();
}
18 changes: 18 additions & 0 deletions src/main/java/io/zipcoder/polymorphism/Cat.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.zipcoder.polymorphism;

public class Cat extends Pet{
@Override
public String getName() {
return super.getName();
}

@Override
public void setName(String name) {
super.setName(name);
}

@Override
public String speak() {
return "Meow!";
}
}
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;

public class Console {
public static void print(String input) {
System.out.println(input);
}

public static void run(Scanner scanner,Integer noOfPets) {
Pet[] pets = findTypeNameOfPet(scanner, noOfPets);
System.out.println(petSpeaks(pets));
}

public static Pet[] findTypeNameOfPet(Scanner scanner,Integer noOfPets) {
String[] typeOfPets = new String[noOfPets];
String[] petNames = new String[noOfPets];
Pet[] pets=new Pet[noOfPets];
for (Integer index = 0; index < noOfPets; index++) {
System.out.println("What kind of pet is pet " + (index + 1) + "?");
Pet pet = createPet(scanner.next());
System.out.println("What is the the name of pet " + (index + 1) + "?");
pet.setName(scanner.next());
pets[index] = pet;
}
return pets;
}


private static Pet createPet(String petType){
if(petType.equals("dog"))
return new Dog();
else if(petType.equals("cat"))
return new Cat();
else if(petType.equals("rabbit"))
return new Rabbit();
else
return null;
}

public static String petSpeaks(Pet[] pets) {
String result = "";

for(Pet pet: pets)
result += String.format("%s the %s says\t%s\n",
pet.getName(), typeOfPetInString(pet), pet.speak());


return result.substring(0,result.length()-1);
}

private static String typeOfPetInString(Pet pet){
if(pet instanceof Cat)
return "cat";
else if (pet instanceof Dog)
return "dog";
else if (pet instanceof Rabbit)
return "rabbit";
else
return null;
}
}

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

public class Dog extends Pet{
@Override
public String speak() {
return "Bark!";
}
}
9 changes: 9 additions & 0 deletions src/main/java/io/zipcoder/polymorphism/MainApplication.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
package io.zipcoder.polymorphism;

import java.util.Scanner;

public class MainApplication {
public static void main(String[] args) throws InstantiationException, IllegalAccessException {
Scanner scanner=new Scanner(System.in);
Scanner userInput=new Scanner(System.in);
Console.print("How many pets do you have? ");
Integer noOfPets=userInput.nextInt();
Console.run(scanner,noOfPets);
}
}
33 changes: 33 additions & 0 deletions src/main/java/io/zipcoder/polymorphism/Pet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package io.zipcoder.polymorphism;


public abstract class Pet implements Animal{

private String name;
private PetOwner owner;

public String getName() {
return name;
}

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

public void setOwner(PetOwner newPetOwner) {

this.owner=newPetOwner;
}

/**
* @return PetOwner object whose composite `pets` collection contains this Pet instance
*/
public PetOwner getOwner() {

return this.owner;
}

public String speak() {
return "A man chooses, a slave obeys";
}
}
31 changes: 31 additions & 0 deletions src/main/java/io/zipcoder/polymorphism/PetOwner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package io.zipcoder.polymorphism;

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

public class PetOwner {
private final List<Pet> petsList;

public PetOwner(Pet... pets) {
if(pets!=null)
this.petsList=new ArrayList<>(Arrays.asList(pets));
else
this.petsList=new ArrayList<>();
for(Pet pet:petsList){
pet.setOwner(this);
}
}

public PetOwner() {
petsList = new ArrayList<Pet>();
}

public Pet[] getPets() {
return petsList.toArray(new Pet [0]);
}

public void addPet(Pet pet) {
petsList.add(pet);
}
}
8 changes: 8 additions & 0 deletions src/main/java/io/zipcoder/polymorphism/Rabbit.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.zipcoder.polymorphism;

public class Rabbit extends Pet{
@Override
public String speak() {
return "Ribbit!";
}
}
31 changes: 31 additions & 0 deletions src/test/java/io/zipcoder/polymorphism/CatTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package io.zipcoder.polymorphism;

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

public class CatTest {

@Test
public void testSetCatName() {
//given
Cat cat = new Cat();
String expectedName = "Garfield";
//when
cat.setName(expectedName);
String actual = cat.getName();
//then
Assert.assertEquals(expectedName, actual);
}

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


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

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

import java.util.Scanner;
/*
public class ConsoleTest {

@Test
public void testTypeOfPetInString() { //Pet pet
//given

//then

//when

private static String typeOfPetInString(Pet pet){
if(pet instanceof Cat)
return "cat";
else if (pet instanceof Dog)
return "dog";
else if (pet instanceof Rabbit)
return "rabbit";
else
return null;
}


}

@Test
public void testPetSpeaks() {
//given
String expectedSpeak = "Meow!";
//when
Cat cat = new Cat();
String actual = cat.speak();
//then
Assert.assertEquals(expectedSpeak, actual);
}

@Test
public void testFindTypeNameOfPet() { //Scanner scanner, Integer noOfPets
//given
Pet[] pets = new Pet[];
String expectedScanner = "cat";
//when
pets. //findNa(expectedScanner,3)
//then



public static Pet[] findTypeNameOfPet(Scanner scanner,Integer noOfPets) {
String[] typeOfPets = new String[noOfPets];
String[] petNames = new String[noOfPets];
Pet[] pets=new Pet[noOfPets];
for (Integer index = 0; index < noOfPets; index++) {
System.out.println("What kind of pet is pet " + (index + 1) + "?");
Pet pet = createPet(scanner.next());
System.out.println("What is the the name of pet " + (index + 1) + "?");
pet.setName(scanner.next());
pets[index] = pet;
}
return pets;

}



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

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

public class DogTest {


@Test
public void testSetDogName() {
//given
Dog dog = new Dog();
String expectedName = "Rosie";
//when
dog.setName(expectedName);
String actual = dog.getName();
//then
Assert.assertEquals(expectedName, actual);
}

@Test
public void dogSpeakTest() {
//given
Dog dog = new Dog();
//then
String actual = dog.speak();
//when
Assert.assertEquals("Bark!", actual);
}

}

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

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

public class PetOwnerTest {


@Test
public void testPetOwner() {
//given
Cat cat = new Cat();
PetOwner owner = new PetOwner(cat);
Pet[] pets = owner.getPets();
//when
Pet actual = pets[0];
//then
Assert.assertEquals(actual, cat);

}

@Test
public void testAddPet() {
Dog dog = new Dog();
PetOwner owner = new PetOwner();

owner.addPet(dog);
Pet[] pets = owner.getPets();

Assert.assertEquals(dog, pets[0]);

}

@Test
public void testGetPet() {
Dog dog = new Dog();
Cat cat = new Cat();
PetOwner owner = new PetOwner(dog, cat);

Pet[] pets = {dog, cat};
Pet[] actual = owner.getPets();

Assert.assertArrayEquals(pets, actual); //for array tests




}
}
Loading