-
Notifications
You must be signed in to change notification settings - Fork 0
/
waggon.cc
137 lines (108 loc) · 3.59 KB
/
waggon.cc
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
/*************************************************************************
Implementation File : waggon.cc
Author - Date : Efstathios Siatras - 18/12/2017
Purpose : Implementation file of class Waggon
*************************************************************************/
#include <iostream>
#include <cstdlib>
#include "waggon.h"
using namespace std;
Waggon::Waggon(const int &maxC)
: maxCapacity(maxC), seatsArray(NULL), numberOfPassengers(0), foundOffenders(0), notFoundOffenders(0), inspection(0) {
seatsArray = new Passenger*[maxCapacity];
for (int i = 0; i < maxCapacity; i++) { // Fills array with NULL
seatsArray[i] = NULL;
}
cout << "A waggon with capacity for " << maxCapacity << " passengers, was created" << endl;
}
Waggon::~Waggon() {
cout << "A waggon was destroyed" << endl;
for (int i = 0; i < maxCapacity; i++) { // Frees passengers in array
if(seatsArray[i] != NULL) {
delete seatsArray[i];
seatsArray[i] = NULL;
}
}
delete[] seatsArray; // Frees array
seatsArray = NULL;
}
void Waggon::inStation(const int &remainingStations) {
if (inspection != 0) { // If there is an inspector
for (int i = 0; i < maxCapacity; i++) { // Checks all passengers
if (seatsArray[i] != NULL) {
if (seatsArray[i]->get_hasTicket() == 0) { // Checks if passenger has ticket
delete seatsArray[i]; // Gets offender off
seatsArray[i] = NULL;
numberOfPassengers--;
}
}
}
}
if (remainingStations != 1) { // If the train is not at the last station
int getOff; // Number of people who are getting off the train
int getOn; // Number of people who are getting on the train
getOff = rand() % (numberOfPassengers + 1); // 0 <= getOff <= numberOfPassengers
int i = 0;
while ((i < maxCapacity) && (getOff != 0)) { // Gets off passengers
if (seatsArray[i] != NULL) {
delete seatsArray[i];
seatsArray[i] = NULL;
getOff--;
numberOfPassengers--;
}
i++;
}
getOn = rand() % (maxCapacity - numberOfPassengers + 1); // 0 <= getOn <= (maxCapacity - numberOfPassengers)
i = 0;
while ((i < maxCapacity) && (getOn != 0)) { // Gets on passengers
if (seatsArray[i] == NULL) {
seatsArray[i] = new Passenger;
getOn--;
numberOfPassengers++;
}
i++;
}
inspection = rand() % 2; // Decides if there will be an inspector
}
else { // The train is at the last station
for (int i = 0; i < maxCapacity; i++) { // Gets off all passengers
if (seatsArray[i] != NULL) {
delete seatsArray[i];
seatsArray[i] = NULL;
numberOfPassengers--;
}
}
}
}
void Waggon::betweenStations(int &money, const int &fullFine, const int &reducedFine) {
if (inspection != 0) { // If there is an inspector
for (int i = 0; i < maxCapacity; i++) { // Checks all passengers
if (seatsArray[i] != NULL) {
if (seatsArray[i]->get_hasTicket() == 0) { // Checks if passenger has ticket
foundOffenders++; // Finds founders
if (seatsArray[i]->get_reducedTicket() == 0) {
money += fullFine; // Full fine for lack of full ticket
}
else {
money += reducedFine; // Reduced fine for lack of reduced ticket
}
}
}
}
}
else { // There is no inspector
for (int i = 0; i < maxCapacity; i++) {
if (seatsArray[i] != NULL) {
if (seatsArray[i]->get_hasTicket() == 0) {
notFoundOffenders++; // Offenders that have not been found
}
}
}
}
}
void Waggon::printStatistics(const int &i) const {
cout << endl;
cout << "--------- Waggon " << i+1 << " ---------" << endl;
cout << "Offenders found: " << foundOffenders << endl;
cout << "Offenders not found: " << notFoundOffenders << endl;
}