@@ -1,15 +1,25 @@
package main;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashSet;

import animals.Chimpanzee;
import animals.Gorilla;
import animals.Lion;
import animals.Tiger;
import structures.*;
import zookeepers.*;
import food.*;

public class Zoo {

//Static stuff
//Static stuff ======================================================

public static HashSet<Food> foods;

@@ -33,20 +43,128 @@ private static void initializeFoods(){
foods.add(ICE_CREAM);
}

//instance stuff
//instance stuff ========================================================

private ArrayList<Enclosure> enclosures;
private ArrayList<Zookeeper> zookeepers;
private Foodstore store;
private String name;

public Zoo(){
initializeFoods();
public Zoo(String config){
store = new Foodstore();
for(Food f : foods){
store.addBucket(f, 20);
if(foods == null) initializeFoods();
if(config != null) initializeZoo(config);
}

private void initializeZoo(String config) {
enclosures = new ArrayList<>();
zookeepers = new ArrayList<>();
//Make sure the file exists and create a reader for it
BufferedReader br;
try{
br = new BufferedReader(new FileReader(config));
//go through each line
String line;
// [0] is the class, 1 is the parameters
String[] property;
String[] parameters;
while(br.ready()){
line = br.readLine();
property = line.split(":");
//split up everything on the left of the ':' based on ','s
parameters = property[1].split(",");
//sort out what to do
switch (property[0]) {
case "zoo":
System.out.println("Building " + parameters[0]);
name = parameters[0];
break;

case "steak":case "hay":case "fruit":case "celery":case "fish":case "icecream":
addFoodToLastStore(property[0], Integer.parseInt(parameters[0]));
break;

case "enclosure":
Enclosure tempEnclosure = new Enclosure();
tempEnclosure.addWaste(Integer.parseInt(parameters[0]));
enclosures.add(tempEnclosure);
break;

case "lion":case "tiger":case "gorilla":case "chimpanzee":
addAnimalToEnclosure(property[0], parameters[0].charAt(0), Integer.parseInt(parameters[1]), Integer.parseInt(parameters[2]), Integer.parseInt(parameters[3]));
break;

case "zookeeper":
zookeepers.add(new Zookeeper(enclosures.get(enclosures.size() -1), store));
break;
case "playZookeeper":
zookeepers.add(new PlayZookeeper(enclosures.get(enclosures.size() -1), store));
break;
case "physioZookeeper":
zookeepers.add(new PhysioZookeeper(enclosures.get(enclosures.size() -1), store));
break;

default:
System.out.println("Tag \"" + property[0] + "\" not recognized.");
break;
}
}
} catch (IOException e){
e.printStackTrace();
}
}

private void addAnimalToEnclosure(String animal, char gender, int age, int health, int enclosure){
Enclosure tempEnclosure = enclosures.get(enclosure);
try{
switch (animal) {
case "lion":
tempEnclosure.addAnimal(new Lion(age, gender, health));
break;
case "tiger":
tempEnclosure.addAnimal(new Tiger(age, gender, health));
break;
case "gorilla":
tempEnclosure.addAnimal(new Gorilla(age, gender, health));
break;
case "chimpanzee":
tempEnclosure.addAnimal(new Chimpanzee(age, gender, health));
break;
}
} catch (EnclosureFullException e){
System.err.println("Cannot add " + animal + " to enclosure " + tempEnclosure);
}
}

private void addFoodToLastStore(String food, int amount) {
Foodstore targetStore;
if(enclosures.isEmpty()){
targetStore = store;
}else{
targetStore = enclosures.get(enclosures.size() -1).getFoodstore();
}
switch(food){
case "steak":
targetStore.addFood(STEAK, amount);
break;
case "hay":
targetStore.addFood(HAY, amount);
break;
case "fruit":
targetStore.addFood(FRUIT, amount);
break;
case "celerey":
targetStore.addFood(CELERY, amount);
break;
case "fish":
targetStore.addFood(FISH, amount);
break;
case "icecream":
targetStore.addFood(ICE_CREAM, amount);
break;
}
}

public void go(){
while(true){
aMonthPasses();
@@ -17,23 +17,29 @@ public Enclosure(){
waste = 0;
}

public void addAnimal(Animal animalToAdd){
public void addAnimal(Animal animalToAdd) throws EnclosureFullException {
if(animals.size() < 20){
animals.add(animalToAdd);
for(Food f : animalToAdd.eats){
if(!foodstore.contains(f)){
foodstore.addBucket(f, 5);
}
}
} else {
throw new EnclosureFullException();
}
//TODO: add code to handle full array
}

public void removeAnimal(Animal animalToRemove){
public void removeAnimal(Animal animalToRemove) throws AnimalNotFoundException{
if(animals.contains(animalToRemove)){
animals.remove(animalToRemove);
} else{
throw new AnimalNotFoundException();
}
//TODO: add code to handle missing animal
}

public Animal[] getAnimals(){
return animals.toArray(new Animal[animals.size()]);
}

/**
@@ -3,7 +3,6 @@
import java.util.HashMap;

import food.*;
import main.Zoo;

public class Foodstore {

@@ -21,7 +20,7 @@ public void addFood(Food food, int amount){
if(store.containsKey(food)){
store.get(food).addFood(amount);
} else {
//throw exception later
addBucket(food, amount);
}
}

@@ -30,30 +29,16 @@ public void addBucket(Food food, int max){
}

/**
* if the requested food isn't present return false,
* if it is reduce the stored amount by 1 and return true
* if the requested food isn't present throw an exception,
* if it is reduce the stored amount by the amount requested
* @throws FoodNotFoundException
* @throws InsufficientFoodException
*/
public void takeFood(Food food){
public void takeFood(Food food, int amount) throws FoodNotFoundException, InsufficientFoodException{
if(store.containsKey(food)){
store.get(food).removeFood(1);
store.get(food).removeFood(amount);
} else {
//throw
throw new FoodNotFoundException(food);
}
}

/**
* same as above but with string in case
* you don't have access to the original
* food object
*/
public Food takeFood(String foodName) throws Exception{
for(Food food : store.keySet()){
if(food.getName().equals(foodName)){
store.get(food).removeFood(1);
return food;
}
}
//add actual exeption
throw new Exception();
}
}
@@ -1,9 +1,18 @@
package zookeepers;

import java.util.ArrayList;
import java.util.Random;

import animals.Animal;
import food.Food;
import food.InsufficientFoodException;
import structures.*;

public class Zookeeper {

private final int MAX_TREATED_ANIMALS = 2;
private final int MAX_FOOD_TRANSFERED = 20;

private Enclosure assignedEnclosure;
private Foodstore zooStore;

@@ -13,6 +22,42 @@ public Zookeeper(Enclosure assignedEnclosure, Foodstore zooStore){
}

public void aMonthPasses(){

//move up to 20 units of food from zoo store to enclosure
Animal[] animals = assignedEnclosure.getAnimals();
ArrayList<Food> requiredFoods = new ArrayList<Food>();
for(Animal a : animals){
for(Food f : a.eats){
if(!requiredFoods.contains(f)){
requiredFoods.add(f);
}
}
}
int amountToTake = MAX_FOOD_TRANSFERED / requiredFoods.size();
for(Food f : requiredFoods){
try {
zooStore.takeFood(f, amountToTake);
} catch (FoodNotFoundException e) {
// this shouldn't be possible, so will just print stack trace
e.printStackTrace();
} catch (InsufficientFoodException e) {
// try again, only taking amount left
try {
zooStore.takeFood(f, e.getAmountLeft());
} catch (FoodNotFoundException | InsufficientFoodException e1) {
//if this fails something REALLY bad just happened, so just print the stacktrace
e1.printStackTrace();
}
}
assignedEnclosure.getFoodstore().addFood(f, amountToTake);
}
//remove up to 20 units of waste
assignedEnclosure.removeWaste(20);
//treat up to 2 animals
Random r = new Random();
int animalsToTreat = r.nextInt(MAX_TREATED_ANIMALS);
for(int i = 0; i < animalsToTreat; i++){
//add loop to catch exception
animals[r.nextInt(animals.length)].treat(this);
}
}
}