Skip to content

Donna rework #52

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Polymorphism Lab 1
# Polymorphism Lab 1 -- Extended for Interfaces

## Objectives

Expand Down Expand Up @@ -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.
12 changes: 12 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@
<groupId>io.zipcoder</groupId>
<artifactId>polymorphism</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>


</project>
22 changes: 22 additions & 0 deletions src/main/java/io/zipcoder/polymorphism/Bird.java
Original file line number Diff line number Diff line change
@@ -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());
}

}
20 changes: 20 additions & 0 deletions src/main/java/io/zipcoder/polymorphism/Cat.java
Original file line number Diff line number Diff line change
@@ -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());
}

}
21 changes: 21 additions & 0 deletions src/main/java/io/zipcoder/polymorphism/CreatePet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.zipcoder.polymorphism;

import java.util.ArrayList;

public class CreatePet {

/* method to createPet */
public void createPet(ArrayList<Pet> 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);
}
}

}
20 changes: 20 additions & 0 deletions src/main/java/io/zipcoder/polymorphism/Dog.java
Original file line number Diff line number Diff line change
@@ -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());
}

}
15 changes: 12 additions & 3 deletions src/main/java/io/zipcoder/polymorphism/MainApplication.java
Original file line number Diff line number Diff line change
@@ -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();

}

}
33 changes: 33 additions & 0 deletions src/main/java/io/zipcoder/polymorphism/Mediator.java
Original file line number Diff line number Diff line change
@@ -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<Pet> petList = new ArrayList<Pet>();

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);
}

}
35 changes: 35 additions & 0 deletions src/main/java/io/zipcoder/polymorphism/Output.java
Original file line number Diff line number Diff line change
@@ -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<Pet> 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<Pet> 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<Pet> petList) {
System.out.println("Pets say: ");
for (Pet each : petList) {
System.out.println(each.getName() + " says " + each.speak());
}
System.out.println();
}

}
40 changes: 40 additions & 0 deletions src/main/java/io/zipcoder/polymorphism/Pet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package io.zipcoder.polymorphism;

import java.util.ArrayList;

public abstract class Pet implements Comparable<Pet> {
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';
}

}
50 changes: 50 additions & 0 deletions src/main/java/io/zipcoder/polymorphism/UserInput.java
Original file line number Diff line number Diff line change
@@ -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;
}

}
23 changes: 23 additions & 0 deletions src/main/java/io/zipcoder/polymorphism/sortBy.java
Original file line number Diff line number Diff line change
@@ -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<Pet> sortByName(ArrayList<Pet> petList){
Comparator<Pet> petNameComparator
= Comparator.comparing(Pet::getName);
Collections.sort(petList, petNameComparator);
return petList;
}

public ArrayList<Pet> sortByType(ArrayList<Pet> petList){
Comparator<Pet> petTypeComparator
= Comparator.comparing(Pet::getType);
Collections.sort(petList, petTypeComparator);
return petList;
}

}