Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,21 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>io.zipcoder</groupId>
<artifactId>polymorphism</artifactId>
<groupId>com.zipcodewilmington</groupId>
<artifactId>loop_labs</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>

</project>
24 changes: 24 additions & 0 deletions src/main/java/io/zipcoder/polymorphism/Bird.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.zipcoder.polymorphism;

public class Bird extends Pet {
public Bird(String name){
super(name);
}

@Override
public String speak() {
return "Chirp Chirp";
}

@Override
public int compareTo(Pet o) {
// do this for remaining pets
if (this.getName().compareTo(o.getName()) == 0) {
this.getClass().getName(); // class name of this pet
o.getClass().getName(); // class name pet o
return this.getClass().getName().compareTo(o.getClass().getName());
} else {
return this.getName().compareTo(o.getName());
}
}
}
25 changes: 25 additions & 0 deletions src/main/java/io/zipcoder/polymorphism/Cat.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.zipcoder.polymorphism;

public class Cat extends Pet {
public Cat(String name){
super(name);
}

@Override
public String speak() {
return "Meow";
}

@Override
public int compareTo(Pet o) {
// do this for remaining pets
if (this.getName().compareTo(o.getName()) == 0) {
this.getClass().getName(); // class name of this pet
o.getClass().getName(); // class name pet o
return this.getClass().getName().compareTo(o.getClass().getName());
} else {
return this.getName().compareTo(o.getName());
}
}

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

import java.util.Scanner;

public class Console {

public static void print(String output, Object... args) {
System.out.printf(output, args);
}

public static void println(String output, Object... args) {
print(output + "\n", args);
}

public static String getStringInput(String prompt) {
Scanner scanner = new Scanner(System.in);
print(prompt);
String userInput = scanner.nextLine();
return userInput;
}

public static Integer getIntegerInput(String prompt) {
Scanner scanner = new Scanner(System.in);
print(prompt);
Integer userInput = Integer.valueOf(scanner.nextLine());
return userInput;
}
}
24 changes: 24 additions & 0 deletions src/main/java/io/zipcoder/polymorphism/Dog.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.zipcoder.polymorphism;

public class Dog extends Pet {
public Dog(String name){
super(name);
}

@Override
public String speak() {
return "Woof Woof";
}

@Override
public int compareTo(Pet o) {
// do this for remaining pets
if (this.getName().compareTo(o.getName()) == 0) {
this.getClass().getName(); // class name of this pet
o.getClass().getName(); // class name pet o
return this.getClass().getName().compareTo(o.getClass().getName());
} else {
return this.getName().compareTo(o.getName());
}
}
}
46 changes: 46 additions & 0 deletions src/main/java/io/zipcoder/polymorphism/MainApplication.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,53 @@
package io.zipcoder.polymorphism;

import java.util.ArrayList;
import java.util.List;

/**
* Created by leon on 11/6/17.
*/

public class MainApplication {
public static void main(String[] args){
Boolean turnOn = true;

while(turnOn){

Integer input = Console.getIntegerInput("\nHow many pets do you have?\n");
ArrayList<Pet> petsLog = new ArrayList<Pet>();

if(input > 0){
for(int i = 0; i < input; i++){
String petValue = Console.getStringInput("What kind of animal is pet #" + (i + 1) + "?");
String nameValue = Console.getStringInput("What is your pet's name?");
switch (petValue){
case ("dog"):
Dog dog = new Dog(nameValue);
petsLog.add(dog);
break;
case ("cat"):
Cat cat = new Cat(nameValue);
petsLog.add(cat);
break;
case ("bird"):
Bird bird = new Bird(nameValue);
petsLog.add(bird);
break;
default:
System.out.println("Wow, that's a really cool pet...");
break;
}

}
System.out.println("\n Your pets: \n");
for(int i = 0; i < petsLog.size(); i++){
System.out.println(petsLog.get(i).getClass().getSimpleName() + " " + petsLog.get(i).getName() + " says: " + petsLog.get(i).speak());
}

}
turnOn = false;
}

}

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

public abstract class Pet implements Comparable<Pet>{
private String name;

public Pet(String name){
this.name = name;
}

public String name(){
return name;
}

public String speak(){
return "";
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}


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

import java.util.Comparator;

public class PetComparator implements Comparator<Pet> {


@Override
public int compare(Pet o1, Pet o2) {
// do this for remaining pets?
if (o1.getClass().getName().compareTo(o2.getClass().getName()) == 0) {
return o1.getName().compareTo(o2.getName());
} else {
return o1.getClass().getName().compareTo(o2.getClass().getName());
}
}
}
59 changes: 59 additions & 0 deletions src/test/java/io/zipcoder/polymorphism/BirdTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package io.zipcoder.polymorphism;

import org.junit.Assert;
import org.junit.Test;

public class BirdTest {
@Test
public void nameTest() {
// Given
Bird bird = new Bird("BlueJay");
String expected = "BlueJay";

// When
String actual = bird.name();

// Then
Assert.assertEquals(expected, actual);
}

@Test
public void speakTest() {
// Given
Bird bird = new Bird("BlueJay");
String expected = "Chirp Chirp";

// When
String actual = bird.speak();

// Then
Assert.assertEquals(expected, actual);
}

@Test
public void compareToTest() {
// Given
Bird bird = new Bird("BlueJay");
Cat cat = new Cat("Tiger");

// When
Integer actual = bird.compareTo(cat);

// Then
Assert.assertTrue(actual<0);
}

@Test
public void compareToByNameTest() {
// Given
Bird bird = new Bird("BlueJay");
Dog dog = new Dog("BlueJay");

// When
Integer actual = bird.compareTo(dog);

// Then
Assert.assertTrue(actual<0);
}

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

import org.junit.Assert;
import org.junit.Test;

public class CatTest {
@Test
public void nameTest() {
// Given
Cat cat = new Cat("Val");
String expected = "Val";

// When
String actual = cat.name();

// Then
Assert.assertEquals(expected, actual);
}

@Test
public void speakTest() {
// Given
Cat cat = new Cat("Valerie");
String expected = "Meow";

// When
String actual = cat.speak();

// Then
Assert.assertEquals(expected, actual);
}

@Test
public void compareToTest() {
// Given
Cat cat = new Cat("Tiger");
Dog dog = new Dog("Bobby");

// When
Integer actual = cat.compareTo(dog);

// Then
Assert.assertTrue(actual>0);
}

@Test
public void compareToByNameTest() {
// Given
Cat cat = new Cat("Tiger");
Dog dog = new Dog("Tiger");

// When
Integer actual = cat.compareTo(dog);

// Then
Assert.assertTrue(actual<0);
}

}
Loading