Commit 1dbebf9e authored by mo's avatar mo

Initial commit

parent d58ed3a4
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/5"/>
<classpathentry kind="output" path="bin"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Zoo</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.release=disabled
org.eclipse.jdt.core.compiler.source=1.8
public abstract class Animal {
protected String name;
protected String type;
protected String eats;
protected String environment;
public Animal(String name) {
this.name = name;
}
/**
* @return
*/
public String getName() {
return name;
}
/**
* @return
*/
public String getEats() {
return eats;
}
/**
* @return
*/
public String getEnvironment() {
return environment;
}
/**
*
*/
@Override
public String toString() {
return "Name: " + name + ", Type: " + this.getClass().getSimpleName() + ", Eats: " + eats + ", Environment: " + environment;
}
}
\ No newline at end of file
public abstract class Bird extends Animal {
public Bird(String name) {
super(name);
// TODO Auto-generated constructor stub
}
}
public class Cod extends Fish {
public Cod(String name) {
super(name);
this.eats = "Small fish";
this.environment = "Ocean";
// TODO Auto-generated constructor stub
}
}
public abstract class Fish extends Animal {
public Fish(String name) {
super(name);
// TODO Auto-generated constructor stub
}
}
public class Kiwi extends Bird {
public Kiwi(String name) {
super(name);
this.eats = "Worms";
this.environment = "Forest";
// TODO Auto-generated constructor stub
}
}
public class Lizard extends Reptile {
public Lizard(String name) {
super(name);
this.eats = "Spiders";
this.environment = "desert";
}
}
public abstract class Mammal extends Animal {
public Mammal(String name) {
super(name);
// TODO Auto-generated constructor stub
}
}
public abstract class Reptile extends Animal {
public Reptile(String name) {
super(name);
}
}
public class Seal extends Mammal {
public Seal(String name) {
super(name);
this.eats = "fish";
this.environment = "Ocean";
}
}
\ No newline at end of file
import static org.junit.jupiter.api.Assertions.*;
import java.util.ArrayList;
import java.util.Iterator;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
class TestZoo{
Zoo zoo;
@BeforeEach
void setUp() throws Exception {
zoo = new Zoo("Molly's zoo");
}
@AfterEach
void tearDown() throws Exception {
}
@Test
void getAnimal() {
System.out.println("Running getAnimal test...");
assertEquals(zoo.getName(), "Molly's zoo");
Animal animal = zoo.getAnimal("Lizzie");
assertEquals(animal.getName(), "Lizzie");
System.out.println(animal.toString());
System.out.println("... end test getAnimal \n");
}
@Test
void getMissingAnimal() {
Animal animal = zoo.getAnimal("freddie");
assertNull(animal);
}
@Test
void getAnimals() {
System.out.println("Running getAnimals test...");
ArrayList<Animal> animals = zoo.getAnimals();
assertEquals(animals.size(), 4);
// Print out Animals in Zoo
System.out.println("Animals in Zoo");
System.out.println(animals.toString());
System.out.println("... end test getAnimals \n");
}
@Test
void getReptiles() {
System.out.println("Running getReptiles test...");
ArrayList<Animal> animals = zoo.getAnimals(Reptile.class);
assertEquals(animals.size(), 1);
// Print out reptiles in Zoo
System.out.println("Reptiles in Zoo");
System.out.println(animals.toString());
System.out.println("... end test getReptiles");
}
@Test
void getMammal() {
System.out.println("Running getMammal test...");
ArrayList<Animal> animals = zoo.getAnimals(Mammal.class);
assertEquals(animals.size(), 1);
// Print out reptiles in Zoo
System.out.println("Mammals in Zoo");
System.out.println(animals.toString());
System.out.print("... end test getMammal \n");
}
@Test
void getFoodForAllAnimals() {
System.out.println("Running getFoodForAllAnimals...");
ArrayList<Animal> animals = zoo.getAnimals();
ArrayList<String> food = zoo.getFoodRequirements(animals);
System.out.println(food.toString());
System.out.println("... end test getFoodForAllAnimals\n");
}
@Test
void getFoodForType() {
System.out.println("\n Running getFoodForType Reptile ...");
ArrayList<Animal> animals = zoo.getAnimals(Reptile.class);
ArrayList<String> food = zoo.getFoodRequirements(animals);
System.out.println(food.toString());
System.out.println("\n Running getFoodForType Bird ...");
animals = zoo.getAnimals(Bird.class);
food = zoo.getFoodRequirements(animals);
System.out.println(food.toString());
System.out.println("... end test getFoodForType");
}
@Test
void getAnimalByEnvironment() {
System.out.println("Running getAnimalByEnvironment...");
System.out.println("Animals fronm the Ocean");
ArrayList<Animal> animalsInEnvironment = zoo.getAnimalByEnvironment("Ocean");
assertEquals(animalsInEnvironment.size(), 2);
System.out.println(animalsInEnvironment.toString());
System.out.println("... end test getAnimalByEnvironment\n");
}
}
import java.util.ArrayList;
import java.util.Iterator;
/**
* @author mo
*
*/
public class Zoo {
ArrayList<Animal> hasAnimals = new ArrayList<Animal>();
private String name;
/**
* @param name
*/
public Zoo(String name) {
this.name = name;
hasAnimals.add(new Lizard("Lizzie"));
hasAnimals.add(new Seal("Sally"));
hasAnimals.add(new Kiwi("Keith"));
hasAnimals.add(new Cod("Catherine"));
}
/**
* @param name
* @return
*/
public Animal getAnimal(String name) {
Animal animal = null;
Iterator <Animal> iterator = hasAnimals.iterator();
while (iterator.hasNext()) {
Animal nextAnimal = (Animal)iterator.next();
if (nextAnimal.getName().equals(name)) {
animal = nextAnimal;
break;
}
}
return animal;
}
/**
* @return
*/
public String getName() {
return this.name;
}
/**
* @return
*/
public ArrayList<Animal> getAnimals() {
return hasAnimals;
}
/**
* @param animalType
* @return
*/
public ArrayList<Animal> getAnimals(Class<?> animalType) {
ArrayList<Animal> animals = new ArrayList <Animal> ();
Iterator <Animal> iterator = hasAnimals.iterator();
while (iterator.hasNext()) {
Animal nextAnimal = (Animal)iterator.next();
if (animalType.isInstance(nextAnimal)) {
animals.add(nextAnimal);
}
}
return animals;
}
/**
* @param animals
* @return
*/
public ArrayList<String> getFoodRequirements(ArrayList<Animal> animals) {
ArrayList<String> food = new ArrayList<String>();
Iterator <Animal> iterator = animals.iterator();
while (iterator.hasNext()) {
Animal animal = (Animal)iterator.next();
food.add(animal.getEats());
}
return food;
}
public ArrayList<Animal> getAnimalByEnvironment(String environment) {
ArrayList<Animal> animalsInEnvironment = new ArrayList<Animal>();
Iterator<Animal> iterator = this.hasAnimals.iterator();
while (iterator.hasNext()) {
Animal animal = (Animal) iterator.next();
if (animal.getEnvironment().equals(environment)) {
animalsInEnvironment.add(animal);
}
}
return animalsInEnvironment;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment