diff --git a/README.md b/README.md index 7ccd086..32fd32b 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Polymorphism Lab 1 +# Polymorphism Lab 1 -- Extended for Interfaces ## Objectives @@ -38,3 +38,14 @@ Use the tests provided as examples to write your own tests for other supported t 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. +## Interfaces + +Begin this lab by rewriting your UML according to the updated features. New unit tests will also be required. + +### Part 1: + +Starting from your completed polymorphism lab, add the `java.lang.Comparable` interface to your pet classes. Implement this interface so that `Arrays.sort()` or `Collections.sort()` (both existing static methods) will sort lists of your objects by name, breaking ties by class type. + +### Part 2: + +Create a new implementation of `java.util.Comparator` that will sort pet objects by type, breaking ties by name. diff --git a/pom.xml b/pom.xml index 93036ba..16a8444 100644 --- a/pom.xml +++ b/pom.xml @@ -7,6 +7,18 @@ io.zipcoder polymorphism 1.0-SNAPSHOT + + + + org.apache.maven.plugins + maven-compiler-plugin + + 8 + 8 + + + + \ No newline at end of file diff --git a/src/main/java/io/zipcoder/polymorphism/Bird.java b/src/main/java/io/zipcoder/polymorphism/Bird.java new file mode 100644 index 0000000..8a7a748 --- /dev/null +++ b/src/main/java/io/zipcoder/polymorphism/Bird.java @@ -0,0 +1,22 @@ +package io.zipcoder.polymorphism; +import java.lang.Comparable; +import java.util.Collections; +import java.util.Comparator; + +public class Bird extends Pet{ + + public Bird(String name){ + super(name); + this.type = "bird"; + } + + @Override + public String speak() { + return "Chirp!"; + } + + public int compareTo(Pet o){ + return this.getName().compareTo(o.getName()); + } + +} 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..d49ecb8 --- /dev/null +++ b/src/main/java/io/zipcoder/polymorphism/Cat.java @@ -0,0 +1,20 @@ +package io.zipcoder.polymorphism; +import java.lang.Comparable; + +public class Cat extends Pet{ + + public Cat(String name) { + super(name); + this.type = "cat"; + } + + @Override + public String speak() { + return "Meow!"; + } + + public int compareTo(Pet o){ + return this.getName().compareTo(o.getName()); + } + +} diff --git a/src/main/java/io/zipcoder/polymorphism/CreatePet.java b/src/main/java/io/zipcoder/polymorphism/CreatePet.java new file mode 100644 index 0000000..455c76d --- /dev/null +++ b/src/main/java/io/zipcoder/polymorphism/CreatePet.java @@ -0,0 +1,21 @@ +package io.zipcoder.polymorphism; + +import java.util.ArrayList; + +public class CreatePet { + + /* method to createPet */ + public void createPet(ArrayList petList, String petType, String petName) { + if (petType.equals("dog")) { + Dog dog = new Dog(petName); + petList.add(dog); + } else if (petType.equals("cat")) { + Cat cat = new Cat(petName); + petList.add(cat); + } else if (petType.equals("bird")) { + Bird bird = new Bird(petName); + petList.add(bird); + } + } + +} 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..0d883dd --- /dev/null +++ b/src/main/java/io/zipcoder/polymorphism/Dog.java @@ -0,0 +1,20 @@ +package io.zipcoder.polymorphism; +import java.lang.Comparable; + +public class Dog extends Pet{ + + public Dog(String name) { + super(name); + this.type = "dog"; + } + + @Override + public String speak() { + return "Woof!"; + } + + public int compareTo(Pet o){ + return this.getName().compareTo(o.getName()); + } + +} diff --git a/src/main/java/io/zipcoder/polymorphism/MainApplication.java b/src/main/java/io/zipcoder/polymorphism/MainApplication.java index ea9281e..06719f5 100644 --- a/src/main/java/io/zipcoder/polymorphism/MainApplication.java +++ b/src/main/java/io/zipcoder/polymorphism/MainApplication.java @@ -1,7 +1,16 @@ package io.zipcoder.polymorphism; -/** - * Created by leon on 11/6/17. - */ +import java.util.*; + + public class MainApplication { + + public static void main(String[] args) { + + Mediator mediator = new Mediator(); + mediator.handleInput(); + mediator.handleOutputs(); + + } + } diff --git a/src/main/java/io/zipcoder/polymorphism/Mediator.java b/src/main/java/io/zipcoder/polymorphism/Mediator.java new file mode 100644 index 0000000..da6b70d --- /dev/null +++ b/src/main/java/io/zipcoder/polymorphism/Mediator.java @@ -0,0 +1,33 @@ +package io.zipcoder.polymorphism; + +import com.sun.tools.internal.xjc.reader.xmlschema.bindinfo.BIConversion; + +import java.util.ArrayList; +import java.util.Arrays; + +public class Mediator { + ArrayList petList = new ArrayList(); + + public void handleInput() { + UserInput input = new UserInput(); + int numberPets = input.getNumberOfPets(); + System.out.println(numberPets); + + for (int i=0; i < numberPets; i++){ + String petType = input.getPetType(i); + String petName = input.getPetName(); + CreatePet newPet = new CreatePet(); + newPet.createPet(petList, petType, petName); + System.out.println( petType + " " + petName + " created"); + } + System.out.println(); + } + + public void handleOutputs() { + Output output = new Output(); + output.petSpeak(petList); + output.listSortedByName(petList); + output.listSortedByType(petList); + } + +} diff --git a/src/main/java/io/zipcoder/polymorphism/Output.java b/src/main/java/io/zipcoder/polymorphism/Output.java new file mode 100644 index 0000000..91d5136 --- /dev/null +++ b/src/main/java/io/zipcoder/polymorphism/Output.java @@ -0,0 +1,35 @@ +package io.zipcoder.polymorphism; + +import java.util.ArrayList; + +public class Output { + sortBy mySort = new sortBy(); + + // method to print petList sorted by Name + public void listSortedByName(ArrayList petList){ + System.out.println("Pets sorted by Name: "); + for (Pet each: mySort.sortByName(petList)){ + System.out.println(each.getName()); + } + System.out.println(); + } + + //method to print petList sorted by Type + public void listSortedByType(ArrayList petList){ + System.out.println("Pets sorted by Type: "); + for (Pet each: mySort.sortByType(petList)){ + System.out.println(each.getName() + " is a " + each.getType()); + } + System.out.println(); + } + + //method to print what pets say + public void petSpeak(ArrayList petList) { + System.out.println("Pets say: "); + for (Pet each : petList) { + System.out.println(each.getName() + " says " + each.speak()); + } + System.out.println(); + } + +} 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..86b84c1 --- /dev/null +++ b/src/main/java/io/zipcoder/polymorphism/Pet.java @@ -0,0 +1,40 @@ +package io.zipcoder.polymorphism; + +import java.util.ArrayList; + +public abstract class Pet implements Comparable { + String name; + String type; + + public Pet(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + public String speak() { + return "animal talk!"; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public abstract int compareTo(Pet o); + + @Override + public String toString() { + return "Pet list: " + + "name: " + getName() + ' ' + getType() + '\n'; + } + +} diff --git a/src/main/java/io/zipcoder/polymorphism/UserInput.java b/src/main/java/io/zipcoder/polymorphism/UserInput.java new file mode 100644 index 0000000..fbce625 --- /dev/null +++ b/src/main/java/io/zipcoder/polymorphism/UserInput.java @@ -0,0 +1,50 @@ +package io.zipcoder.polymorphism; + +import java.util.Scanner; + +public class UserInput { + Scanner input = new Scanner(System.in); + + /* method to getNumberOfPets from user */ + public Integer getNumberOfPets() { + int numberOfPets = 0; + System.out.print("How many pets do you have: "); + while (numberOfPets <= 0) { + while (!input.hasNextInt()) { + System.out.println("That's not a valid number!"); + input.next(); + } + numberOfPets = input.nextInt(); + if (numberOfPets == 0) { + System.out.println("That's too bad, pets are great!"); + break; + } else if (numberOfPets < 0) { + System.out.println("That's not a valid number!"); + } + } + System.out.println("Thanks."); + return numberOfPets; + } + + /* method to prompt for petType */ + public String getPetType(int i) { + String petType = ""; + System.out.println("Is pet " + (i+1) + " a dog, cat, or bird ?"); + while ((!((petType.equals("dog")) || (petType.equals("cat")) || (petType.equals("bird")) ))) { + petType = input.next().toLowerCase(); + if ((!((petType.equals("dog")) || (petType.equals("cat")) || (petType.equals("bird"))))) { + System.out.println("That is not a valid pet type."); + System.out.println("Is pet a dog, cat, or bird ?"); + } + } + return petType; + } + + /* method to prompt for petName */ + public String getPetName() { + System.out.print("Enter your pet's name:\n"); + String petName = input.next(); + return petName; + } + +} diff --git a/src/main/java/io/zipcoder/polymorphism/sortBy.java b/src/main/java/io/zipcoder/polymorphism/sortBy.java new file mode 100644 index 0000000..621afc9 --- /dev/null +++ b/src/main/java/io/zipcoder/polymorphism/sortBy.java @@ -0,0 +1,23 @@ +package io.zipcoder.polymorphism; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; + +public class sortBy { + + public ArrayList sortByName(ArrayList petList){ + Comparator petNameComparator + = Comparator.comparing(Pet::getName); + Collections.sort(petList, petNameComparator); + return petList; + } + + public ArrayList sortByType(ArrayList petList){ + Comparator petTypeComparator + = Comparator.comparing(Pet::getType); + Collections.sort(petList, petTypeComparator); + return petList; + } + +}