diff --git a/README.md b/README.md index 7ccd086..de8a512 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ In this lab you will practice creating a simple Java program designed to make us ## Unit Test -Select a partner from your tribe; You will each write tests for the requirements below, but your partner must develop against your tests and vice versa. Be sure to use the `io.zipcoder.pets` package for your Pet classes to allow tests to execute properly. +Select a partner from your tribe; You will each write tests for the requirements below, but your partner must develop against your tests and vice versa. Be sure to use the `io.zipcoder.io.zipcoder.polymorphism.polymorphism.pets` package for your Pet classes to allow tests to execute properly. **Hint:** *An easy way to achieve this is for each partner to set up a GitHub repository for this lab, and add the other partner as a collaborator with write access (in the repository settings).* @@ -20,21 +20,21 @@ Select a partner from your tribe; You will each write tests for the requirements ### Part 1: -Create a program that asks the user how many pets they have. Once you know how many pets they have, ask them what kind of pet each one is, along with each pet's name. For now your program should just hold onto the user input and print out the list at the end; we'll modify this in part 3. +Create a program that asks the user how many io.zipcoder.polymorphism.polymorphism.pets they have. Once you know how many io.zipcoder.polymorphism.polymorphism.pets they have, ask them what kind of pet each one is, along with each pet's name. For now your program should just hold onto the user input and print out the list at the end; we'll modify this in part 3. ### Part 2: Create a Pet class, and a subclass for each type of pet that you want your program to support. Your classes should follow the following requirements: -- You must support at least three types of pets. +- You must support at least three types of io.zipcoder.polymorphism.polymorphism.pets. - Dog must be one of the types you support. - Cat must be one of the types you support. - The Pet class must have a `speak` method that each subclass overrides. - The Pet class must have a `name` field with setters and getters. -Use the tests provided as examples to write your own tests for other supported types of pets. +Use the tests provided as examples to write your own tests for other supported types of io.zipcoder.polymorphism.polymorphism.pets. ### Part 3: -Modify your program from part 1 to use the Pet class and its subclasses. Keep a list of the pets your user lists and at the end of the program print out a list of their names and what they say when they speak. +Modify your program from part 1 to use the Pet class and its subclasses. Keep a list of the io.zipcoder.polymorphism.polymorphism.pets your user lists and at the end of the program print out a list of their names and what they say when they speak. diff --git a/pom.xml b/pom.xml index 93036ba..fe4161e 100644 --- a/pom.xml +++ b/pom.xml @@ -5,8 +5,16 @@ 4.0.0 io.zipcoder - polymorphism + io.zipcoder.polymorphism.polymorphism 1.0-SNAPSHOT + + + junit + junit + 4.12 + test + + \ No newline at end of file diff --git a/src/main/java/io/zipcoder/pets/Animal.java b/src/main/java/io/zipcoder/pets/Animal.java new file mode 100644 index 0000000..d3fbde7 --- /dev/null +++ b/src/main/java/io/zipcoder/pets/Animal.java @@ -0,0 +1,5 @@ +package io.zipcoder.pets; + +public interface Animal { + String speak(); +} diff --git a/src/main/java/io/zipcoder/pets/Cat.java b/src/main/java/io/zipcoder/pets/Cat.java new file mode 100644 index 0000000..485d2c7 --- /dev/null +++ b/src/main/java/io/zipcoder/pets/Cat.java @@ -0,0 +1,16 @@ +package io.zipcoder.pets; + +public class Cat extends Pet { + + public Cat(){ + super("Furball"); + } + + public Cat(String name){ + super(name); + } + + public String speak() { + return "meow ~"; + } +} diff --git a/src/main/java/io/zipcoder/pets/Dog.java b/src/main/java/io/zipcoder/pets/Dog.java new file mode 100644 index 0000000..8c6b763 --- /dev/null +++ b/src/main/java/io/zipcoder/pets/Dog.java @@ -0,0 +1,14 @@ +package io.zipcoder.pets; + +public class Dog extends Pet { + + public Dog(){ + super("Clifford"); + } + public Dog(String name) { + super(name); + } + public String speak() { + return "bow-wow"; + } +} diff --git a/src/main/java/io/zipcoder/pets/Pet.java b/src/main/java/io/zipcoder/pets/Pet.java new file mode 100644 index 0000000..5c5a9e3 --- /dev/null +++ b/src/main/java/io/zipcoder/pets/Pet.java @@ -0,0 +1,19 @@ +package io.zipcoder.pets; + +public abstract class Pet implements Animal { + private String name; + protected Pet(){} + + protected Pet(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + +} diff --git a/src/main/java/io/zipcoder/pets/Platypus.java b/src/main/java/io/zipcoder/pets/Platypus.java new file mode 100644 index 0000000..db39329 --- /dev/null +++ b/src/main/java/io/zipcoder/pets/Platypus.java @@ -0,0 +1,14 @@ +package io.zipcoder.pets; + +public class Platypus extends Pet { + + public Platypus(){ + super("Perry"); + } + public Platypus(String name) { + super(name); + } + public String speak() { + return "Perry the platypus noises"; + } +} diff --git a/src/main/java/io/zipcoder/polymorphism/MainApplication.java b/src/main/java/io/zipcoder/polymorphism/MainApplication.java index ea9281e..5725794 100644 --- a/src/main/java/io/zipcoder/polymorphism/MainApplication.java +++ b/src/main/java/io/zipcoder/polymorphism/MainApplication.java @@ -1,7 +1,96 @@ package io.zipcoder.polymorphism; +import io.zipcoder.pets.Cat; +import io.zipcoder.pets.Dog; +import io.zipcoder.pets.Pet; +import io.zipcoder.pets.Platypus; +import java.util.Scanner; /** * Created by leon on 11/6/17. */ public class MainApplication { + + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + Integer amount = askNumberOfPet(scanner); + Pet[] pets = askTypesAndNamesToPet(scanner,amount); + System.out.println(petSpeaks(pets)); + } + + public static String printPetList(Integer numberOfPets, String[] typeOfPets, String[] petNames) { + + String result = ""; + for(Integer index = 0; index < numberOfPets; index++) + result += String.format("%s %s\n", typeOfPets[index], petNames[index]); + + return result; + } + + + public static String petSpeaks(Pet[] pets) { + String result = ""; + + for(Pet pet: pets) + result += String.format("%s the %s says\n\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 Platypus) + return "platypus"; + else + return null; + } + + private static Integer askNumberOfPet(Scanner scanner){ + System.out.println("How many io.zipcoder.polymorphism.polymorphism.pets do you have?"); + return scanner.nextInt(); + } + + private static String askTypesAndNamesToString(Scanner scanner, Integer amount){ + String[] typeOfPets = new String[amount]; + String[] petNames = new String[amount]; + + for(Integer index = 0; index < amount; index++) { + System.out.println("What kind of pet is pet " + (index+1) + "?"); + typeOfPets[index] = scanner.next(); + System.out.println("What is the the name of pet " + (index+1) + "?"); + petNames[index] = scanner.next(); + } + + return printPetList(amount, typeOfPets, petNames); + } + + private static Pet[] askTypesAndNamesToPet(Scanner scanner, Integer amount){ + String[] typeOfPets = new String[amount]; + String[] petNames = new String[amount]; + Pet[] pets = new Pet[amount]; + + for(Integer index = 0; index < amount; 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("platypus")) + return new Platypus(); + else + return null; + } } diff --git a/src/test/java/io/zipcoder/pets/CatTest.java b/src/test/java/io/zipcoder/pets/CatTest.java new file mode 100644 index 0000000..24fd407 --- /dev/null +++ b/src/test/java/io/zipcoder/pets/CatTest.java @@ -0,0 +1,36 @@ +package io.zipcoder.pets; +import org.junit.Assert; +import org.junit.Test; + +public class CatTest { + + @Test + public void nullaryConstructorTest() { + String expectedName = "Furball"; + Cat cat = new Cat(); + + String actualName = cat.getName(); + + Assert.assertEquals(expectedName, actualName); + } + + @Test + public void constructorWithNameTest() { + String expectedName = "SirMeowsAlot"; + Cat cat = new Cat(expectedName); + + String actualName = cat.getName(); + + Assert.assertEquals(expectedName, actualName); + } + + @Test + public void speakTest() { + Cat cat = new Cat(); + String expected = "meow ~"; + + String actual = cat.speak(); + + Assert.assertEquals(expected, actual); + } +} diff --git a/src/test/java/io/zipcoder/pets/DogTest.java b/src/test/java/io/zipcoder/pets/DogTest.java new file mode 100644 index 0000000..740199c --- /dev/null +++ b/src/test/java/io/zipcoder/pets/DogTest.java @@ -0,0 +1,36 @@ +package io.zipcoder.pets; +import org.junit.Assert; +import org.junit.Test; + +public class DogTest { + + @Test + public void nullaryConstructorTest() { + String expectedName = "Clifford"; + Dog dog = new Dog(); + + String actualName = dog.getName(); + + Assert.assertEquals(expectedName, actualName); + } + + @Test + public void constructorWithNameTest() { + String expectedName = "SirBarksAlots"; + Dog dog = new Dog(expectedName); + + String actualName = dog.getName(); + + Assert.assertEquals(expectedName, actualName); + } + + @Test + public void speakTest() { + Dog dog = new Dog(); + String expected = "bow-wow"; + + String actual = dog.speak(); + + Assert.assertEquals(expected, actual); + } +} diff --git a/src/test/java/io/zipcoder/pets/PetTest.java b/src/test/java/io/zipcoder/pets/PetTest.java new file mode 100644 index 0000000..a5ff669 --- /dev/null +++ b/src/test/java/io/zipcoder/pets/PetTest.java @@ -0,0 +1,43 @@ +package io.zipcoder.pets; +import org.junit.Test; +import org.junit.Assert; + +public class PetTest { + + @Test + public void testDogInheritance() { + Pet p = new Dog(); + Assert.assertTrue(p instanceof Pet); + } + + @Test + public void testCatInheritance() { + Pet p = new Cat(); + Assert.assertTrue(p instanceof Pet); + } + + @Test + public void testPlatypusInheritance() { + Pet p = new Platypus(); + Assert.assertTrue(p instanceof Pet); + } + + @Test + public void testDogImplementation() { + Pet p = new Dog(); + Assert.assertTrue(p instanceof Animal); + } + + @Test + public void testCatImplementation() { + Pet p = new Cat(); + Assert.assertTrue(p instanceof Animal); + } + + @Test + public void testPlatypusImplementation() { + Pet p = new Platypus(); + Assert.assertTrue(p instanceof Animal); + } + +} diff --git a/src/test/java/io/zipcoder/pets/PlatypusTest.java b/src/test/java/io/zipcoder/pets/PlatypusTest.java new file mode 100644 index 0000000..b2a4930 --- /dev/null +++ b/src/test/java/io/zipcoder/pets/PlatypusTest.java @@ -0,0 +1,36 @@ +package io.zipcoder.pets; +import org.junit.Assert; +import org.junit.Test; + +public class PlatypusTest { + + @Test + public void nullaryConstructorTest() { + String expectedName = "Perry"; + Platypus platypus = new Platypus(); + + String actualName = platypus.getName(); + + Assert.assertEquals(expectedName, actualName); + } + + @Test + public void constructorWithNameTest() { + String expectedName = "SirBarksAlots"; + Platypus platypus = new Platypus(expectedName); + + String actualName = platypus.getName(); + + Assert.assertEquals(expectedName, actualName); + } + + @Test + public void speakTest() { + Platypus platypus = new Platypus(); + String expected = "bow-wow"; + + String actual = platypus.speak(); + + Assert.assertEquals(expected, actual); + } +} diff --git a/src/test/java/io/zipcoder/polymorphism/MainApplicationTest.java b/src/test/java/io/zipcoder/polymorphism/MainApplicationTest.java index 7181623..3964337 100644 --- a/src/test/java/io/zipcoder/polymorphism/MainApplicationTest.java +++ b/src/test/java/io/zipcoder/polymorphism/MainApplicationTest.java @@ -1,7 +1,41 @@ package io.zipcoder.polymorphism; +import io.zipcoder.pets.Cat; +import io.zipcoder.pets.Dog; +import io.zipcoder.pets.Pet; +import io.zipcoder.pets.Platypus; +import org.junit.Assert; +import org.junit.Test; /** * Created by leon on 11/6/17. */ public class MainApplicationTest { + + @Test + public void printPetListTest() { + //Given + Integer numberOfPets = 3; + String[] typeOfPets = {"Cat", "Dog", "Platypus"}; + String[] petNames = {"Luna", "Bruno", "Ben"}; + + String expected = "Cat Luna\nDog Bruno\nPlatypus Ben\n"; + String actual = MainApplication.printPetList(numberOfPets, typeOfPets, petNames); + Assert.assertEquals(expected, actual); + + } + + @Test + public void petSpeaksTest() { + + Cat cat = new Cat("Mittens"); + Dog dog = new Dog("Aika"); + Platypus platypus = new Platypus("Sam"); + Pet[] pets ={cat,dog,platypus}; + + String expected = "Mittens the cat says\n\tmeow ~\nAika the dog says\n\tbow-wow\nSam the platypus says\n\tPerry the platypus noises"; + String actual = MainApplication.petSpeaks(pets); + Assert.assertEquals(expected, actual); + + } + }