Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program in C++ to develop an algorithm to simulate a single elevator in

ID: 3697865 • Letter: W

Question

Write a program in C++ to develop an algorithm to simulate a single elevator in a 10-story building. There will be some simplifying assumptions that will make this a bit less complex than a real-world implementation. The number of floors is not important, nor is the number of people getting on or getting off the elevator. The following 6 Rules apply to movement of the elevator:

1. By default, the elevator will always begin to operate from the 1st floor.

2. The algorithm needs to support the 2 separate directions of up and down.

3. The algorithm needs to support the 2 separate requirements of “get on” and “get off”.

4. Wherever the elevator happens to be, it always continues in the same direction, unless there are no passengers and no people waiting for the elevator with “get on” requests.

5. When the elevator stops moving in one direction, it checks to see if there are additional “get on” requests. The elevator does not move again until it receives a request to move.

6. Once the elevator starts to move, there can be no “get on” requests until the elevator has stopped and has no more pickups or drop-offs in the same direction. In other words, the elevator does not move in the opposite direction until a request to move is made.

The approach to simulating the elevator is as follows:

1. The events needed to simulate are described below. The 2 event types are ‘GET ON’ and ‘MOVE. Since they are fixed, you can populate them into an array if you like. The array should be 2-dimensional, since each event will have 3 pieces of information, as described by #2.

2. ‘GET ON’ has 2 parameters: the number of the floor on which a person or persons will enter the elevator and the number of the floor on which the person(s) will get off. ‘MOVE’ has no parameters but you can assign 2 parameters with values of zeros so that the 2 events are parallel.

3. Implement the elevator as a Doubly-linked list. When the elevator is moving up, it follows from the head to determine where to go to next. When it is moving down, it follows from the tail backwards.

4. When there are no more nodes in the same direction as the elevator is moving, it checks the array for its next requests and either moves or adds “get on” requests. It then continues to move in the same direction it was moving, if there are now requests for that direction, or it reverses direction to pick up people. If the entire list is empty, it returns to the 1st floor to wait.

5. The doubly-linked list nodes need to contain two items of information: a primary floor number to stop at and, for an arriving passenger, a secondary floor number representing the “get off”. You cannot add the “get off” as a node until the passenger has gotten on. Otherwise, you may try to discharge a passenger who is not yet on the elevator.

6. To summarize, when you pick up a ‘GET ON’ request, add it to the list, along with the ‘get off’, as an additional parameter. When the ‘GET ON’ is satisfied, you remove its node and add the ‘get off’ node.

Use the following EVENT LIST :

GET ON 4,6 (means a person wants to get on at 4 and get off at 6)

MOVE                                  

GET ON 8,2

GET ON 1,5

GET ON 9,3

MOVE

GET ON 10,2

GET ON 7,4

MOVE

GET ON 1,7

MOVE

NOTE: “MOVE” makes the elevator move (a) in the same direction if there are requests, (b) in the opposite direction if there are requests or (c) down to 1 if there are no requests in the list.

The output from this program should identify each of the elevator stops and whether people get off, get on, or both. An ouput should be produced when a MOVE instruction is encountered. In other words, there should be 4 separate outputs, each identifying a set of floors and “get on” or “get off”.

PLEASE answer the complete question.

Explanation / Answer

class Floor { goUp() { ElevatorManager.queue.offer(new Request(currentFloor, destinationFloor, up)); } goDown() { ElevatorManager.queue.offer(new Request(currentFloor, destinationFloor, down)); } } ElevatorManager { delegate() { // Instead of using one object, we could use a list to track idle and elevators moving in same direction so that these list could be used for next requests in queue // but again to simplify pseudocode, I am using single objects instead of lists Elevator idleElevator; // track idle elevator Elevator elevatorMovingInSameDirection; // elevator moving in same direction as next request in main elevator manager queue while(!halt()) { //keep delegating until powered down or whole system is halted through main controls if(queue.peek() != null) { Request req = queue.peek(); boolean startAgain = false; // flag to start from beginning if the request is already pushed to one of the elevators queue during iterating elevators for(Elevator elevator : elevators) { // first find if there is an elevator at current floor going in same direction as current request in queue if(req.currentFloor == elevator.currentFloor && req.direction == elevator.direction) { elevator.queue.offer(req.destinationFloor); queue.poll(); // remove this request from Elevator Manager queue startAgain = true; break; } // check if this elevator is idle if(elevator.direction == "idle")) { idleElevator = elevator; // For this simple design, I am ok to overwrite idle elevator value and instead get the latest idle elevatior } // check if this elevator is moving in desired direction and elevator's current floor is behind desired floor in queue if(elevator.direction == req.direction) { // Make sure elevators moving in same direction should also be behind the floor where request is made if(req.direction == "Up" && req.currentFloor - elevator.currentFloor > 0) { elevatorMovingInSameDirection = elevator; // Same as above, it's ok to get this overwritten and instead get the latest elevator moving in same direction } // Make sure elevators moving in same direction should also be behind the floor where request is made if(req.direction == "Down" && req.currentFloor - elevator.currentFloor < 0) { elevatorMovingInSameDirection = elevator; } } } // Only delegate to other floors if you could not find elevator going in same direction at same floor from where the request was made if(!startAgain && idleElevator != null) { idleElevator.queue.offer(req.destinationFloor); queue.poll(); } // if we could neither find elevator at current floor nor idle elevator then send this request to elevator behind current Floor and moving in same direction as the request if(!startAgain && elevatorMovingInSameDirection != null) { elevatorMovingInSameDirection.queue.offer(req.destinationFloor); queue.poll(); } } } } } Elevator { moveUp() { this.currentFloor += 1; } moveDown() { this.currentFloor -= 1; } operate() { while(queue.peek() != null) { Floor nextFloorInQueue = queue.peek(); while(this.currentFloor != nextFloorInQueue.request.destinationFloor) { if(this.direction == "Up") { moveUp(); } else if(this.direction == "down") { moveDown(); } } queue.poll(); // remove the request from queue open(); //open door Direction backUpDirection = this.direction; //back up elevators direction to retrieve it later once dooor closes this.direction = "idle"; // set state to idle to let elevatorManager know that requests at current floor could be offered to this elevator queue Thread.sleep(10000); // sleep for 10 seconds so that people can leave elevator close(); // once people are out close door to move to next floor in queue this.direction = backUpDirection; } this.direction = "idle"; // once queue is empty set the direction to idle } }
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote