-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathECElevatorSim.h
More file actions
131 lines (113 loc) · 4.39 KB
/
ECElevatorSim.h
File metadata and controls
131 lines (113 loc) · 4.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
//
// ECElevatorSim.h
//
//
// Created by Yufeng Wu on 6/27/23.
// Elevator simulation
#ifndef ECElevatorSim_h
#define ECElevatorSim_h
#include <iostream>
#include <set>
#include <vector>
#include <map>
#include <string>
//*****************************************************************************
// DON'T CHANGE THIS CLASS
//
// Elevator simulation request:
// (i) time: when the request is made
// (ii) floorSrc: which floor the user is at at present
// (iii) floorDest floor: where the user wants to go; we assume floorDest != floorSrc
//
// Note: a request is in three stages:
// (i) floor request: the passenger is waiting at floorSrc; once the elevator arrived
// at the floor (and in the right direction), move to the next stage
// (ii) inside request: passenger now requests to go to a specific floor once inside the elevator
// (iii) Once the passenger arrives at the floor, this request is considered to be "serviced"
//
// two sspecial requests:
// (a) maintenance start: floorSrc=floorDest=-1; put elevator into maintenance
// starting at the specified time; elevator starts at the current floor
// (b) maintenance end: floorSrc=floorDest=0; put elevator back to operation (from the current floor)
class ECElevatorSimRequest
{
public:
ECElevatorSimRequest(int timeIn, int floorSrcIn, int floorDestIn)
: time(timeIn), floorSrc(floorSrcIn), floorDest(floorDestIn),
fFloorReqDone(false), fServiced(false), timeArrive(-1) {}
int GetTime() const { return time; }
int GetFloorSrc() const { return floorSrc; }
int GetFloorDest() const { return floorDest; }
bool IsGoingUp() const { return floorDest >= floorSrc; }
bool IsFloorRequestDone() const { return fFloorReqDone; }
void SetFloorRequestDone(bool f) { fFloorReqDone = f; }
bool IsServiced() const { return fServiced; }
void SetServiced(bool f) { fServiced = f; }
int GetRequestedFloor() const;
int GetArriveTime() const { return timeArrive; }
void SetArriveTime(int t) { timeArrive = t; }
private:
int time; // time of request made
int floorSrc; // which floor the request is made
int floorDest; // which floor is going
bool fFloorReqDone; // is this passenger passing stage one (no longer waiting at the floor) or not
bool fServiced; // is this request serviced already?
int timeArrive; // when the user gets to the desitnation floor
};
//*****************************************************************************
// Elevator moving direction
typedef enum
{
EC_ELEVATOR_STOPPED = 0, // not moving
EC_ELEVATOR_UP, // moving up
EC_ELEVATOR_DOWN // moving down
} EC_ELEVATOR_DIR;
//*****************************************************************************
// Add your own classes here...
// Abstract Base Class for Elevator
class ElevatorBase
{
public:
ElevatorBase(int numFloors) : numFloors(numFloors), currFloor(1), currDir(EC_ELEVATOR_STOPPED) {}
virtual ~ElevatorBase() {}
virtual void Simulate(int lenSim) = 0;
virtual void MoveToFloor(int targetFloor) = 0;
int GetCurrFloor() const { return currFloor; }
void SetCurrFloor(int f) { currFloor = f; }
EC_ELEVATOR_DIR GetCurrDir() const { return currDir; }
void SetCurrDir(EC_ELEVATOR_DIR dir) { currDir = dir; }
protected:
int numFloors;
int currFloor;
EC_ELEVATOR_DIR currDir;
};
//*****************************************************************************
// Simulation of elevator
class ECElevatorSim : public ElevatorBase
{
public:
ECElevatorSim(int numFloors, std::vector<ECElevatorSimRequest> &listRequests);
~ECElevatorSim();
void Simulate(int lenSim) override;
void MoveToFloor(int targetFloor) override;
const std::vector<ECElevatorSimRequest *> &GetPassengersInCabin() const {
return passengersInCabin;
}
const std::vector<ECElevatorSimRequest *> &GetPendingRequests() const {
return pendingRequests;
}
private:
// Your code here
int timeElapsed;
std::vector<ECElevatorSimRequest *> pendingRequests;
std::vector<ECElevatorSimRequest> &listRequests;
// New member variables
std::vector<ECElevatorSimRequest *> passengersInCabin;
// New member functions
void CollectRequests(int currentTime);
void MoveOneFloor();
void DecideDirection();
bool HasFurtherRequestsInCurrentDirection();
bool HandlePassengers();
};
#endif /* ECElevatorSim_h */