Skip to content

add cat, dog and hampster class and main app questions #64

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 4 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>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>6</source>
<target>6</target>
</configuration>
</plugin>
</plugins>
</build>
<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>
</dependencies>


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

public class Cat extends Pet {
//private static final String name = "";
public Cat(String type, String name) {
super(type, name);
}

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

public class Dog extends Pet {
// private static String name = "";

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

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

public class Hampster extends Pet {

public Hampster(String type, String name) {
super(type, name);
}

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

import io.zipcoder.polymorphism.Animal;

public abstract class Pet implements Animal {
private String name;
private String type;

protected Pet() {
}

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

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public String getName() {
return name;
}

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

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

public interface Animal {
String speak();
}
37 changes: 37 additions & 0 deletions src/main/java/io/zipcoder/polymorphism/MainApplication.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,44 @@
package io.zipcoder.polymorphism;


import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;
import io.zipcoder.pets.Cat;
import io.zipcoder.pets.Dog;
import io.zipcoder.pets.Hampster;
import io.zipcoder.pets.Pet;
import javax.crypto.spec.PSource;
import javax.xml.namespace.QName;
import java.util.ArrayList;
import java.util.Scanner;

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

public static void main(String[] args) {
String petType, petName;
ArrayList<Pet> petArrayList = new ArrayList<Pet>();
Scanner scanner = new Scanner(System.in);
System.out.println("How many pets do you have?");
int numberOfPets = scanner.nextInt();

for(int i = 1; i <= numberOfPets; i++) {
System.out.println("What kind of pet?");
petType = scanner.next();
System.out.println("Enter pet's name.");
petName = scanner.next();
if ("dog".equals(petType.toLowerCase())) {
petArrayList.add(new Dog(petType, petName));
} else if ("cat".equals(petType.toLowerCase())) {
petArrayList.add(new Cat(petType, petName));
} else if ("hampster".equals(petType.toLowerCase())) {
petArrayList.add(new Hampster(petType, petName));
}
}
for (Pet petObj : petArrayList) {
System.out.println(petObj.getName() + " " + petObj.speak());

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

import io.zipcoder.polymorphism.Animal;
import org.junit.Assert;
import org.junit.Test;

public class CatTest extends Pet {

@Test
public void testGetName() {
Pet pet = new Cat("cat", "Sylvester");
String expected = "Sylvester";
String actual = pet.getName();
Assert.assertEquals(expected, actual);
}

@Test
public void testSpeak() {
Pet pet = new Cat("cat", "Socks");
String expected = "Meow!";
String actual = pet.speak();
Assert.assertEquals(expected, actual);
}

@Test
public void inheritanceTest() {
Cat TestCat = new Cat("cat", "Fluffy");
Assert.assertTrue(TestCat instanceof Pet);
Assert.assertTrue(TestCat instanceof Cat);
}
}
30 changes: 30 additions & 0 deletions src/test/java/io/zipcoder/pets/DogTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package io.zipcoder.pets;

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

public class DogTest extends Pet {

@Test
public void inheritanceTest() {
Dog TestDog = new Dog("dog", "Rex");
Assert.assertTrue(TestDog instanceof Pet);
Assert.assertTrue(TestDog instanceof Dog);
}

@Test
public void testGetName() {
Pet pet = new Dog("dog", "X");
String expected = "X";
String actual = pet.getName();
Assert.assertEquals(expected, actual);
}

@Test
public void testSpeak() {
Pet pet = new Dog("dog", "XX");
String expected = "Ruff Ruff";
String actual = pet.speak();
Assert.assertEquals(expected, actual);
}
}
32 changes: 32 additions & 0 deletions src/test/java/io/zipcoder/pets/HampsterTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package io.zipcoder.pets;

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

public class HampsterTest extends Pet {

@Test
public void testGetName() {
Pet pet = new Hampster("hampster", "Apple");
String expected = "Apple";
String actual = pet.getName();
Assert.assertEquals(expected, actual);
}

@Test
public void testSpeak() {
Pet pet = new Hampster("hampster", "Elvis");
String expected = "Squeak, Squeak.";
String actual = pet.speak();
Assert.assertEquals(expected, actual);
}

@Test
public void inheritanceTest() {
Hampster TestHampster = new Hampster("hampster", "Earl");
Assert.assertTrue(TestHampster instanceof Pet);
Assert.assertTrue(TestHampster instanceof Hampster);

}
}

30 changes: 30 additions & 0 deletions src/test/java/io/zipcoder/polymorphism/MainApplicationTest.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,37 @@
package io.zipcoder.polymorphism;

import io.zipcoder.pets.Cat;
import io.zipcoder.pets.Dog;
import io.zipcoder.pets.Hampster;
import io.zipcoder.pets.Pet;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;

/**
* Created by leon on 11/6/17.
*/
public class MainApplicationTest {
Pet testHamster = new Hampster("hampster", "Apple");
Pet testCat = new Cat("cat", "Sylvester");
Pet testDog = new Dog("dog", "XX");
ArrayList<Pet> petArrayList = new ArrayList<Pet>();

@Test
public void ArrayListTest() {
petArrayList.add(testDog);
Integer expected = 1;
Integer actual = petArrayList.size();
Assert.assertEquals(actual, expected);
}
@Test
public void ArrayListTest2() {
petArrayList.add(testCat);
petArrayList.add(testHamster);
Integer expected = 2;
Integer actual = petArrayList.size();
Assert.assertEquals (actual, expected);
}
}