-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparkinglot.cxx
163 lines (150 loc) · 3.34 KB
/
parkinglot.cxx
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/* An object-oriented parking lot. */
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class lot;
class spot;
class vehicle;
class lot {
public:
lot(int n);
~lot();
void print();
int park(vehicle*);
int leave(vehicle*);
void setVerbose(bool verbose) {
verbose_ = verbose;
}
private:
vector<spot> spots;
bool verbose_;
};
class spot {
public:
spot() : v(NULL) {}
~spot() { leave(); }
int park(vehicle*);
int leave();
vehicle* getVehicle() { return v; }
private:
vehicle *v;
};
class vehicle {
public:
vehicle(std::string newdescription) : description(newdescription) {}
~vehicle() {}
std::string getDescription() { return description; }
private:
std::string description;
};
lot::lot(int n) : spots(n, spot()) {}
lot::~lot() {
for (vector<spot>::iterator it = spots.begin(); it != spots.end(); ++it) {
it->leave();
}
}
void lot::print() {
if (spots.size() == 0) {
cout << "Zero sized lot.";
} else {
for (int i = 0 ; i < spots.size() ; ++i) {
if (spots[i].getVehicle() == NULL)
cout << "Spot " << i << " empty; ";
else
cout << "Spot " << i << " has " << spots[i].getVehicle()->getDescription() << "; ";
}
}
cout << endl;
}
int lot::park(vehicle *v) {
for (vector<spot>::iterator it = spots.begin(); it != spots.end(); ++it) {
if (it->park(v)) {
if(verbose_)
cout << "Successfully parked " << v->getDescription() << "." << endl;
return true;
}
}
if(verbose_)
cout << "Failed to park " << v->getDescription() << "." << endl;
return false;
}
int lot::leave(vehicle *v) {
int retval;
std::string description = v->getDescription();
for (vector<spot>::iterator it = spots.begin(); it != spots.end(); ++it) {
if((it->getVehicle() != NULL) && (v->getDescription().compare(it->getVehicle()->getDescription()) == 0)) {
retval = it->leave();
if(verbose_) {
if(retval)
cout << description << " successfully left." << endl;
else
cout << description << " cannot leave: internal error." << endl;
}
return retval;
}
}
if(verbose_)
cout << v->getDescription() << " not found in the parking lot." << endl;
return false;
}
int spot::park(vehicle *newvehicle) {
if (v == NULL) {
v = newvehicle;
return true;
} else {
return false;
}
}
int spot::leave() {
if (v != NULL) {
v = NULL;
return true;
} else {
return false;
}
}
int main() {
lot *myLot = new lot(3);
myLot->setVerbose(true);
vehicle *v1 = new vehicle("Prius");
vehicle *v2 = new vehicle("Insight");
vehicle *v3 = new vehicle("Leaf");
myLot->print();
myLot->park(v1);
myLot->print();
myLot->park(v2);
myLot->print();
myLot->park(v3);
myLot->print();
myLot->leave(v2);
myLot->print();
myLot->leave(v3);
myLot->print();
myLot->leave(v1);
myLot->print();
myLot->leave(v2);
myLot->print();
delete myLot;
myLot = new lot(1);
myLot->print();
myLot->park(v1);
myLot->print();
myLot->park(v2);
myLot->print();
myLot->leave(v2);
myLot->print();
myLot->leave(v1);
myLot->print();
delete myLot;
myLot = new lot(0);
myLot->print();
myLot->park(v1);
myLot->print();
myLot->leave(v1);
myLot->print();
myLot->leave(v2);
myLot->print();
delete myLot;
return 0;
}