Skip to content

submit #54

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 3 commits into
base: master
Choose a base branch
from
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
26 changes: 26 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,32 @@
<groupId>io.zipcoder</groupId>
<artifactId>polymorphism</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>


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

public class Cat extends Pets{

public Cat(String petName, String petType) {
super(petName, petType);
}

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

import java.util.logging.Logger;

public class Dog extends Pets {

public Dog(String petName) {
}
public Dog(String name, String type){
super(name, type);

}

@Override
public String speak() {
return "Woof";
}
}
43 changes: 43 additions & 0 deletions src/main/java/io/zipcoder/polymorphism/MainApplication.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,50 @@
package io.zipcoder.polymorphism;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* Created by leon on 11/6/17.
*/
public class MainApplication {
public static void main(String [] args) {
List<Pets> pets = new ArrayList();

Logger logger = Logger.getLogger(MainApplication.class.getName());
Scanner scan = new Scanner(System.in);

logger.info("How many pets ? ");
int numberOfPets = scan.nextInt();


for(int i = 0; i < numberOfPets; i++){
logger.info("What is the name of pet " + (i +1) + " ?");
String name = scan.next();

logger.info("What is the type of pet " + (i +1) + " ?");
String type = scan.next();
String correctedType = type.toLowerCase();

if (correctedType.equals("dog")) {
Pets pet = new Dog(name,type);
pets.add(i,pet);
}else if(correctedType.equals("cat")){
Pets pet = new Cat(name,type);
pets.add(i,pet);
}else if(correctedType.equals("turtle")) {
Pets pet = new Turtle(name, type);
pets.add(i, pet);
}else{
Pets pet = new Pets();
pets.add(i,pet);
}
}
for(int i =0 ; i < pets.size(); i++ ){
logger.info(pets.get(i).speak());
}

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

import java.util.logging.Logger;


public class Pets {
String petName;
String petType;

public Pets(){}
public Pets(String petName, String petType){
this.petName = petName;
this.petType = petType;
}

public String getPetName() {
return petName;
}

public void setPetName(String petName) {
this.petName = petName;
}

public String getPetType() {
return petType;
}

public void setPetType(String petType) {
this.petType = petType;
}

public String speak(){ return "AnimalNoise"; }

public String toString(){
return petName + " " + petType;
}

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

import javax.swing.event.AncestorEvent;

public class Turtle extends Pets {

public Turtle(String petName, String petType) {
super(petName, petType);
}

@Override
public String speak() {
return "Meowf";
}
}
41 changes: 41 additions & 0 deletions src/test/java/io/zipcoder/polymorphism/CatTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package io.zipcoder.polymorphism;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.*;

public class CatTest {

@Before
public void setUp() throws Exception {
}

@After
public void tearDown() throws Exception {
}

@Test
public void TestCatConstructor() {
String expectedName = "Bilbo";
String expectedType = "cat";

Cat cat = new Cat(expectedName, expectedType);

String actualName = cat.getPetName();
String actualType = cat.getPetType();

Assert.assertEquals(expectedName, actualName);
Assert.assertEquals(expectedType, actualType);
}

@Test
public void speak() {
Cat cat = new Cat("Bilbo", "cat");
String expected = "Meow";
String actual = cat.speak();
Assert.assertEquals(expected, actual);
}
}
41 changes: 41 additions & 0 deletions src/test/java/io/zipcoder/polymorphism/DogTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package io.zipcoder.polymorphism;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.*;

public class DogTest {

@Before
public void setUp() throws Exception {
}

@After
public void tearDown() throws Exception {
}

@Test
public void TestDogConstructor(){

String expectedName = "Frodo";
String expectedType = "dog";
Dog dog = new Dog(expectedName, expectedType);

String actualName = dog.getPetName();
String actualType = dog.getPetType();

Assert.assertEquals(expectedName, actualName);
Assert.assertEquals(expectedType, actualType);
}

@Test
public void speak() {
Dog dog = new Dog("Frodo", "dog");
String expected = "Woof";
String actual = dog.speak();
Assert.assertEquals(expected, actual);
}
}
12 changes: 12 additions & 0 deletions src/test/java/io/zipcoder/polymorphism/MainApplicationTest.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
package io.zipcoder.polymorphism;

import org.junit.Test;

import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

/**
* Created by leon on 11/6/17.
*/
public class MainApplicationTest {


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

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

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

import static org.junit.Assert.*;

public class PetsTest {
Logger logger = Logger.getLogger(PetsTest.class.getName());
Scanner scan = new Scanner(System.in);
// @Test
// public void TestPet(){
// List<Pets> pets = new ArrayList();
// logger.info("How many pets ? ");
// int numberOfPets = scan.nextInt();
//
//
// for(int i = 0; i < numberOfPets; i++){
// logger.info("What is the name of pet" + i +1 + " ?");
// String name = scan.nextLine();
// logger.info("What is the type of pet" + i +1 + " ?");
// String type = scan.nextLine();
// Pets pet = new Pets(name,type);
// pets.add(i,pet);
// }
// for(int i =0 ; i < pets.size(); i++ ){
// logger.info(String.valueOf(pets.get(i)));
// }
//
// }



@Test
public void TestPetConstructor() {
String expectedName = "Bilbo";
String expectedType = "cat";

Pets pet = new Pets(expectedName, expectedType);

String actualName = pet.getPetName();
String actualType = pet.getPetType();

Assert.assertEquals(expectedName, actualName);
Assert.assertEquals(expectedType, actualType);
}

@Test
public void speak() {
Pets pet = new Pets("Bilbo", "cat");
String expected = "AnimalNoise";
String actual = pet.speak();
Assert.assertEquals(expected, actual);
}



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

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.*;

public class TurtleTest {

@Before
public void setUp() throws Exception {
}

@After
public void tearDown() throws Exception {
}

@Test
public void TestTurtleConstructor(){



String expectedName = "Gretchen";
String expectedType = "turtle";
Turtle turtle = new Turtle(expectedName, expectedType);
String actualName = turtle.getPetName();
String actualType = turtle.getPetType();

Assert.assertEquals(expectedName, actualName);
Assert.assertEquals(expectedType, actualType);
}

@Test
public void speak() {
Turtle turtle = new Turtle("Gretchen", "turtle");
String expected = "Meowf";
String actual = turtle.speak();
Assert.assertEquals(expected, actual);
}
}