My code jumps around a bit but what I want it to do is this:
when public void another day is run (in the hive class) the for loop iterates through an arraylist and gets the bees from it. It then tries using the bee.anotherDay() method on the bee which was in the arraylist.
the anotherDay() method from bee runs eat() method from the bee class- adds 1 to the age of the bee and then every 3 days (starting on day 11) adds an egg to the arraylist in hive.
the eat method- if there is enough honey (2 or more units) will take the 2 honey and if the bees health is 2 or less- will increase it by 1. If there is not enough honey then the bees health is reduced by 1 and if the health reaches 0 an exception is thrown.
the exception is caught in the anotherDay() method of the hive which removes the bee with health 0.
By using println a few times I think what is actually happening is that my exception is being thrown and caught but the bee is not being removed from the arraylist.
My code:
public void anotherDay(){ //anotherDay method in hive class
for(int i = 0;i<cells.size(); i++){
Bee bee = cells.get(i);
try{
bee = bee.anotherDay();
}catch(Exception e){
cells.remove(i);
}
cells.set(i, bee);
}
}
public Bee anotherDay() throws Exception{ //anotherDay mehtod in my Queen class (subclass of Bee}
eat();
age++;
if(age%3 == 2){
hive.addBee(new Egg());
}
return this;
}
public boolean eat() throws Exception{
if(hive.honey >= 2){
hive.takeHoney(2);
if(health == 3){
}else{
health= health++;
}
return true;
}else{
health = health -1;
if(health == 0){
throw new Exception();
}
return false;
}
}