0

My code jumps around a bit but what I want it to do is this:

  1. 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.

  2. 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.

  3. 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.

  4. 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;
    }
}
Andrea
  • 10,337
  • 17
  • 55
  • 59
user2973447
  • 339
  • 1
  • 2
  • 13
  • 2
    Please read: [Why not use exceptions as regular flow of control?](http://stackoverflow.com/questions/729379/why-not-use-exceptions-as-regular-flow-of-control) – reto Dec 07 '13 at 12:43

1 Answers1

2

First comment, this isn't really the correct use of an Exception. You should return a status code, maybe an enum.

Second comment, never throw new Exception(). Create your own Exception class, otherwise you deal with all exceptions that could be thrown (NullPointerException, ArrayIndexOutOfBoundsException etc) in exactly the samne way. This is a bug waiting to happen.

Now onto your problem. You do remove the item at index i from your List, but you always add it back in again with your call to set.

Never remove items from a Collection while looping. In fact never loop over a collection by index unless you have to.

Use an Iterator which has a remove method. This is the only (almost only) safe way to remove from a collection while looping.

final Iterator<Bee> myIter = cells.iterator();
while(myIter.hasNext()) {
    final Bee bee = myIter.next();
    try{
        bee.anotherDay();
    }catch(Exception e){
        myIter.remove();
    }
}

There is no need for you another day method to return this and there is no need to replace the reference in your List with exactly the same reference.

Looking at your eat method, there is quite a bit of tidying that can happen; I would recommend this:

public boolean eat() throws Exception{
    if(hive.honey >= 2){
        hive.takeHoney(2);
        if(health < 3){
            health++;
        }
        return true;
    }
    health -= 1;
    if(health == 0){
        throw new BeeOutOfHealthException(); 
    }
    return false;
}

I.e. many of your assignments are rather confused. You don't need the else if you return in the if. Your nested if is a little odd - having an empty statement is certainly code smell.

Further the logic of taking honey from the hive should really be in the hive for takeHoney should probably return a boolean if that amount of honey cannot be taken. That then reduces your method to:

public boolean eat() throws Exception{
    if(hive.takeHoney(2)){
        if(health < 3){
            health++;
        }
        return true;
    }
    health -= 1;
    if(health == 0){
        throw new BeeOutOfHealthException(); 
    }
    return false;
}
Boris the Spider
  • 54,398
  • 6
  • 98
  • 152