-
Notifications
You must be signed in to change notification settings - Fork 0
/
TrainTicketSystem.cpp
277 lines (233 loc) · 7.93 KB
/
TrainTicketSystem.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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <memory> // For smart pointers
using namespace std;
enum class SeatStatus { Yes, No };
enum class ClassType { FirstClass, NormalClass };
enum class TicketType { Discounted, Normal, Sleep };
class Train {
public:
virtual void Print_Info() const = 0;
virtual double Calc_Price() const = 0;
virtual ~Train() = default; // Virtual destructor for proper cleanup
protected:
ClassType ID;
};
class Fast_Train : public Train {
public:
Fast_Train(ClassType classType, SeatStatus seatStatus, int seatNumber)
: ID(classType), Seat_Reserved(seatStatus), Seat_Number(seatNumber), Revenue(50) {}
void Print_Info() const override {
cout << "Ticket type: " << GetClassTypeString(ID)
<< ", Seat reserved: " << GetSeatStatusString(Seat_Reserved)
<< ", Seat number: " << Seat_Number
<< ", Price: " << Calc_Price() << " BGN" << endl;
}
double Calc_Price() const override {
double basePrice = (ID == ClassType::FirstClass) ? Revenue * 1.5 : Revenue;
if (Seat_Reserved == SeatStatus::No) {
basePrice += (ID == ClassType::FirstClass) ? 5 : 2;
}
return basePrice;
}
private:
ClassType ID;
SeatStatus Seat_Reserved;
int Seat_Number;
double Revenue;
string GetClassTypeString(ClassType type) const {
switch (type) {
case ClassType::FirstClass: return "First Class";
case ClassType::NormalClass: return "Normal Class";
default: return "Unknown";
}
}
string GetSeatStatusString(SeatStatus status) const {
return (status == SeatStatus::Yes) ? "Yes" : "No";
}
};
class Passenger_Train : public Train {
public:
Passenger_Train(TicketType ticketType)
: ID(ticketType), Price(5) {}
void Print_Info() const override {
cout << "Ticket type: " << GetTicketTypeString(ID)
<< ", Price: " << Calc_Price() << " BGN" << endl;
}
double Calc_Price() const override {
return (ID == TicketType::Discounted) ? Price / 2 : Price;
}
private:
TicketType ID;
double Price;
string GetTicketTypeString(TicketType type) const {
switch (type) {
case TicketType::Discounted: return "Discounted";
case TicketType::Normal: return "Normal";
default: return "Unknown";
}
}
};
class Night_Train : public Train {
public:
Night_Train(int seatNumber, SeatStatus seatStatus, TicketType ticketType, bool breakfastIncluded)
: Seat_Number(seatNumber), Seat_Reserved(seatStatus), ID(ticketType), Breakfast(breakfastIncluded), Price(15) {}
void Print_Info() const override {
cout << "Ticket type: " << GetTicketTypeString(ID)
<< ", Seat reserved: " << GetSeatStatusString(Seat_Reserved)
<< ", Seat number: " << Seat_Number
<< ", Breakfast: " << (Breakfast ? "Yes" : "No")
<< ", Price: " << Calc_Price() << " BGN" << endl;
}
double Calc_Price() const override {
return Breakfast ? Price * 1.2 : Price;
}
private:
int Seat_Number;
SeatStatus Seat_Reserved;
TicketType ID;
bool Breakfast;
int Price;
string GetTicketTypeString(TicketType type) const {
switch (type) {
case TicketType::Sleep: return "Sleep";
default: return "Unknown";
}
}
string GetSeatStatusString(SeatStatus status) const {
return (status == SeatStatus::Yes) ? "Yes" : "No";
}
};
class Passengers {
public:
void Add(unique_ptr<Train> train) {
Queue.push_back(move(train));
}
void List() const {
if (Queue.empty()) {
cout << "The queue is empty.\n";
return;
}
cout << "Current queue:\n";
for (const auto& train : Queue) {
train->Print_Info();
}
}
void Process() {
if (Queue.empty()) {
cout << "The queue is empty.\n";
return;
}
cout << "Processing:\n";
Queue.front()->Print_Info();
cout << "Cost for ticket: " << Queue.front()->Calc_Price() << " BGN" << endl;
Queue.erase(Queue.begin());
}
double Total() const {
double sum = 0;
for (const auto& train : Queue) {
sum += train->Calc_Price();
}
return sum;
}
private:
vector<unique_ptr<Train>> Queue;
};
ClassType GetClassTypeFromUser() {
int input;
cout << "Would you like to travel in First Class?\n(1) Yes\n(2) No\n";
cin >> input;
while (input != 1 && input != 2) {
cout << "Invalid input\nSelect again: ";
cin >> input;
}
return (input == 1) ? ClassType::FirstClass : ClassType::NormalClass;
}
SeatStatus GetSeatStatusFromUser() {
int input;
cout << "Would you like to reserve a seat? \n(1) Yes\n(2) No\n";
cin >> input;
while (input != 1 && input != 2) {
cout << "Invalid input\nSelect again: ";
cin >> input;
}
return (input == 1) ? SeatStatus::Yes : SeatStatus::No;
}
TicketType GetTicketTypeFromUser() {
int input;
cout << "What kind of ticket would you like?\n(1) Discounted (For pupils, students and pensioners)\n(2) Normal\n";
cin >> input;
while (input != 1 && input != 2) {
cout << "Invalid input\nSelect again: ";
cin >> input;
}
return (input == 1) ? TicketType::Discounted : TicketType::Normal;
}
bool GetBreakfastOptionFromUser() {
int input;
cout << "Would you like breakfast? (3 BGN)\n(1) Yes\n(2) No\n";
cin >> input;
while (input != 1 && input != 2) {
cout << "Invalid input\nSelect again: ";
cin >> input;
}
return (input == 1);
}
int main() {
ofstream file1("C:\\Data\\Output.txt");
if (!file1.is_open()) {
cout << "File not found!\n";
return -1;
}
Passengers passenger;
int choice;
int nightSeatNumber = 1, fastSeatNumber = 1;
double sum = 0;
do {
cout << "\tMenu\n=====================\n";
cout << "(1) Fast train\n(2) Passenger train\n(3) Sleep compartment\n";
cout << "(4) Show queue\n(5) Process payment\n(6) Expected revenue\n(0) Exit\n";
cin >> choice;
switch (choice) {
case 1: {
ClassType classType = GetClassTypeFromUser();
SeatStatus seatStatus = GetSeatStatusFromUser();
auto train = make_unique<Fast_Train>(classType, seatStatus, fastSeatNumber);
passenger.Add(move(train));
if (seatStatus == SeatStatus::Yes) fastSeatNumber++;
break;
}
case 2: {
TicketType ticketType = GetTicketTypeFromUser();
auto train = make_unique<Passenger_Train>(ticketType);
passenger.Add(move(train));
break;
}
case 3: {
bool breakfast = GetBreakfastOptionFromUser();
auto train = make_unique<Night_Train>(nightSeatNumber++, SeatStatus::Yes, TicketType::Sleep, breakfast);
passenger.Add(move(train));
break;
}
case 4:
passenger.List();
break;
case 5:
passenger.Process();
break;
case 6:
cout << "Total revenue: " << passenger.Total() << " BGN" << endl;
file1 << "Total revenue: " << passenger.Total() << " BGN" << endl;
break;
case 0:
break;
default:
cout << "Invalid input!\n";
break;
}
} while (choice != 0);
file1.close();
return 0;
}