Hello, for this assignment, we have to imitate SW Airline's boarding process. I think I have everything working so far, except for the fact that my program ignores every Passenger entered into the queue except the first one. It's almost as if it bypasses the if statement asking if the queue is empty. Any help would be greatly appreciated.
import java.util.ArrayList;
public class LinkedQueue implements PriorityQueue<Passenger>{
private ArrayList<LinkedNode> Q = new ArrayList<LinkedNode>();
/**
* Is there anything in the queue?
* @return true if the queue is empty; otherwise return false
*/
public boolean isEmpty(){
if(Q.size() == 0){
return true;
}
else if(Q.size() > 0){
return false;
}
else{
return false;
}
}
/**
* Add passenger to the queue (at the appropriate location).
* @param toInsert Passenger to be added
*/
public void enqueue(Passenger toInsert){
if(Q.isEmpty() == true){
LinkedNode<Passenger> thisToInsert = new LinkedNode<Passenger>(toInsert);
Q.add(thisToInsert);
}
if(Q.isEmpty() == false){
LinkedNode<Passenger> thisToInsert = new LinkedNode<Passenger>(toInsert);
for(int i = 0; i == Q.size(); i++){
int compared = thisToInsert.getValue().compareTo(Q.get(i).getValue());
if(compared < 0){
thisToInsert.changeNext(Q.get(i));
Q.add(i, thisToInsert);
Q.get(i - 1).changeNext(thisToInsert);
}
if(i == (Q.size() - 1)){
Q.add(Q.size(), thisToInsert);
}
}
}
}
/**
* Removes and returns the item at the front of the queue.
* @return the removed element
*/
public Passenger dequeue() {
LinkedNode nextNode = Q.get(0);
Passenger nextPass = nextNode.getValue();
Q.remove(0);
return nextPass;
}
}
That's the offending program.
How should I fix this?