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
1 change: 1 addition & 0 deletions Nathan.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
##This is Nathan's Project
12 changes: 12 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@
<groupId>io.zipcoder</groupId>
<artifactId>polymorphism-1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
</plugins>
</build>
<packaging>jar</packaging>

<name>interfaces-1</name>
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/io/zipcoder/pets/Animal.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package io.zipcoder.pets;

public interface Animal {
String speak();
Integer getId();
}
16 changes: 16 additions & 0 deletions src/main/java/io/zipcoder/pets/Cat.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.zipcoder.pets;

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

public Cat() {
super("Cat", null);
}

@Override
public String speak(){
return "Meow!";
}
}
17 changes: 17 additions & 0 deletions src/main/java/io/zipcoder/pets/Dog.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.zipcoder.pets;

public class Dog extends Pet{

public Dog(String name, Integer id) {
super(name, id);
}

public Dog() {
super("Dog", null);
}

@Override
public String speak(){
return "Bark!";
}
}
17 changes: 17 additions & 0 deletions src/main/java/io/zipcoder/pets/Horse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.zipcoder.pets;

public class Horse extends Pet {

public Horse(String name, Integer id) {
super(name, id);
}

public Horse() {
super("Horse", null);
}

@Override
public String speak(){
return "Neigh!";
}
}
44 changes: 44 additions & 0 deletions src/main/java/io/zipcoder/pets/Pet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package io.zipcoder.pets;

public class Pet implements Animal {
private String Name;
private final Integer Id;
private PetOwner Owner;

public Pet(String name, Integer id) {
this.Name = name;
this.Id = id;
this.Owner = null;
}

public String getName() {
return this.Name;
}

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

public Integer getId() {
return Id;
}

// public void setId(Integer id) {
// this.Id = id;
// }

public PetOwner getOwner() {
return this.Owner;
}

public void setOwner(PetOwner owner) {
this.Owner = owner;
}




public String speak() {
return null;
}
}
58 changes: 58 additions & 0 deletions src/main/java/io/zipcoder/pets/PetOwner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package io.zipcoder.pets;

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

public class PetOwner {
private String name;
private ArrayList<Pet> petList = new ArrayList<>();
private static Integer ID = 0;
//private Integer numberOfPets;

public PetOwner(String name, int numOfPets) {
this.name = name;
this.petList = new ArrayList<>();
for (int i = 0; i < numOfPets; i++) {
petList.add(new Dog("TEST DOG", 90));
}
}

public String getName() {
return name;
}

public void setNumberOfPets(Integer newNumOfPets) {
for (int i = 0; i < newNumOfPets; i++) {
petList.add(new Dog("TEST DOG", 80));
}
}

public int getNumberOfPets() {
return petList.size();
}

public void addPet(Pet pet) {
for(Pet element: petList){
if(element != pet){
element = pet;
break;
}
}

}

public Pet getPetById(Integer petId) {
for(Pet pet: petList){
System.out.println(pet.getId());
if(pet.getId() == petId){
return pet;
}
}
return null;
}

public void setPetName(Pet pet, String newPetName) {

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

import io.zipcoder.pets.Cat;
import io.zipcoder.pets.Dog;
import io.zipcoder.pets.Horse;
import io.zipcoder.pets.Pet;

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

public class Console {
private static List<Pet> petList = new ArrayList<>();
private static Integer count;

public static void start() {
Scanner scanner = new Scanner(System.in);
System.out.println("Hello pet owner! How many pets do you have?");
//count = getIntegerInput("Hello pet owner! How many pets do you have?");
count = scanner.nextInt();
int I = 0;
while (I < count){
System.out.println("What type of pet is your pet # " + (I +1) + " ?\n" +
"1.Dog, 2. Cat, 3. Horse");
int input = scanner.nextInt();
// Integer input = getIntegerInput("What type of pet is your pet # " + I + " ?\n" +
// "1.Dog, 2. Cat, 3. Horse");
switch(input) {
case 1 :
petList.add(new Dog());
break;
case 2 :
petList.add(new Cat());
break;
case 3 :
petList.add(new Horse());
break;
default :
System.out.println("Error! Pick 1, 2 or 3");
continue;
}
I++;
}

}

public static void getNames(){
Scanner scanner = new Scanner(System.in);
String input;
int I = 0;
while (I < count){
System.out.println("What is the name of your pet #" + (I + 1) + " who is a " + petList.get(I).getClass().getSimpleName() + "?" );
input = scanner.nextLine();
petList.get(I).setName(input);
I++;
}
}
public static void speak(){
System.out.println("You have " + count + " pets.");
for(Pet eachPet: petList){
System.out.println(eachPet.getName() + " the " + eachPet.getClass().getSimpleName() + " says " + eachPet.speak());
}
}

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);
println(prompt);
return scanner.nextLine();
}

public static Integer getIntegerInput(String prompt) {
Scanner scanner = new Scanner(System.in);
println(prompt);
return Integer.parseInt(scanner.nextLine());
}

public static Float getFloatInput(String prompt) {
Scanner scanner = new Scanner(System.in);
println(String.valueOf(prompt));
return Float.parseFloat((scanner.nextLine()));
}


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

public class MainApplication {

public static void main(String[] args) {
Console.start();
Console.getNames();
Console.speak();
}

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

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

public class CatTest {
@Test
public void constructorTest() {
//given
String givenName = "Banksy";
Integer givenId = 0;
//when
Cat cat = new Cat(givenName, givenId);
String retrievedName = cat.getName();
Integer retrievedID = cat.getId();
//then
Assert.assertEquals(givenName, retrievedName);
Assert.assertEquals(givenId, retrievedID);
}
@Test
public void testGetName() {
//given
String expectedName = "Ony";
//when
Cat cat = new Cat(expectedName, 2);
String actualName = cat.getName();
//then
Assert.assertEquals(expectedName, actualName);
}
@Test
public void testGetID() {
//given
Integer expectedId = 1;
//when
Cat cat = new Cat("Kyo", expectedId);
Integer actualId = cat.getId();
//then
Assert.assertEquals(expectedId, actualId);
}
@Test
public void testSpeak() {
//given
String expectedSpeak = "Meow!";
//when
Cat newCat = new Cat("Spooky", 1);
String actualSpeak = newCat.speak();
//then
Assert.assertEquals(expectedSpeak, actualSpeak);
}
@Test
public void testPetInheritance() {
Cat newCat = new Cat("Gojo", 2);
Assert.assertTrue(newCat instanceof Pet);
}
}
Loading