Skip to content
Snippets Groups Projects
Commit d746ae79 authored by Mathea Berg Vindsetmo's avatar Mathea Berg Vindsetmo
Browse files

of5_2 lf

parent 23a4faa0
No related branches found
No related tags found
No related merge requests found
package of5_2.lf;
import java.util.Iterator;
public class YoungAnimalsIterator implements Iterator<Animal> {
private int index = 0;
private Farm farm;
public YoungAnimalsIterator(Farm farm){
this.farm = farm;
}
@Override
public boolean hasNext() {
while(index<farm.numberOfAnimals()){
if(farm.getAnimal(index).getAge()<=2){
return true;
}
else{
index++;
}
}
return false;
}
@Override
public Animal next() {
if(!hasNext()){
throw new IllegalArgumentException("No more animals in the farm");
}
return farm.getAnimal(index++);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment