Commit 5410072d authored by Eky's avatar Eky

zoo app

parents
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/5">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="bin"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>zooApplication</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>
module zooApplication
{
exports zooApplication;
requires junit;
}
\ No newline at end of file
package zooApplication;
import java.util.ArrayList;
public class Animal
{
public String species;
public String habitat;
public String food;
public ArrayList<String> specialTraits = new ArrayList<String>();
//Basic traits: species name, habitat and food
public Animal(String species, String habitat, String food)
{
this.species = species;
this.habitat = habitat;
this.food = food;
}
//Add custom trait
public void addTrait(String input)
{
this.specialTraits.add(input);
return;
}
//Remove specific trait
public void removeTrait(String input)
{
if(specialTraits.size()>0)
{
for(int i = 0; i < specialTraits.size(); i++)
{
if(specialTraits.get(i).equals(input))
{
specialTraits.remove(i);
}
}
}
return;
}
//Find trait - returns true if trait exists for this animal
public boolean findTrait(String input)
{
if(specialTraits.size()>0)
{
for(int i = 0; i < specialTraits.size(); i++)
{
if(specialTraits.get(i).equals(input))
{
return true;
}
}
}
return false;
}
}
package zooApplication;
public class Avian extends Animal
{
//Basic - assumes bird can fly
public Avian(String species, String habitat, String food)
{
super(species, habitat, food);
setFlight(true);
}
//Option to declare flightless
public Avian(String species, String habitat, String food, boolean flight)
{
super(species, habitat, food);
setFlight(flight);
}
private void setFlight(boolean flight)
{
String canFly = "Can Fly";
if(flight == false) {canFly = "Flightless";}
addTrait(canFly);
return;
}
}
package zooApplication;
public class Main
{
public static void main(String[] args)
{
org.junit.runner.JUnitCore.main("zooApplication.UnitTest");
}
}
package zooApplication;
import java.util.ArrayList;
public class Mammal extends Animal
{
//Basic - assumes mammal is not a large predator
public Mammal(String species, String habitat, String food)
{
super(species, habitat, food);
}
//Option to declare mammal is a large predator
public Mammal(String species, String habitat, String food, boolean predator)
{
super(species, habitat, food);
setPredator(predator);
}
//Option to declare a mammal that might have unusual traits (e.g. it can fly)
public Mammal(String species, String habitat, String food, ArrayList<String> oddTraits)
{
super(species,habitat,food);
this.specialTraits = oddTraits;
}
private void setPredator(boolean predator)
{
if(predator == true) {addTrait("Large predator");}
return;
}
}
package zooApplication;
import java.util.ArrayList;
public class Mythic extends Animal
{
//Just for fun
public Mythic(String species, String habitat, String food)
{
super(species, habitat, food);
}
public Mythic(String species, String habitat, String food, ArrayList<String> mythicTraits)
{
super(species,habitat,food);
this.specialTraits = mythicTraits;
}
}
package zooApplication;
public class Piscine extends Animal
{
//Basic - assumes fish is salt water
public Piscine(String species, String habitat, String food)
{
super(species, habitat, food);
setSaline(true);
}
//Option to declare fresh water
public Piscine(String species, String habitat, String food, boolean saline)
{
super(species, habitat, food);
setSaline(saline);
}
private void setSaline(boolean saline)
{
String water = "Salt water";
if(saline == false) {water = "Fresh water";}
addTrait(water);
return;
}
}
package zooApplication;
public class Reptile extends Animal
{
//Basic - assumes reptile non-venomous
public Reptile(String species, String habitat, String food)
{
super(species, habitat, food);
}
//Option to declare venom
public Reptile(String species, String habitat, String food, boolean venom)
{
super(species, habitat, food);
setVenomous(venom);
}
private void setVenomous(boolean input)
{
String venom = "Venomous";
if(input == true) {addTrait(venom);}
return;
}
}
package zooApplication;
import org.junit.*;
//import org.junit.runner.*;
import static org.junit.Assert.*;
import java.util.ArrayList;
public class UnitTest
{
@SuppressWarnings("serial")
public ArrayList<String> batSpec = new ArrayList<String>() {{add("Can Fly");}};
@SuppressWarnings("serial")
public ArrayList<String> platSpec = new ArrayList<String>() {{add("Egg Laying");add("Venomous");}};
@SuppressWarnings("serial")
public ArrayList<String> dragSpec = new ArrayList<String>()
{{
add("Can Fly");
add("Breathes Fire");
add("Large Predator");
}};
public Animal wolf = new Mammal("Wolf","Mountain","Meat");
public Animal tiger = new Mammal("Tiger","Jungle","Meat",true);
public Animal fruitBat = new Mammal("Fruit Bat","Jungle","Fruit",batSpec);
public Animal platypus = new Mammal("Platypus","Riverlands","Invertebrates",platSpec);
public Animal eagle = new Avian("Eagle","Mountain","Meat");
public Animal emu = new Avian("Emu","Desert","Vegetation",false);
public Animal shark = new Piscine("Shark","Water","Meat");
public Animal carp = new Piscine("Carp","Water","Vegetation",false);
public Animal cobra = new Reptile("Cobra","Desert","Meat",true);
public Animal crocodile = new Reptile("Crocodile","Riverlands","Meat") {{addTrait("Large Predator");}};
public Animal dragon = new Mythic("Dragon","Volcano","Maidens",dragSpec);
@SuppressWarnings("serial")
public ArrayList<Animal> zooAnimals = new ArrayList<Animal>()
{{
add(wolf);add(tiger);add(fruitBat);add(platypus);
add(eagle);add(emu);
add(shark);add(carp);
add(cobra);add(crocodile);
add(dragon);
}};
public Zoo ysjZoo = new Zoo(zooAnimals);
@Test
public void testBasic()
{
assertEquals(true,ysjZoo.containsAnimal(wolf));
System.out.println("\nTest: Display Single Animal\n");
ysjZoo.displayDetails(wolf);
}
@Test
public void testDisplayAll()
{
assertTrue(ysjZoo.zooList.size()>0);
System.out.println("\nTest: Display All Animals\n");
ysjZoo.displayAll(ysjZoo.zooList);
}
@Test
public void testAnimalTypeList()
{
System.out.println("\nTest: Display all Mammals\n");
ArrayList<Animal> test = ysjZoo.typeList("Mammal");
assertTrue(test.size()>0);
ysjZoo.displayAll(test);
}
@Test
public void foodList()
{
System.out.println("\nTest: Print food list for Mammals");
ArrayList<Animal> test = ysjZoo.typeList("Mammal");
assertTrue(test.size()>0);
System.out.println("\n"+ysjZoo.foodList(test));
}
@Test
public void enviroList()
{
System.out.println("\nTest: Print animals of Jungle habitat\n");
ArrayList<Animal> test = ysjZoo.enviroList(ysjZoo.zooList, "Jungle");
assertTrue(test.size()>0);
ysjZoo.displayAll(test);
}
}
package zooApplication;
import java.util.ArrayList;
public class Zoo
{
public ArrayList<Animal> zooList = new ArrayList<Animal>();
//Create object using a pre-existing list
public Zoo(ArrayList<Animal> zooList)
{
this.zooList = zooList;
}
//Create object with empty list for adding to later
public Zoo(){}
//Add animal
public void addToZoo(Animal animal)
{
zooList.add(animal);
}
//Remove animal
public void removeFromZoo(Animal animal)
{
zooList.remove(animal);
}
//Returns true if list contains animal
public boolean containsAnimal(Animal animal)
{
if(zooList.contains(animal))
{
return true;
}
return false;
}
//Display details of specific animal in list
public void displayDetails(Animal animal)
{
if(containsAnimal(animal))
{
System.out.println("Name: "+animal.species+"\nHabitat: "+animal.habitat+"\nFood: "+animal.food);
if(animal.specialTraits.size()>0)
{
System.out.println("Special Traits: "+animal.specialTraits);
}
}
}
//Displays all animals in list
public void displayAll(ArrayList<Animal> input)
{
for(int i=0;i<input.size();i++)
{
displayDetails(input.get(i));
System.out.println("");
}
}
//generates list of animal of specific type
public ArrayList<Animal> typeList(String animal)
{
ArrayList<Animal> output = new ArrayList<Animal>();
for(int i=0;i<zooList.size();i++)
{
if(zooList.get(i).getClass().getSimpleName().equals(animal))
{
output.add(zooList.get(i));
}
}
return output;
}
//Generates list of foods that a list of animals needs
public ArrayList<String> foodList(ArrayList<Animal> input)
{
ArrayList<String> output = new ArrayList<String>();
for(int i=0;i<input.size();i++)
{
output.add(input.get(i).food);
}
return output;
}
//Checks which animals in a list belong to a specific habitat
public ArrayList<Animal> enviroList(ArrayList<Animal> input, String habitat)
{
ArrayList<Animal> output = new ArrayList<Animal>();
for(int i=0;i<input.size();i++)
{
if(input.get(i).habitat.equals(habitat))
{
output.add(input.get(i));
}
}
return output;
}
}
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