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

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;
}


}


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

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

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

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

public String getName() {

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

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

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

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

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

public class Dog extends Pet{

public Dog(String name,Integer age,String breed){
super(name,age,breed);
}

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

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

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

import io.Console;

import java.util.Scanner;

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.Dinosaur \n");

//Console.getStringInput("How many pets do you have? \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:
Dinosaur dinosaur=new Dinosaur(name,null,null);
pet.addPet(pet);
break;

}

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



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

public class Pet {
String name;
Integer age;
String breed;
//Integer id;
ArrayList<Pet> pets= new ArrayList<Pet>();

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

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

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

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 String speak(){
return "I'm not talking until you tell me what kinda pet i'm";
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
37 changes: 37 additions & 0 deletions src/test/java/io/zipcoder/polymorphism/CatTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import io.zipcoder.polymorphism.Cat;
import junit.framework.TestCase;
import org.junit.Assert;

public class CatTest extends TestCase {

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

}

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

public void testBreed(){
Cat cat = new Cat("Tess", 5, "Tuxedo");
String actual = cat.getBreed();
String expected = "Tuxedo";

Assert.assertEquals(actual, expected);
}

public void testAge() {
Cat cat = new Cat("Tess", 5, "Tuxedo");
Integer actual = cat.getAge();
Integer expected = 5;

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

import junit.framework.TestCase;
import org.junit.Assert;

public class DinosaurTest extends TestCase {

public void testSpeak() {
Dinosaur dinosaur =new Dinosaur(null,null,null);
dinosaur.speak();
Assert.assertEquals("Rawr!",dinosaur.speak());

}

public void testName(){
Dinosaur dinosaur =new Dinosaur(null,null,null);
dinosaur.setName("Joseph");
String actual = dinosaur.getName();
String expected = "Joseph";
Assert.assertEquals(actual, expected);
}

public void testBreed(){
Dinosaur dinosaur = new Dinosaur("Terrence", 15, "Friendly Rex");
String actual = dinosaur.getBreed();
String expected = "Friendly Rex";

Assert.assertEquals(actual, expected);
}

public void testAge(){
Dinosaur dinosaur = new Dinosaur("Terrence", 15, "Friendly Rex");
Integer actual = dinosaur.getAge();
Integer expected = 15;

Assert.assertEquals(actual, expected);
}
}
37 changes: 37 additions & 0 deletions src/test/java/io/zipcoder/polymorphism/DogTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import io.zipcoder.polymorphism.Dog;
import junit.framework.TestCase;
import org.junit.Assert;

public class DogTest extends TestCase {

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

}

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

public void testBreed(){
Dog dog = new Dog("Tess", 5, "Tuxedo");
String actual = dog.getBreed();
String expected = "Tuxedo";

Assert.assertEquals(actual, expected);
}

public void testAge() {
Dog dog = new Dog("Tess", 5, "Tuxedo");
Integer actual = dog.getAge();
Integer expected = 5;

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

import junit.framework.TestCase;
import org.junit.Assert;
import org.junit.Test;

public class PetTest extends TestCase {

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

}

public void testRemovePet() {
Pet pet =new Pet();
Integer expected = 2;
Dog dog = new Dog(null,null,null);
Dinosaur dino = new Dinosaur("bumpy",1,"trico");
Cat cat = new Cat("marble",5,"fat");
pet.addPet(dog);
pet.addPet(dino);
pet.addPet(cat);
pet.removePet(dog);
Assert.assertEquals(expected,pet.numberOfPets());
}

public void testSpeak() {
Pet pet =new Pet(null,null,null);
pet.speak();
Assert.assertEquals("I'm not talking until you tell me what kinda pet i'm",pet.speak());
}
@Test
public void testInheritance() {
Pet pet = new Dog(null,null,null);
Assert.assertTrue(pet instanceof Pet);
}


}