Commit 01ee8881 authored by thomas.fuller's avatar thomas.fuller

1

parents
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
<classpathentry kind="output" path="bin"/>
</classpath>
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.source=1.8
File added
File added
File added
File added
import java.util.ArrayList;
public abstract class Animal implements Edible {
protected String name;
protected String gender;
protected boolean maturity;
protected Integer happiness;
protected Integer hunger;
protected ArrayList<Edible> belly;
protected BIOME biome;
protected int plotSize;
protected boolean carnivoreStatus;
protected int nutritionalValue;
protected int strength;
protected int price;
protected boolean isDead;
protected Zoo zoo;
public Animal(String name, String gender, boolean maturity) {
this.name = name;
this.gender = gender;
this.maturity = maturity;
happiness = 50;
hunger = 50;
belly = new ArrayList<Edible>();
isDead = false;
}
public String getName() {
return this.name;
}
public void growUp(){
this.maturity = true;
}
public boolean getIsDead(){
return this.isDead;
}
public void die(){
isDead = true;
}
public boolean isAdult(){
return this.maturity;
}
public int checkHunger(){
return hunger;
}
public void beActive(){
hunger -=20;
happiness += 10;
}
public boolean eat(Animal food){
boolean result = food.beEaten(strength);
if(result == true){
belly.add(food);
int value = food.getNutritionalValue();
hunger += value;
return true;}
else return false;
}
public boolean eat(Plant edible){
belly.add(edible);
int value = edible.getNutritionalValue();
hunger += value;
return true;
}
public BIOME getBiome() {
return biome;
}
public int getPlotSize(){
return plotSize;
}
public boolean getCarvinoreStatus(){
return carnivoreStatus;
}
public int getNutritionalValue(){
return nutritionalValue;
}
public boolean beEaten(int strength) {
if(this.strength > strength){
return false;}
else {
die();
return true;
}
}
public boolean beSold(){
if(maturity == false){
return true;
}
else return false;
}
public int getPrice() {
return price;
}
public Zoo getZoo(){
return this.zoo;
}
public void setZoo(Zoo zoo) {
this.zoo = zoo;
}
public void rampage(){
System.out.println( this.getClass().getSimpleName() + " " + name + " going on RAMPAGE!");
zoo.removeAnimal(this);
zoo.addAnimalToZoo(this);
Visitor visitor = zoo.getVisitors().get(0);
eat(visitor);
}
}
File added
public enum BIOME {
SAVANNAH,
JUNGLE,
PLAINS,
TUNDRA,
OCEAN,
GRASSLANDS
}
public interface Edible {
boolean beEaten(int strength);
int getNutritionalValue();
String getName();
}
import java.util.ArrayList;
import java.util.HashMap;
public class Enclosure {
private HashMap<String, Animal> animals;
private Integer plotSize;
private BIOME biome;
public Enclosure(int plotSize, BIOME biome){
animals = new HashMap<String, Animal>();
this.plotSize = plotSize;
this.biome = biome;
}
public HashMap<String, Animal> getAnimals(){
return animals;
}
public BIOME getBiome(){
return this.biome;
}
public boolean addAnimal(Animal animal){
if (animal.getBiome() == biome && animal.getPlotSize() <= plotSize){
animals.put(animal.getName(), animal);
plotSize -= animal.getPlotSize();
return true;
}
return false;
}
public void expand(int size){
plotSize += size;
}
public int getPlotSize() {
return plotSize;
}
public void removeAnimal(Animal animal) {
animals.remove(animal.getName());
}
}
public class FireDragon extends Animal {
public FireDragon(String name, String gender, boolean maturity){
super(name, gender, maturity);
biome = BIOME.SAVANNAH;
plotSize = 50;
carnivoreStatus = true;
nutritionalValue = 200;
strength = 300;
price = 200;
}
@Override
public boolean eat(Plant food){
System.out.println("Fire Dragon " + name +" spits out the " + food.getName());
return false;
}
@Override
public boolean eat(Animal food){
boolean result = super.eat(food);
if (result==true){
System.out.println("Fire Dragon " + name + " " + "ate " + food.getName());
return true;}
else return false;
}
}
File added
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;
public class Main {
public static void printZooStatus(Zoo zoo) {
// 1. store all animals in array list
ArrayList<Enclosure> enclosures = zoo.getEnclosures();
// 3. display the details of an individual animal
System.out.println("Displaying details of all animals");
for (Enclosure enclosure : enclosures) {
HashMap<String, Animal> animals = enclosure.getAnimals();
System.out.println("This enclosure is " + enclosure.getBiome());
if (animals.size() == 0) {
System.out.println("There are no animals here");
}
for (Animal currentAnimal : animals.values()) {
String animalDetails = currentAnimal.getClass().getSimpleName();
System.out.println("The " + enclosure.getBiome() + " enclosure has " + animalDetails + " "
+ currentAnimal.getName());
}
}
}
public static void DisplayIndividualanimals(Zoo zoo) {
ArrayList<Enclosure> enclosures = zoo.getEnclosures();
// 2. display the details of an individual animal
System.out.println("Displaying details of individual animals");
for (Enclosure enclosure : enclosures) {
HashMap<String, Animal> animals = enclosure.getAnimals();
System.out.println("This enclosure is " + enclosure.getBiome());
if (animals.size() == 0) {
System.out.println("There are no animals here");
}
for (Animal currentAnimal : animals.values()) {
String animalDetails = currentAnimal.getClass().getSimpleName();
System.out.println("The " + enclosure.getBiome() + " enclosure has " + animalDetails + " "
+ currentAnimal.getName());
break;
}
}
}
public static void DisplayFood(Zoo zoo1,Plant carrot,Animal unicorn,Animal dragon) {
// 5. display food
zoo1.findAnimalInEnclosures(dragon.getName()).eat(carrot);
zoo1.findAnimalInEnclosures(dragon.getName()).eat(unicorn);
}
public static void printMenu(String[] options) {
for (String option : options) {
System.out.println(option);
}
System.out.print("Choose your option : ");
}
public static void main(String[] args) {
Zoo zoo1 = new Zoo();
Zoo zoo2 = new Zoo();
zoo1.setBudget(1000);
zoo1.addEnclosure(new Enclosure(100, BIOME.GRASSLANDS));
Animal dragon = new FireDragon("Fluffy", "male", true);
Animal unicorn = new Unicorn("Sparkles", "female", true);
Animal phoenix = new Phoenix("Felix", "male", true);
Animal babyPhoenix = new Phoenix("Dusty", "female", false);
Animal seaSerpent = new SeaSerpent("Kraken", "female", true);
Visitor visitor = new Visitor("Tony", "male", true, 200);
Plant carrot = new Plant("Carrot", 5);
zoo1.placeAnimalInEnclosure(dragon);
zoo1.placeAnimalInEnclosure(unicorn);
zoo1.placeAnimalInEnclosure(phoenix);
zoo1.placeAnimalInEnclosure(babyPhoenix);
zoo1.placeAnimalInEnclosure(seaSerpent);
String[] options = { "1- Do you want to add animals to zoo",
"2- Display indiviudal animals details",
"3- Display all animals details",
"4- output food required for all animals ",
"5- Exit",
};
Scanner scanner = new Scanner(System.in);
int option = 1;
while (option != 6) {
printMenu(options);
try {
option = scanner.nextInt();
switch (option) {
case 1:
System.out.println("Animals Added");
break;
case 2:
DisplayIndividualanimals(zoo1);
break;
case 3:
printZooStatus(zoo1);
break;
case 4:
DisplayFood(zoo1, carrot, unicorn, dragon);
break;
case 5:
System.exit(0);
}
} catch (Exception ex) {
System.out.println("Please enter an integer value between 1 and " + options.length);
scanner.next();
}
}
System.out.println("----------------------");
System.out.println("This is what is left in Zoo1:");
// 6. animals from different envirments
printZooStatus(zoo1);
}
}
public class Phoenix extends Animal {
public Phoenix(String name, String gender, boolean maturity){
super(name, gender, maturity);
biome = BIOME.SAVANNAH;
plotSize = 10;
carnivoreStatus = false;
nutritionalValue = 30;
strength = 100;
price = 100;
}
@Override
public boolean eat(Animal food){
System.out.println("Phoenix " + name +" is still hungry");
return false;
}
@Override
public boolean eat(Plant food){
super.eat(food);
System.out.println("Phoenix " + name + " " + "ate " + food.getName());
return true;
}
}
File added
public class Plant implements Edible {
private String name;
private int nutritionalValue;
public Plant(String name, int nutritionalValue){
this.name = name;
this.nutritionalValue = nutritionalValue;
}
public boolean beEaten(int strength) {
return true;
}
public int getNutritionalValue() {
return nutritionalValue;
}
public String getName(){
return name;
}
}
public class SeaSerpent extends Animal {
public SeaSerpent(String name, String gender, boolean maturity){
super(name, gender, maturity);
biome = BIOME.OCEAN;
plotSize = 200;
carnivoreStatus = true;
nutritionalValue = 500;
strength = 400;
price = 500;
}
@Override
public boolean eat(Plant food){
boolean result = super.eat(food);
if (result==true){
System.out.println("SeaSerpent " + name + " " + "ate " + food.getName());
return true;}
else return false;
}
@Override
public boolean eat(Animal food){
boolean result = super.eat(food);
if (result==true){
System.out.println("SeaSerpent " + name + " " + "ate " + food.getName());
return true;}
else return false;
}
}
public class Unicorn extends Animal implements Edible {
public Unicorn(String name, String gender, boolean maturity){
super(name, gender, maturity);
biome = BIOME.SAVANNAH;
plotSize = 20;
carnivoreStatus = false;
nutritionalValue = 70;
strength = 50;
}
@Override
public boolean eat(Animal food){
System.out.println("Unicorn " + name +" is still hungry");
return false;
}
@Override
public boolean eat(Plant food){
super.eat(food);
System.out.println("Unicorn " + name + " " + "ate " + food.getName());
return true;
}
}
public class Visitor extends Animal implements Edible {
private String name;
private int money;
public Visitor(String name, String gender, boolean maturity, int money) {
super(name, gender, maturity);
this.name = name;
this.money = money;
this.strength = 100;
this.nutritionalValue = 150;
}
public String getName() {
return this.name;
}
public int getMoney() {
return this.money;
}
public boolean beEaten(int strength){
if(strength >= this.strength){
die();
return true;
}
else return false;
}
public boolean buyTicket(int price){
if(price < money){
money -= price;
return true;
}
return false;
}
public int getNutritionalValue() {
return nutritionalValue;
}
}
File added
import java.util.ArrayList;
import java.util.HashMap;
public class Zoo {
private ArrayList<Visitor> visitors;
private int ticketPrice;
private ArrayList<Enclosure> enclosures;
private int budget;
private HashMap<String, Animal> unplacedAnimals;
public Zoo(){
visitors = new ArrayList<Visitor>();
ticketPrice = 20;
enclosures = new ArrayList<Enclosure>();
unplacedAnimals = new HashMap<String, Animal>();
budget = 100;
}
public ArrayList<Visitor> getVisitors(){
return new ArrayList<Visitor>(visitors);
}
public ArrayList<Enclosure> getEnclosures(){
return new ArrayList<Enclosure>(enclosures);
}
public int getTicketPrice(){
return ticketPrice;
}
public void setTicketPrice(int newPrice){
ticketPrice = newPrice;
}
public boolean sellTicket(Visitor visitor){
if(visitor.buyTicket(ticketPrice) == true){
budget += ticketPrice;
visitors.add(visitor);
System.out.println(visitor.getName() + " bought a ticket");
return true;
}
else return false;
}
public int getBudget() {
return budget;
}
public void removeAnimal(Animal animal){
for(Enclosure enclosure : enclosures){
enclosure.removeAnimal(animal);
}
}
public void addAnimalToZoo(Animal animal){
unplacedAnimals.put(animal.getName(),animal);
}
private boolean animalExistsInEnclosure(Enclosure enclosure, String name){
if (enclosure.getAnimals().get(name) != null) return true;
else return false;
}
public Animal findAnimalInEnclosures(String name){
Animal foundAnimal = null;
for(Enclosure enclosure : enclosures){
if(animalExistsInEnclosure(enclosure, name)){
foundAnimal = enclosure.getAnimals().get(name);
break;
}
}
return foundAnimal;
}
public Animal findUnplacedAnimal(String name){
Animal foundAnimal = null;
foundAnimal = unplacedAnimals.get(name);
return foundAnimal;
}
public boolean sellBabyAnimal(String name, Zoo buyingZoo){
Animal animalToSell = findAnimalInEnclosures(name);
if (buyingZoo.checkCanAffordAnimal(animalToSell) && animalToSell.beSold() == true) {
budget += animalToSell.getPrice();
removeAnimal(animalToSell);
buyingZoo.buyBabyAnimal(animalToSell);
System.out.println(animalToSell.name + " was sold to another zoo :( ");
return true;
}
else return false;
}
private void buyBabyAnimal(Animal animal) {
budget -= animal.getPrice();
placeAnimalInEnclosure(animal);
}
public void setBudget(int amount) {
budget = amount;
}
public boolean checkCanAffordAnimal(Animal animal) {
if(budget >= animal.getPrice()) return true;
else return false;
}
public HashMap<String, Animal> getUnplacedAnimals() {
return unplacedAnimals;
}
public void addEnclosure(Enclosure enclosure){
enclosures.add(enclosure);
}
public void createSuitableEnclosure(Animal animal){
Enclosure enclosure = new Enclosure(animal.getPlotSize(), animal.getBiome());
enclosure.addAnimal(animal);
animal.setZoo(this);
enclosures.add(enclosure);
}
public void placeAnimalInEnclosure(Animal animal) {
for(Enclosure enclosure : enclosures){
if (enclosure.getBiome() == animal.getBiome()){
if(enclosure.getPlotSize()>= animal.getPlotSize()){
enclosure.addAnimal(animal);
animal.setZoo(this);
return;
}
else {
enclosure.expand(animal.getPlotSize() - enclosure.getPlotSize());
enclosure.addAnimal(animal);
animal.setZoo(this);
return;
}
}
}
createSuitableEnclosure(animal);
}
public void removeDeadAnimalsFromZoo(){
for(Enclosure enclosure : enclosures){
ArrayList<String> animalToRemoveKeys = new ArrayList<String>();
HashMap<String, Animal> animals = enclosure.getAnimals();
for(String animalName : animals.keySet()) {
Animal animal = animals.get(animalName);
if(animal.getIsDead() == true) {
animalToRemoveKeys.add(animalName);
}
}
for(String animalToRemoveName : animalToRemoveKeys) {
animals.remove(animalToRemoveName);
}
}
}
public void returnAnimalToCageFromUnplacedAnimals(){
for(Animal animal : unplacedAnimals.values()){
this.placeAnimalInEnclosure(animal);
System.out.println(animal.getName() + " was returned to the cage");
}
unplacedAnimals.clear();
}
}
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