-
Notifications
You must be signed in to change notification settings - Fork 0
/
passengers.cpp
262 lines (236 loc) · 8.23 KB
/
passengers.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
#include "passengers.h"
#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <utility>
using namespace std;
int Passengers::Count() {
return passengers.size();
}
bool Passengers::PassengerExists(int id) {
for (int i = 0; i < passengers.size(); i++) {
if (id == passengers.at(i).GetID()) {
return true;
break;
}
}
return false;
}
void Passengers::Add(char* f, char* l) {
PassengerInfo *newPassenger = (PassengerInfo*)malloc(sizeof(PassengerInfo));
int id, paymentPref; // id of passenger and their payment preference
float rating;
int isHandicapped, hasPets;
bool idAlreadyRegistered;
srand(time(NULL));
id = rand() % 900000 + 100000;
/* make ID */
do { // check if ID has already been made
idAlreadyRegistered = false;
for (int i = 0; i < passengers.size(); i++) {
if (id == passengers.at(i).GetID()) {
idAlreadyRegistered = true;
break;
}
}
id = rand() % 900000 + 100000;
} while (idAlreadyRegistered);
cout << "PASSENGER ID: " << id << endl;
/* prompt for payment preference */
cout << "0 - cash\n1 - credit\n2 - debit\nEnter the passengers's payment preference: ";
cin >> paymentPref;
/* check for validity of payement preference */
while (cin.fail() || (paymentPref > 2 || paymentPref < 0)) {
while (cin.fail()) {
cin.clear();
cin.ignore(100, '\n');
cout << "Invalid data type..." << endl;
cout << "0 - cash\n1 - credit\n2 - debit\nEnter the passengers's payment preference: ";
cin >> paymentPref;
}
while (paymentPref > 2 || paymentPref < 0) {
cout << "Invalid..." << endl;
cout << "0 - cash\n1 - credit\n2 - debit\nEnter the passengers's payment preference: ";
cin >> paymentPref;
}
}
/* prompt for passengers's rating */
cout << "Enter the passengers's default rating (0 - 5): ";
cin >> rating;
// check that rating is valid
while (cin.fail() || (rating > 5 || rating < 0)) {
while (cin.fail()) {
cin.clear();
cin.ignore(100, '\n');
cout << "Invalid data type..." << endl;
cout << "Enter the passenger's required rating (0 - 5): ";
cin >> rating;
}
while (rating > 5 || rating < 0) {
cout << "Invalid..." << endl;
cout << "Enter the passengers's required rating (0 - 5): ";
cin >> rating;
}
}
/* prompt for passengers's other details */
cout << "0 - no" << endl;
cout << "1 - yes" << endl;
cout << "Does this passenger have pets? 0 or 1: ";
cin >> hasPets;
while (cin.fail()) {
cin.clear();
cin.ignore(100, '\n');
cout << "Invalid data type..." << endl;
cout << "Does this passenger have pets? 0 or 1: ";
cin >> hasPets;
}
cout << "0 - no" << endl;
cout << "1 - yes" << endl;
cout << "Is this passenger handicapped? 0 or 1: ";
cin >> isHandicapped;
while (cin.fail()) {
cin.clear();
cin.ignore(100, '\n');
cout << "Invalid data type..." << endl;
cout << "Is this passenger handicapped? 0 or 1: ";
cin >> isHandicapped;
}
// call mutator functions
newPassenger->SetPets(hasPets);
newPassenger->SetHandicapped(isHandicapped);
newPassenger->SetName(f, l);
newPassenger->SetID(id);
newPassenger->SetPayment(paymentPref);
newPassenger->SetRating(rating);
passengers.push_back(*newPassenger);
free(newPassenger);
}
pair<int, PassengerInfo> Passengers::Find(int id, bool printName) {
int i;
for (i = 0; i < passengers.size(); i++) {
if (id == passengers.at(i).GetID()) {
if (printName) {
printf("Name: %s %s\t\tID: %d\n", passengers.at(i).GetFirstName(), passengers.at(i).GetLastName(), id); // print driver name and id
}
break;
}
}
return make_pair(i, passengers.at(i));
}
void Passengers::Delete(int id) {
int i = Find(id, false).first; // find index of driver
passengers.erase(passengers.begin() + i); // delete driver
}
void Passengers::PrintPassenger(int id) {
Find(id, false).second.Print();
}
void Passengers::PrintAll() {
cout << "THERE IS A TOTAL OF " << passengers.size() << " PASSENGERS" << endl;
for (int i = 0; i < passengers.size(); i++) {
cout << "- PASSENGER #" << i + 1 << " -" << endl;
passengers.at(i).Print();
cout << endl;
}
}
void Passengers::Edit(int option, int index) {
if (option == 1) { // edit payment preference
int newPaymentPref;
/* prompt for new vehicle type */
cout << "0 - cash" << endl << "1 - credit" << "2 - debit" << endl << "Enter the new payment preference for this passenger: ";
cin >> newPaymentPref;
while (cin.fail() || (newPaymentPref > 2 || newPaymentPref < 0)) {
while (cin.fail()) {
cout << "Invalid data type..." << endl;
cout << "0 - cash" << endl << "1 - credit" << "2 - debit" << endl << "Enter the new payment preference for this passenger: ";
cin.clear();
cin.ignore(100, '\n');
cin >> newPaymentPref;
}
while (newPaymentPref > 2 || newPaymentPref < 0) {
cout << "Invalid..." << endl << "0 - cash" << endl << "1 - credit" << endl << "2 - debit" << endl;
cout << "Enter this passenger's payment preference: ";
cin >> newPaymentPref;
}
}
passengers.at(index).SetPayment(newPaymentPref); // set new type
}
else if (option == 2) { // edit default rating
float newRating; // new rating for driver
/* prompt for new rating */
cout << "Enter the new default rating for this passenger: ";
cin >> newRating;
while (cin.fail() || (newRating < 0 || newRating > 5)) {
while (cin.fail()) {
cin.clear();
cin.ignore(100, '\n');
cout << "Invalid data type..." << endl;
cout << "Enter the new default rating for this passenger: ";
cin >> newRating;
}
while (newRating < 0 || newRating > 5) {
cout << "Invalid...Rating must be between 0 and 5: ";
cin >> newRating;
}
} // check for validity
passengers.at(index).SetRating(newRating); // set new rating for driver
}
else if (option == 3) { // edit if handicapped
int h;
cout << "0 - no" << endl;
cout << "1 - yes" << endl;
cout << "Is this passenger handicapped? 0 or 1: ";
cin >> h;
while (cin.fail()) {
cin.clear();
cin.ignore(100, '\n');
cout << "Invalid data type..." << endl;
cout << "Is this driver's vehicle handicapped capable? 0 or 1: ";
cin >> h;
}
passengers.at(index).SetHandicapped(h);
}
else if (option == 4) { // edit pets
int p;
cout << "0 - no" << endl;
cout << "1 - yes" << endl;
cout << "Does this passenger have pets? 0 or 1: ";
cin >> p;
while (cin.fail()) {
cin.clear();
cin.ignore(100, '\n');
cout << "Invalid data type..." << endl;
cout << "Does this passenger have pets? 0 or 1: ";
cin >> p;
}
passengers.at(index).SetPets(p);
}
else if (option == 5) { // edit rating
float newRating;
/* prompt for new rating */
cout << "Enter the new default rating for this passenger: ";
cin >> newRating;
while (cin.fail() || newRating < 0 || newRating > 5) {
while (newRating < 0 || newRating > 5) {
cout << "Invalid..." << endl;
cout << "Rating must be between 0 and 5: ";
cin >> newRating;
}
while (cin.fail()) {
cin.clear();
cin.ignore(100, '\n');
cout << "Invalid data type..." << endl;
cout << "Enter the new default rating for this passenger: ";
cin >> newRating;
}
}
passengers.at(index).SetRating(newRating); // set new rating
}
}
PassengerInfo Passengers::GetPassenger(int i) {
return passengers.at(i);
}
void Passengers::AddPassenger(PassengerInfo p) {
passengers.push_back(p);
}