diff --git a/pom.xml b/pom.xml index 62dbb86..22e55fc 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,19 @@ io.zipcoder polymorphism-1 0.0.1-SNAPSHOT - jar + + + + org.apache.maven.plugins + maven-compiler-plugin + + 7 + 7 + + + + + jar interfaces-1 http://maven.apache.org diff --git a/src/main/java/io/zipcoder/polymorphism/Animal.java b/src/main/java/io/zipcoder/polymorphism/Animal.java new file mode 100644 index 0000000..b9a1461 --- /dev/null +++ b/src/main/java/io/zipcoder/polymorphism/Animal.java @@ -0,0 +1,5 @@ +package io.zipcoder.polymorphism; + +public interface Animal { + String speak(); +} 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..1c0b105 --- /dev/null +++ b/src/main/java/io/zipcoder/polymorphism/Cat.java @@ -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!"; + } +} diff --git a/src/main/java/io/zipcoder/polymorphism/Console.java b/src/main/java/io/zipcoder/polymorphism/Console.java new file mode 100644 index 0000000..9bd4c39 --- /dev/null +++ b/src/main/java/io/zipcoder/polymorphism/Console.java @@ -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; + } +} + 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..ee6b9b4 --- /dev/null +++ b/src/main/java/io/zipcoder/polymorphism/Dog.java @@ -0,0 +1,8 @@ +package io.zipcoder.polymorphism; + +public class Dog extends Pet{ + @Override + public String speak() { + return "Bark!"; + } +} diff --git a/src/main/java/io/zipcoder/polymorphism/MainApplication.java b/src/main/java/io/zipcoder/polymorphism/MainApplication.java index 668c627..b944d0b 100644 --- a/src/main/java/io/zipcoder/polymorphism/MainApplication.java +++ b/src/main/java/io/zipcoder/polymorphism/MainApplication.java @@ -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); + } } diff --git a/src/main/java/io/zipcoder/polymorphism/Pet.java b/src/main/java/io/zipcoder/polymorphism/Pet.java new file mode 100644 index 0000000..7dfdb19 --- /dev/null +++ b/src/main/java/io/zipcoder/polymorphism/Pet.java @@ -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"; + } +} diff --git a/src/main/java/io/zipcoder/polymorphism/PetOwner.java b/src/main/java/io/zipcoder/polymorphism/PetOwner.java new file mode 100644 index 0000000..c1e3947 --- /dev/null +++ b/src/main/java/io/zipcoder/polymorphism/PetOwner.java @@ -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 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(); + } + + public Pet[] getPets() { + return petsList.toArray(new Pet [0]); + } + + public void addPet(Pet pet) { + petsList.add(pet); + } +} diff --git a/src/main/java/io/zipcoder/polymorphism/Rabbit.java b/src/main/java/io/zipcoder/polymorphism/Rabbit.java new file mode 100644 index 0000000..fc202cb --- /dev/null +++ b/src/main/java/io/zipcoder/polymorphism/Rabbit.java @@ -0,0 +1,8 @@ +package io.zipcoder.polymorphism; + +public class Rabbit extends Pet{ + @Override + public String speak() { + return "Ribbit!"; + } +} 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..154bbea --- /dev/null +++ b/src/test/java/io/zipcoder/polymorphism/CatTest.java @@ -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); + } + + +} \ No newline at end of file diff --git a/src/test/java/io/zipcoder/polymorphism/ConsoleTest.java b/src/test/java/io/zipcoder/polymorphism/ConsoleTest.java new file mode 100644 index 0000000..7a04ba9 --- /dev/null +++ b/src/test/java/io/zipcoder/polymorphism/ConsoleTest.java @@ -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; + + } + + + +} +*/ \ No newline at end of file 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..6395520 --- /dev/null +++ b/src/test/java/io/zipcoder/polymorphism/DogTest.java @@ -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); + } + +} + diff --git a/src/test/java/io/zipcoder/polymorphism/PetOwnerTest.java b/src/test/java/io/zipcoder/polymorphism/PetOwnerTest.java new file mode 100644 index 0000000..f2fd35d --- /dev/null +++ b/src/test/java/io/zipcoder/polymorphism/PetOwnerTest.java @@ -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 + + + + + } +} 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..1f0dd52 --- /dev/null +++ b/src/test/java/io/zipcoder/polymorphism/PetTest.java @@ -0,0 +1,9 @@ +package io.zipcoder.polymorphism; + +import org.junit.Test; + +public class PetTest { + + + +} diff --git a/src/test/java/io/zipcoder/polymorphism/RabbitTest.java b/src/test/java/io/zipcoder/polymorphism/RabbitTest.java new file mode 100644 index 0000000..b7e6e5b --- /dev/null +++ b/src/test/java/io/zipcoder/polymorphism/RabbitTest.java @@ -0,0 +1,30 @@ +package io.zipcoder.polymorphism; + +import org.junit.Assert; +import org.junit.Test; + +public class RabbitTest { + + @Test + public void testSetRabbitName() { + //given + Rabbit rabbit = new Rabbit(); + String expectedName = "Momo"; + //when + rabbit.setName(expectedName); + String actual = rabbit.getName(); + //then + Assert.assertEquals(expectedName, actual); + } + + @Test + public void rabbitSpeakTest() { + //given + Rabbit rabbit = new Rabbit(); + //then + String actual = rabbit.speak(); + //when + Assert.assertEquals("Ribbit!", actual); + } + +}