I have a method need to implement which ask me to return the ship in a queue according to some conditions
/** Returns the next ship waiting to enter the port. The queue should not change.
*
* The rules for determining which ship in the queue should be returned next are as
* follows:
* <ul>
* <li>If a ship is carrying dangerous cargo, it should be returned. If more than one
* ship is carrying dangerous cargo return the one added to the queue first.</li>
*
* <li>If a ship requires medical assistance, it should be returned. If more than one
* ship requires medical assistance, return the one added to the queue first.</li>
*
* <li>If a ship is ready to be docked, it should be returned. If more than one ship is
* ready to be docked, return the one added to the queue first.</li>
*
* <li>If there is a container ship in the queue, return the one added to the queue first.</li>
*
* <li>If this point is reached and no ship has been returned, return the ship that was
* added to the queue first.</li>
*
* <li>If there are no ships in the queue, return null.</li>
* </ul>
* @return next ship in queue
* */
public Ship peek() {
Queue<Ship> newShips = new LinkedList<>(ships); // Make a copy of the queue and change here
for (Ship s : newShips) {
if (s.getFlag() == NauticalFlag.BRAVO) { //ship carrying dangerous cargo
return s;
} else if (s.getFlag() == NauticalFlag.WHISKEY) { // ship requires medical assistance
return s;
} else if (s.getFlag() == NauticalFlag.HOTEL) { // ship is ready to be docked
return s;
} else if (s.getName() == "ContainerShip") { // Container Ship
return s;
} else {
return poll(); // ship that was added to the queue first
}
}
if (newShips.isEmpty()) {
return null;
}
}
I am stuck on returning the ship in the if else statement in the for loop. And also how can I know if the condition for example carrying dangerous cargo
= NauticalFlag.BRAVO
have more than one ship in the queue and return the one added to queue first.