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>6</source>
<target>6</target>
</configuration>
</plugin>
</plugins>
</build>
<packaging>jar</packaging>

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

import java.util.Scanner;

public class Console {

public static void printWelcome(){
System.out.println("Number Of Pets");
}

public static void print(String output,Object... args){
System.out.printf(output, args);
}

public static void println(String output,Object... args){
print(output + "\n" + args);
}

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

public static Integer getIntegerInput(String prompt) {
Scanner scanner = new Scanner(System.in);
println(prompt);
Integer userInput = 0;
try {
userInput = scanner.nextInt();
}
catch (Exception e)
{
System.out.println("Enter a valid number!");
}
return userInput;
}


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

public class Cat extends Pet{
public Cat(String name ,Integer age,String breed) {
super(name, breed, age);
}

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

public void setName(String milo){
name = milo;
}

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

public class Dog extends Pet{
public Dog (String name ,Integer age,String breed){
super(name, breed, age);
}
@Override
public String speak(){
return "Bark!";
}

public void setName(String jim){
name = jim;
}

public String getName(){
return name;
}
}
33 changes: 33 additions & 0 deletions src/main/java/io/zipcoder/polymorphism/MainApplication.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,37 @@
package io.zipcoder.polymorphism;

import io.zipcoder.common.Console;

public class MainApplication {
public static void main(String[] args) {
Console.printWelcome();
Integer numberOfPets = Console.getIntegerInput("How many pets do you have? \n");
Pet pet = new Pet();

for (int i = 0; i < numberOfPets; i++) {

Integer options = Console.getIntegerInput("What type of pet do you have?Enter a number \n" + "1.Dog \n"
+ "2.Cat \n" + "3.Rabbit \n");
String name = Console.getStringInput("what is his/her name? \n");

switch(options) {
case 1:
Dog dog = new Dog(name, null, null);
pet.addPet(pet);
break;
case 2:
Cat cat = new Cat(name, null, null);
pet.addPet(pet);
break;
case 3:
Rabbit rabbit = new Rabbit(name, null, null);
pet.addPet(pet);
break;
}

}
Console.println("Information has been Entered.Thank you!");

}

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

import java.util.ArrayList;

public class Pet {
Integer age;
String name;
String breed;

ArrayList<Pet> pets = new ArrayList<Pet>();

public Pet(String name,String breed,Integer age){
this.name = name;
this.breed= breed;
this.age = age;

}
public Pet(){}

public Integer getAge() {
return age;
}

public void setAge(Integer age) {
this.age = age;
}

public String getName() {
return name;
}

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

public String getBreed() {
return breed;
}

public void setBreed(String breed) {
this.breed = breed;
}

public void addPet(Pet pet){
pets.add(pet);
}

public void removePet(Pet pet){
pets.remove(pet);
}

public Integer numberOfPets(){
return pets.size();
}

public String speak(){
return "I am Not Going To Speak Until You tell what Pet i am";
}

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

public class Rabbit extends Pet{
public Rabbit(String name ,Integer age,String breed){
super(name, breed, age);
}
@Override
public String speak(){
return "rab rab!";
}

public void setName(String lucky){
name = lucky;
}

public String getName(){
return name;
}
}
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 org.junit.Assert;
import org.junit.Test;

public class CatTest {
@Test
public void testSpeak(){
Cat cat = new Cat(null,null,null);
cat.speak();
Assert.assertEquals("Meow!",cat.speak());
}

@Test
public void testName(){
Cat cat = new Cat(null ,null,null);
cat.setName("milo");
String actual = cat.getName();
String expected = "milo";
Assert.assertEquals(expected,actual);
}

@Test
public void testBreed(){
Cat cat = new Cat("milo",5,"Tuxedo");
String actual= cat.getBreed();
String expected = "Tuxedo";
Assert.assertEquals(expected,actual);
}

@Test
public void testAge(){
Cat cat = new Cat("milo",5,"Tuxedo");
Integer actual = cat.getAge();
Integer expected = 5;
Assert.assertEquals(expected,actual);
}
}
38 changes: 38 additions & 0 deletions src/test/java/io/zipcoder/polymorphism/DogTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package io.zipcoder.polymorphism;

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

public class DogTest {
@Test
public void testSpeak(){
Dog dog = new Dog(null,null,null);
dog.speak();
Assert.assertEquals("Bark!",dog.speak());
}

@Test
public void testName(){
Dog dog = new Dog(null,null,null);
dog.setName("jim");
String actual = dog.getName();
String expected = "jim";
Assert.assertEquals(expected,actual);
}

@Test
public void testBreed(){
Dog dog = new Dog("jim",5,"Tuxedo");
String actual= dog.getBreed();
String expected = "Tuxedo";
Assert.assertEquals(expected,actual);
}

@Test
public void testAge(){
Dog dog = new Dog("milo",5,"Tuxedo");
Integer actual = dog.getAge();
Integer expected = 5;
Assert.assertEquals(expected,actual);
}
}
47 changes: 47 additions & 0 deletions src/test/java/io/zipcoder/polymorphism/PetTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package io.zipcoder.polymorphism;

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

public class PetTest {
@Test
public void testAddPet() {
Pet pet =new Pet();
String expected ="milo";
Dog dog = new Dog(expected,null,"lab");
pet.addPet(dog);
Assert.assertEquals(expected,dog.getName());

}

@Test

public void testRemovePet() {
Pet pet =new Pet();
Integer expected = 2;
Dog dog = new Dog(null,null,null);
Rabbit rabbit = new Rabbit("lucky",1,"Cavies");
Cat cat = new Cat("milo",5,"fat");
pet.addPet(dog);
pet.addPet(rabbit);
pet.addPet(cat);
pet.removePet(dog);
Assert.assertEquals(expected,pet.numberOfPets());
}

@Test

public void testSpeak() {
Pet pet =new Pet(null,null,null);
pet.speak();
Assert.assertEquals("I am Not Going To Speak Until You tell what Pet i am",pet.speak());
}

@Test
public void testInheritance() {
Pet pet = new Dog(null,null,null);
Assert.assertTrue(pet instanceof Pet);
}


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

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

public class RabbitTest {
@Test
public void testSpeak(){
Rabbit rabbit= new Rabbit(null,null,null);
rabbit.speak();
Assert.assertEquals("rab rab!",rabbit.speak());
}

@Test
public void testName(){
Rabbit rabbit = new Rabbit("lucky" ,5,"Caves");
rabbit.setName("lucky");
String actual = rabbit.getName();
String expected = "lucky";
Assert.assertEquals(expected,actual);
}

@Test
public void testBreed(){
Rabbit rabbit = new Rabbit("lucky",5,"Cavies");
String actual= rabbit.getBreed();
String expected = "Cavies";
Assert.assertEquals(expected,actual);
}

@Test
public void testAge(){
Rabbit rabbit= new Rabbit("lucky",5,"Cavies");
Integer actual = rabbit.getAge();
Integer expected = 5;
Assert.assertEquals(expected,actual);
}
}