-
Notifications
You must be signed in to change notification settings - Fork 0
/
TrainTickets - Basics.cpp
399 lines (344 loc) · 8.86 KB
/
TrainTickets - Basics.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
#include <iostream>
#include <string>
using namespace std;
//class that describes the behavior of a train-type object
class Tren {
private:
const int nrTren; //constant attribute - unique train number
string ruta; // describe train's route
int nrStatii; //additional variable, added for crossing the stations vector
string * statii; //dynamic array with the name of the stations
int nrCalatori; //additional variable, added for crossing the prices vector
float pret[20]; //static array containing prices paid by the travellers for the ticket
//for exemplify the way of working of the program, I will consider a maximum number of 20 travellers
//0 eur collected, represents the tickets with a full discount
bool rangTren; //bool variable for train type: Regio/InterRegio
string rang; //true=R, false=IR
public:
static int contor; //static variable that helps on creating a train number
//destructor
~Tren() {
if (this->statii != NULL)
delete[] this->statii;
this->contor--;
}
//4 types of constructor
//default constructor
Tren() : nrTren(contor++ + 1000) {
this->ruta = "Necunoscut";
this->nrStatii = 0;
this->nrCalatori = 0;
this->statii = NULL;
}
Tren(bool rangTren, string ruta, int nrStatii, string * statii) : nrTren(contor++ + 1000), nrCalatori(0) {
this->rangTren = rangTren;
if (this->rangTren)
this->rang = "R";
else
this->rang = "IR";
this->ruta = ruta;
this->nrStatii = nrStatii;
this->statii = new string[this->nrStatii];
for (int i = 0; i < this->nrStatii; i++)
this->statii[i] = statii[i];
}
Tren(bool rangTren, string ruta, int nrStatii, string * statii, int nrCalatori, float pret[10]) : nrTren(contor++ + 1000) {
this->rangTren = rangTren;
if (this->rangTren)
this->rang = "R";
else
this->rang = "IR";
this->ruta = ruta;
this->nrStatii = nrStatii;
this->statii = new string[this->nrStatii];
for (int i = 0; i < this->nrStatii; i++)
this->statii[i] = statii[i];
this->nrCalatori = nrCalatori;
for (int i = 0; i < nrCalatori; i++)
this->pret[i] = pret[i];
}
Tren(const Tren & t) : nrTren(t.nrTren) {
this->rang = t.rang;
this->rangTren = t.rangTren;
this->ruta = t.ruta;
this->nrStatii = t.nrStatii;
this->statii = new string[this->nrStatii];
if (statii != NULL) {
for (int i = 0; i < this->nrStatii; i++)
this->statii[i] = t.statii[i];
}
else
this->statii = NULL;
this->nrCalatori = t.nrCalatori;
for (int i = 0; i < this->nrCalatori; i++)
this->pret[i] = t.pret[i];
}
//override operator =
Tren operator = (const Tren & t) {
this->rang = t.rang;
this->ruta = t.ruta;
this->nrStatii = t.nrStatii;
if (this->statii != NULL)
delete[] this->statii;
this->statii = new string[this->nrStatii];
if (statii != NULL) {
for (int i = 0; i < this->nrStatii; i++)
this->statii[i] = t.statii[i];
}
else
this->statii = NULL;
this->nrCalatori = t.nrCalatori;
for (int i = 0; i < this->nrCalatori; i++)
this->pret[i] = t.pret[i];
return *this;
}
//getters and setters
int getNrTen() {
return this->nrTren;
}
bool getRangTren() {
return this->rangTren;
}
void setRangTren(bool rangTren) {
this->rangTren = rangTren;
if (this->rangTren)
this->rang = "R";
else
this->rang = "IR";
}
string getRuta() {
return this->ruta;
}
void setRuta(string ruta) {
this->ruta = ruta;
}
int getNrStatii() {
return this->nrStatii;
}
void setNrStatii(int nrStatii) {
this->nrStatii = nrStatii;
}
void getStatii() {
if (this->statii != NULL)
for (int i = 0; i < this->nrStatii; i++)
cout << this->statii[i] << " ";
}
void setStatii(string * statii) {
if (this->statii != NULL)
delete[] this->statii;
this->statii = new string[this->nrStatii];
for (int i = 0; i < this->nrStatii; i++)
this->statii[i] = statii[i];
}
int getNrCalatori() {
return this->nrCalatori;
}
void setNrCalatori(int nrCalatori) {
this->nrCalatori = nrCalatori;
}
void getPret() {
for (int i = 0; i < this->nrCalatori; i++)
cout << this->pret[i] << " ";
}
void setPret(float pret[20]) {
for (int i = 0; i < this->nrCalatori; i++)
this->pret[i] = pret[i];
}
//2 methods
float incasariMedii() {
float s = 0;
for (int i = 0; i < this->nrCalatori; i++)
s += this->pret[i];
return s / this->nrCalatori;
}
float incasariTotale() {
float sum = 0;
if (this->nrCalatori > 0) {
for (int i = 0; i < this->nrCalatori; i++)
sum += this->pret[i];
return sum;
}
}
//10 overrides
Tren operator + (float val) {
this->nrCalatori++;
if (this->nrCalatori < 20)
this->pret[this->nrCalatori - 1] = val;
return *this;
}
Tren operator += (string val) {
string * copie;
copie = new string[this->nrStatii + 1];
for (int i = 0; i < this->nrStatii; i++)
copie[i] = this->statii[i];
if (this->statii != NULL)
delete[] this->statii;
this->nrStatii++;
this->statii = new string[this->nrStatii];
for (int i = 0; i < this->nrStatii; i++)
this->statii[i] = copie[i];
this->statii[this->nrStatii - 1] = val;
return *this;
}
Tren operator * (int val) {
this->pret[this->nrCalatori - 1] *= 2;
return *this;
}
float operator()() {
float max = 0;
for (int i = 0; i < this->nrCalatori; i++)
if (this->pret[i] > max)
max = this->pret[i];
return max;
}
explicit operator float() {
float min = 0;
for (int i = 0; i < this->nrCalatori; i++)
if (min > this->pret[i])
min = this->pret[i];
return min;
}
string operator[](int i) {
return this->statii[i];
}
Tren operator++() {
this->pret[this->nrCalatori - 1] += 10;
return *this;
}
Tren operator++(int) {
Tren copie = *this;
this->pret[this->nrCalatori - 1] += 10;
return copie;
}
bool operator <= (Tren & t) {
if (this->incasariTotale() <= t.incasariTotale())
return 0;
else
return 1;
}
bool operator!() {
if (!this->nrCalatori)
return 0;
else
return 1;
}
friend ostream & operator << (ostream & out,
const Tren & t);
friend istream & operator >> (istream & in, Tren & t);
};
//override << and >>
ostream & operator << (ostream & out,
const Tren & t) {
out << t.rang << t.nrTren << " circula pe ruta " << t.ruta << ". Trenul va opri in statiile: ";
for (int i = 0; i < t.nrStatii; i++)
out << " " << t.statii[i];
out << "\nDe pe urma celor " << t.nrCalatori << " calatori s-au incasat sumele: ";
if (t.nrCalatori > 0) {
for (int i = 0; i < t.nrCalatori; i++)
out << " " << t.pret[i];
}
else out << "Fara bilete vandute!";
return out;
}
istream & operator >> (istream & in, Tren & t) {
cout << "\nRang tren(0-REGIO, 1-INTERREGIO): "; in >> t.rangTren; in.ignore();
cout << "Ruta: "; in >> t.ruta;
cout << "Nr. statii: "; in >> t.nrStatii; in.ignore();
if (t.statii != NULL)
delete[] t.statii;
t.statii = new string[t.nrStatii];
for (int i = 0; i < t.nrStatii; i++) {
cout << "Statie[" << i << "]: "; in >> t.statii[i];
}
cout << "Nr. Calatori: "; in >> t.nrCalatori;
for (int i = 0; i < t.nrCalatori; i++) {
cout << "Pret: "; in >> t.pret[i];
}
return in;
}
int Tren::contor = 0;
void main() {
string statii[]{
"Videle",
"Craiova",
"Orsova",
"Timisoara"
};
string statii2[]{
"Ploiesti",
"Campina",
"Brasov"
};
Tren t1(true, "Bucuresti-Brasov-Sibiu", 3, statii2);
float pret[7] = {
50.5,
70,
55,
40.6,
30,
32,
0
};
Tren t2(false, "Bucuresti-Timisoara", 4, statii, 7, pret);
cout << "\nStatii: ";
t2.getStatii();
cout << endl;
cout << t1.getRangTren() << endl;
cout << t1.getNrTen() << endl;
t1.setRangTren(false);
cout << t1.getRuta() << endl;
t2.setRuta("Bucuresti-Timsoara Nord");
cout << t2.getNrStatii() << endl;
t1.setNrStatii(4);
string statii3[]{
"Breaza",
"Sinaia",
"Predeal",
"Brasov"
};
t1.setStatii(statii3);
t2.setNrCalatori(8);
cout << t2.getNrCalatori() << endl;
float pret1[8] = {
50.5,
0,
55,
0,
40.6,
30,
0,
32
};
t2.setPret(pret1);
t2.getPret();
cout << endl << "Trenuri in circulatie: " << Tren::contor;
Tren t4(t1);
cout << endl << t4;
Tren t3 = t2;
t1 = t2;
t2.setRangTren(true);
cout << endl << t2;
t3 + 34.7; //add a tiket
t4 += "Sibiu"; //add a station
t2 * 2; //multiply with 2 the last price
cout << endl << t2;
cout << "\nIncasari totale: " << t2.incasariTotale();
cout << "\nIncasari medii: " << t2.incasariMedii();
cout << "\nPretul maxim: " << t2();
cout << "\nPretul minim: " << (float)(t2);
cout << endl << t2[3]; //Show the 3th station
Tren t5 = ++t2; //rise the last price with 10
cout << endl << t5;
Tren t6 = t2++; //the object t6 is created using t4, with copy constructor; after that, the last price is increased with 10
if (t5 <= t2)
cout << "\nIncasarile din t5 sunt mai mici sau egale decat incasarile din t2.";
else
cout << "\nIncasarile din t5 nu sunt mai mici sau egale decat incasarile din t2.";
if (!t3)
cout << "\nTrenul t3 nu are calatori.";
else
cout << "\nTrenul t3 are calatori.";
cin >> t2;
cout << t2;
cout << endl << endl << endl;
}