import java.util.*; public class RequestList { private ArrayList requestList; public RequestList() { requestList = new ArrayList(); } /** * This method adds a new request to the queue. * * @param reguest the new request to be added */ public void addPendingRequest(Request request) { int i = 0; while (i < requestList.size()) { if (request.getTime() < requestList.get(i).getTime()) { break; } else { i++; } } requestList.add(i, request); } /** * This method checks if there are any requests to summon an elevator * * @return true of there are any floors requesting an elevator, false otherwise */ public boolean hasPendingRequest() { if (requestList.size() > 0) { return true; } else { return false; } } /** * This method determines the next request based on timestamp. * * @return the floor number of the next request */ public int getFirstPendingRequest() { return requestList.get(0).getCallingFloor(); } /** * This method determines the next request based on proximity to the elevator's current floor. * * @param currentFloor the current floor of the elevator that is scanning for requests */ public int getClosestPendingRequest(int currentFloor) { int shortestDistance = Math.abs(currentFloor - requestList.get(0).getCallingFloor()); int nearestFloor = requestList.get(0).getCallingFloor(); for (int i = 1; i < requestList.size(); i++) { int distance = Math.abs(currentFloor - requestList.get(i).getCallingFloor()); if (distance < shortestDistance) { shortestDistance = distance; nearestFloor = requestList.get(i).getCallingFloor(); } } return nearestFloor; } /** * This method removes a request to a given floor from the queue. * @param floor the floor of the request to remove */ public void removePendingRequest(int floor) { Request request = null; for (int i = 0; i < requestList.size(); i++) { request = requestList.get(i); if (request.getCallingFloor() == floor) { break; } } requestList.remove(request); } }