-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpa_lab_5_3_10_(6)-B.cpp
117 lines (104 loc) · 2.84 KB
/
cpa_lab_5_3_10_(6)-B.cpp
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
#include <iostream>
#include <vector>
#include <bits/stdc++.h>
using namespace std;
class FlightBooking {
public:
FlightBooking(int id, int capacity, int reserved = 0);
void printStatus();
bool reserveSeats(int number_ob_seats);
bool cancelReservations(int number_ob_seats);
int getID() { return id; };
private:
int id;
int capacity;
int reserved;
};
FlightBooking::FlightBooking(int id, int capacity, int reserved)
{
this->id = id;
if (reserved<0)
this->reserved = 0;
if (reserved * 100 / capacity>105)
this->reserved = 105 * capacity / 100;
else
this->reserved = reserved;
this->capacity = capacity;
}
bool FlightBooking::reserveSeats(int number_ob_seats)
{
if ((reserved + number_ob_seats) * 100 / capacity > 105)
return false;
reserved += number_ob_seats;
return true;
}
bool FlightBooking::cancelReservations(int number_ob_seats)
{
if (reserved < number_ob_seats)
return false;
reserved -= number_ob_seats;
return true;
}
void FlightBooking::printStatus()
{
cout << "Flight " << id << " : " << reserved << "\\" << capacity << " ("
<< reserved * 100 / capacity << "%) seats reserved" << endl;
}
int main() {
vector<FlightBooking> booking;
string temp;
string command = "";
int i, id, capacity, n;
while (command != "quit")
{
cout << "What would you like to do?: ";
getline(cin, command);
i = -1;
if (command.find("add") != -1) {
command = command.substr(command.find(" ") + 1);
id = atoi(command.c_str());
n = atoi(command.substr(command.find(" ")).c_str());
while (++i < booking.size())
if (booking[i].getID() == id) break;
if (i == booking.size() || !booking[i].reserveSeats(n)) {
cout << "Cannot perform this operation" << endl;
};
}
if (command.find("cancel") != -1) {
command = command.substr(command.find(" ") + 1);
id = atoi(command.c_str());
n = atoi(command.substr(command.find(" ")).c_str());
while (++i < booking.size())
if (booking[i].getID() == id) break;
if (i == booking.size() || !booking[i].cancelReservations(n)) {
cout << "Cannot perform this operation" << endl;
};
}
if (command.find("create") != -1) {
try {
command = command.substr(command.find(" ") + 1);
id = atoi(command.c_str());
capacity = atoi(command.substr(command.find(" ")).c_str());
booking.push_back(FlightBooking(id, capacity));
}
catch(...)
{
cout << "Cannot perform this operation" << endl;
}
}
if (command.find("delete") != -1) {
id = atoi(command.substr(command.find(" ")).c_str());
while (++i < booking.size())
if (booking[i].getID() == id) break;
if (i == booking.size())
cout << "Cannot perform this operation" << endl;
else
booking.erase(booking.begin() + i);
}
for (int i = 0; i < booking.size(); ++i)
{
booking[i].printStatus();
}
}
return 0;
}