From 1cfefa70b9f71c92de6509af3c6530597ede9904 Mon Sep 17 00:00:00 2001 From: Nick Date: Sat, 10 Jul 2021 19:28:31 -0400 Subject: [PATCH 1/2] Finished baseline, doing tests next --- README.md | 1 - pom.xml | 14 ++++- src/main/java/io/zipcoder/pets/Bird.java | 15 +++++ src/main/java/io/zipcoder/pets/Cat.java | 13 ++++ src/main/java/io/zipcoder/pets/Dog.java | 13 ++++ src/main/java/io/zipcoder/pets/Pets.java | 27 ++++++++ .../io/zipcoder/polymorphism/Console.java | 62 +++++++++++++++++++ .../java/io/zipcoder/polymorphism/IO.java | 24 +++++++ .../polymorphism/MainApplication.java | 4 ++ 9 files changed, 171 insertions(+), 2 deletions(-) create mode 100644 src/main/java/io/zipcoder/pets/Bird.java create mode 100644 src/main/java/io/zipcoder/pets/Cat.java create mode 100644 src/main/java/io/zipcoder/pets/Dog.java create mode 100644 src/main/java/io/zipcoder/pets/Pets.java create mode 100644 src/main/java/io/zipcoder/polymorphism/Console.java create mode 100644 src/main/java/io/zipcoder/polymorphism/IO.java 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..7f768d6 --- /dev/null +++ b/src/main/java/io/zipcoder/pets/Bird.java @@ -0,0 +1,15 @@ +package io.zipcoder.pets; + +public class Bird extends Pets { + + public Bird () { + this.name = "Alfred"; + } + + @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..6cf6ba3 --- /dev/null +++ b/src/main/java/io/zipcoder/pets/Cat.java @@ -0,0 +1,13 @@ +package io.zipcoder.pets; + +public class Cat extends Pets{ + + public Cat () { + this.name = name; + } + + @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..fb9571a --- /dev/null +++ b/src/main/java/io/zipcoder/pets/Dog.java @@ -0,0 +1,13 @@ +package io.zipcoder.pets; + +public class Dog extends Pets { + + public Dog () { + this.name = "Allen, I think?"; + } + + @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..538428d --- /dev/null +++ b/src/main/java/io/zipcoder/pets/Pets.java @@ -0,0 +1,27 @@ +package io.zipcoder.pets; + +public class Pets { + String name; + String speak; + + 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..b077317 --- /dev/null +++ b/src/main/java/io/zipcoder/polymorphism/Console.java @@ -0,0 +1,62 @@ +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; + } + } + System.out.println("What are their names?" + "\n"); + for (int i = 0; i < listOfPets.size(); i++) { + if (listOfPets.get(i) instanceof Cat) { + System.out.println("The name of your cat?"); + listOfPets.get(i).setName(IO.namesOfPets()); + } else if (listOfPets.get(i) instanceof Dog) { + System.out.println("The name of your dog?"); + listOfPets.get(i).setName(IO.namesOfPets()); + } else { + System.out.println("The name of your bird?"); + listOfPets.get(i).setName(IO.namesOfPets()); + } + } + for (int i = 0; i < listOfPets.size(); i++) { + if (listOfPets.get(i) instanceof Cat) { + System.out.println("Your cat, " + listOfPets.get(i).getName() + + ", wants to tell you " + listOfPets.get(i).speak()); + } else if (listOfPets.get(i) instanceof Dog) { + System.out.println("Your dog, " + listOfPets.get(i).getName() + + ", wants to tell you " + listOfPets.get(i).speak()); + } else { + System.out.println("Your bird, " + listOfPets.get(i).getName() + + ", wants to tell you " + listOfPets.get(i).speak()); + } + } + } + } +} + 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..91ca8fe --- /dev/null +++ b/src/main/java/io/zipcoder/polymorphism/IO.java @@ -0,0 +1,24 @@ +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; + } +} 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(); + } } From ad8e2b2e6287c51978409cb6aa7d2d2a21b8c169 Mon Sep 17 00:00:00 2001 From: Nick Date: Sat, 10 Jul 2021 20:39:15 -0400 Subject: [PATCH 2/2] Finished 'front end', waiting for a parter to run tests --- src/main/java/io/zipcoder/pets/Bird.java | 3 - src/main/java/io/zipcoder/pets/Cat.java | 3 - src/main/java/io/zipcoder/pets/Dog.java | 3 - src/main/java/io/zipcoder/pets/Pets.java | 10 +++ .../io/zipcoder/polymorphism/Console.java | 62 ++++++++++++------- .../java/io/zipcoder/polymorphism/IO.java | 5 ++ 6 files changed, 54 insertions(+), 32 deletions(-) diff --git a/src/main/java/io/zipcoder/pets/Bird.java b/src/main/java/io/zipcoder/pets/Bird.java index 7f768d6..835c509 100644 --- a/src/main/java/io/zipcoder/pets/Bird.java +++ b/src/main/java/io/zipcoder/pets/Bird.java @@ -2,9 +2,6 @@ public class Bird extends Pets { - public Bird () { - this.name = "Alfred"; - } @Override public String speak() { diff --git a/src/main/java/io/zipcoder/pets/Cat.java b/src/main/java/io/zipcoder/pets/Cat.java index 6cf6ba3..3b75a48 100644 --- a/src/main/java/io/zipcoder/pets/Cat.java +++ b/src/main/java/io/zipcoder/pets/Cat.java @@ -2,9 +2,6 @@ public class Cat extends Pets{ - public Cat () { - this.name = name; - } @Override public String speak() { diff --git a/src/main/java/io/zipcoder/pets/Dog.java b/src/main/java/io/zipcoder/pets/Dog.java index fb9571a..e7913eb 100644 --- a/src/main/java/io/zipcoder/pets/Dog.java +++ b/src/main/java/io/zipcoder/pets/Dog.java @@ -2,9 +2,6 @@ public class Dog extends Pets { - public Dog () { - this.name = "Allen, I think?"; - } @Override public String speak() { diff --git a/src/main/java/io/zipcoder/pets/Pets.java b/src/main/java/io/zipcoder/pets/Pets.java index 538428d..2cc2cb7 100644 --- a/src/main/java/io/zipcoder/pets/Pets.java +++ b/src/main/java/io/zipcoder/pets/Pets.java @@ -4,6 +4,16 @@ 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; } diff --git a/src/main/java/io/zipcoder/polymorphism/Console.java b/src/main/java/io/zipcoder/polymorphism/Console.java index b077317..2f474d1 100644 --- a/src/main/java/io/zipcoder/polymorphism/Console.java +++ b/src/main/java/io/zipcoder/polymorphism/Console.java @@ -31,32 +31,48 @@ public void run() { break; } } - System.out.println("What are their names?" + "\n"); - for (int i = 0; i < listOfPets.size(); i++) { - if (listOfPets.get(i) instanceof Cat) { - System.out.println("The name of your cat?"); - listOfPets.get(i).setName(IO.namesOfPets()); - } else if (listOfPets.get(i) instanceof Dog) { - System.out.println("The name of your dog?"); - listOfPets.get(i).setName(IO.namesOfPets()); - } else { - System.out.println("The name of your bird?"); - listOfPets.get(i).setName(IO.namesOfPets()); - } + } + 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()); } - for (int i = 0; i < listOfPets.size(); i++) { - if (listOfPets.get(i) instanceof Cat) { - System.out.println("Your cat, " + listOfPets.get(i).getName() + - ", wants to tell you " + listOfPets.get(i).speak()); - } else if (listOfPets.get(i) instanceof Dog) { - System.out.println("Your dog, " + listOfPets.get(i).getName() + - ", wants to tell you " + listOfPets.get(i).speak()); - } else { - System.out.println("Your bird, " + listOfPets.get(i).getName() + - ", wants to tell you " + listOfPets.get(i).speak()); - } + } + } + 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 index 91ca8fe..78bfd0b 100644 --- a/src/main/java/io/zipcoder/polymorphism/IO.java +++ b/src/main/java/io/zipcoder/polymorphism/IO.java @@ -21,4 +21,9 @@ public static Integer chooseThePet() { Integer chooseYourPet = scanner.nextInt(); return chooseYourPet; } + public static Integer thePetsAge() { + Scanner scanner = new Scanner(System.in); + Integer chooseYourPet = scanner.nextInt(); + return chooseYourPet; + } }