import java.util.Arrays;
public class TaskCarousel {
private int count = 0;
private int executed = 0;
private final Task[] carousel;
public TaskCarousel(int capacity) {
carousel = new Task[capacity];
}
public boolean addTask(Task task) {
if (task == null || task.isFinished() || isFull()){
return false;
}
else {
carousel[count] = task;
count++;
return true;
}
}
public boolean execute() {
if (executed > carousel.length - 1){
executed = executed - (carousel.length - 1);
}
// This condition will skip over any null or finished tasks without removing them from the array
// This will cause the array to fill up with null or finished tasks and prevent new tasks from being added
if (carousel[executed] == null
carousel[executed].isFinished()){
executed++;
return false;
}
// This condition will only set the current task to null if it is finished
// But it will not shift the remaining tasks to fill up the empty space
// This will cause the array to have gaps between tasks and mess up the execution order
else {
if (carousel[executed].isFinished()) {
carousel[executed] = null;
}
executed++;
return true;
}
}
}