diff --git a/src/main/java/io/Console.java b/src/main/java/io/Console.java new file mode 100644 index 0000000..6036e6f --- /dev/null +++ b/src/main/java/io/Console.java @@ -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; + } + + + } + + diff --git a/src/main/java/io/zipcoder/polymorphism/Cat.java b/src/main/java/io/zipcoder/polymorphism/Cat.java new file mode 100644 index 0000000..c6a32b9 --- /dev/null +++ b/src/main/java/io/zipcoder/polymorphism/Cat.java @@ -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; + } +} diff --git a/src/main/java/io/zipcoder/polymorphism/Dinosaur.java b/src/main/java/io/zipcoder/polymorphism/Dinosaur.java new file mode 100644 index 0000000..20c1fc5 --- /dev/null +++ b/src/main/java/io/zipcoder/polymorphism/Dinosaur.java @@ -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; + } +} diff --git a/src/main/java/io/zipcoder/polymorphism/Dog.java b/src/main/java/io/zipcoder/polymorphism/Dog.java new file mode 100644 index 0000000..1cc1c61 --- /dev/null +++ b/src/main/java/io/zipcoder/polymorphism/Dog.java @@ -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; + } +} diff --git a/src/main/java/io/zipcoder/polymorphism/MainApplication.java b/src/main/java/io/zipcoder/polymorphism/MainApplication.java index 668c627..c133f54 100644 --- a/src/main/java/io/zipcoder/polymorphism/MainApplication.java +++ b/src/main/java/io/zipcoder/polymorphism/MainApplication.java @@ -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 pets= new ArrayList(); + + 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; + } +} diff --git a/src/test/java/io/zipcoder/polymorphism/CatTest.java b/src/test/java/io/zipcoder/polymorphism/CatTest.java new file mode 100644 index 0000000..597ec6e --- /dev/null +++ b/src/test/java/io/zipcoder/polymorphism/CatTest.java @@ -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); + } +} diff --git a/src/test/java/io/zipcoder/polymorphism/DinosaurTest.java b/src/test/java/io/zipcoder/polymorphism/DinosaurTest.java new file mode 100644 index 0000000..9247e3d --- /dev/null +++ b/src/test/java/io/zipcoder/polymorphism/DinosaurTest.java @@ -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); + } +} diff --git a/src/test/java/io/zipcoder/polymorphism/DogTest.java b/src/test/java/io/zipcoder/polymorphism/DogTest.java new file mode 100644 index 0000000..45eaec0 --- /dev/null +++ b/src/test/java/io/zipcoder/polymorphism/DogTest.java @@ -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); + } +} diff --git a/src/test/java/io/zipcoder/polymorphism/PetTest.java b/src/test/java/io/zipcoder/polymorphism/PetTest.java new file mode 100644 index 0000000..5de294d --- /dev/null +++ b/src/test/java/io/zipcoder/polymorphism/PetTest.java @@ -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); + } + + +} \ No newline at end of file