diff --git a/README.md b/README.md index 7ccd086..81545ed 100644 --- a/README.md +++ b/README.md @@ -37,4 +37,3 @@ Use the tests provided as examples to write your own tests for other supported t ### 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. - 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/pets/Bird.java b/src/main/java/io/zipcoder/pets/Bird.java new file mode 100644 index 0000000..835c509 --- /dev/null +++ b/src/main/java/io/zipcoder/pets/Bird.java @@ -0,0 +1,12 @@ +package io.zipcoder.pets; + +public class Bird extends Pets { + + + @Override + public String speak() { + return "Tweet"; + } + + +} 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..3b75a48 --- /dev/null +++ b/src/main/java/io/zipcoder/pets/Cat.java @@ -0,0 +1,10 @@ +package io.zipcoder.pets; + +public class Cat extends Pets{ + + + @Override + public String speak() { + return "Meeeeeeeeow"; + } +} 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..e7913eb --- /dev/null +++ b/src/main/java/io/zipcoder/pets/Dog.java @@ -0,0 +1,10 @@ +package io.zipcoder.pets; + +public class Dog extends Pets { + + + @Override + public String speak() { + return "Bark"; + } +} diff --git a/src/main/java/io/zipcoder/pets/Pets.java b/src/main/java/io/zipcoder/pets/Pets.java new file mode 100644 index 0000000..2cc2cb7 --- /dev/null +++ b/src/main/java/io/zipcoder/pets/Pets.java @@ -0,0 +1,37 @@ +package io.zipcoder.pets; + +public class Pets { + String name; + String speak; + + public Integer getAge() { + return age; + } + + public void setAge(Integer age) { + this.age = age; + } + + Integer age; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getSpeak() { + return speak; + } + + public void setSpeak(String speak) { + this.speak = speak; + } + + public String speak () { + return speak; + } + +} 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..2f474d1 --- /dev/null +++ b/src/main/java/io/zipcoder/polymorphism/Console.java @@ -0,0 +1,78 @@ +package io.zipcoder.polymorphism; +import io.zipcoder.pets.Bird; +import io.zipcoder.pets.Cat; +import io.zipcoder.pets.Dog; +import io.zipcoder.pets.Pets; +import java.util.ArrayList; +import java.util.List; + +public class Console { + + List listOfPets = new ArrayList<>(); + private static Integer counter = 0; + + public void run() { + System.out.println("Hello there! How many pets do you have?"); + Integer amountOfPets = IO.numberOfPets(); + while (counter < amountOfPets) { + for (int i = 0; i < amountOfPets; i++) { + counter++; + System.out.println("What kind of pet do you have?"); + System.out.println("1. Cat" + "\n" + "2. Dog" + "\n" + "3. Bird"); + switch (IO.chooseThePet()) { + case 1 : + listOfPets.add(i, new Cat()); + break; + case 2 : + listOfPets.add(i, new Dog()); + break; + case 3 : + listOfPets.add(i, new Bird()); + break; + } + } + } + assigningNames(); + assigningAges(); + System.out.println("Wow! You have " + listOfPets.size() + " pets?! That's a lot!" + + "\n" + "Thank you for telling me about..." + "\n"); + printingTheList(); + } + public void assigningNames () { + System.out.println("What are their names?" + "\n"); + for (Pets listOfPet : listOfPets) { + if (listOfPet instanceof Cat) { + System.out.println("The name of your cat?"); + listOfPet.setName(IO.namesOfPets()); + } else if (listOfPet instanceof Dog) { + System.out.println("The name of your dog?"); + listOfPet.setName(IO.namesOfPets()); + } else { + System.out.println("The name of your bird?"); + listOfPet.setName(IO.namesOfPets()); + } + } + } + public void printingTheList () { + for (Pets listOfPet : listOfPets) { + if (listOfPet instanceof Cat) { + System.out.println("Your cat, " + listOfPet.getName() + ", who is " + listOfPet.getAge() + + " years old, wants to tell you " + listOfPet.speak()); + } else if (listOfPet instanceof Dog) { + System.out.println("Your dog, " + listOfPet.getName() + ", who is " + listOfPet.getAge() + + " years old, wants to tell you " + listOfPet.speak()); + } else { + System.out.println("Your bird, " + listOfPet.getName() + ", who is " + listOfPet.getAge() + + " years old, wants to tell you " + listOfPet.speak()); + } + } + } + public void assigningAges () { + System.out.println("What are their Ages?" + "\n"); + for (Pets eachPet : listOfPets) { + System.out.println("How old is " + eachPet.getName() + "?"); + eachPet.setAge(IO.thePetsAge()); + } + } +} + diff --git a/src/main/java/io/zipcoder/polymorphism/IO.java b/src/main/java/io/zipcoder/polymorphism/IO.java new file mode 100644 index 0000000..78bfd0b --- /dev/null +++ b/src/main/java/io/zipcoder/polymorphism/IO.java @@ -0,0 +1,29 @@ +package io.zipcoder.polymorphism; + +import java.util.Scanner; + +public class IO { + + public static Integer numberOfPets() { + Scanner scanner = new Scanner(System.in); + Integer userInitialInput = scanner.nextInt(); + return userInitialInput; + } + + public static String namesOfPets() { + Scanner scanner = new Scanner(System.in); + String nameOfPet = scanner.next(); + return nameOfPet; + } + + public static Integer chooseThePet() { + Scanner scanner = new Scanner(System.in); + Integer chooseYourPet = scanner.nextInt(); + return chooseYourPet; + } + public static Integer thePetsAge() { + Scanner scanner = new Scanner(System.in); + Integer chooseYourPet = scanner.nextInt(); + return chooseYourPet; + } +} diff --git a/src/main/java/io/zipcoder/polymorphism/MainApplication.java b/src/main/java/io/zipcoder/polymorphism/MainApplication.java index 668c627..d35e317 100644 --- a/src/main/java/io/zipcoder/polymorphism/MainApplication.java +++ b/src/main/java/io/zipcoder/polymorphism/MainApplication.java @@ -1,4 +1,8 @@ package io.zipcoder.polymorphism; public class MainApplication { + public static void main(String[] args) { + Console console = new Console(); + console.run(); + } }