-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVehicle.cpp
352 lines (216 loc) · 5.58 KB
/
Vehicle.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
/* ------------------------------------------------------
Name: Morris Anthony
Student ID:158091199
Date:08/05/2020
I have done all the coding by myself and only copied the code that my professor provided to complete my workshops and assignments.
-----------------------------------------------------------*/
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <stdio.h>
#include <cctype>
#include <sstream>
#include <iostream>
#include "Vehicle.h"
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
namespace sdds {
Vehicle::Vehicle() {
plate[0] = '\0';
mnm = nullptr;
pspot = 0;
}
Vehicle::Vehicle(const char* lplate, const char* m) {
plate[0] = '\0';
mnm = nullptr;
pspot = 0;
if (lplate != nullptr && strlen(lplate) <= 8 && strlen(lplate) >= 1 && lplate[0] != '\0' && m != nullptr && m[0] != '\0' && strlen(m) >= 2) {
strcpy(plate, lplate);
mnm = nullptr;
mnm = new char[strlen(m) + 1];
strcpy(mnm, m);
pspot = 0;
}
else {
setEmpty();
}
}
Vehicle::~Vehicle() {
delete[] mnm;
mnm = nullptr;
pspot = 0;
}
int Vehicle::getParkingSpot() {
return pspot;
}
void Vehicle::setParkingSpot(int s) {
if (s > 0) {
pspot = s;
}
else {
plate[0] = '\0';
delete[] mnm;
mnm = nullptr;
pspot = 0;
}
}
bool Vehicle::operator==(const char* lplate)const { //copy plate if recieved plate is valid
int result = 0;
if (lplate[0] != '\0') {
string tplate = plate;
string nplate = lplate;
int tlen = strlen(tplate.c_str());
int nlen = strlen(nplate.c_str());
for (int i = 0; i < nlen; i++)
{
nplate[i] = toupper(nplate[i]);
}
if (tlen == nlen) {
result = strcmp(tplate.c_str(), nplate.c_str());
}
else result = 1;
}
if (result == 1 || result == -1) {
return false;
}
else return true;
}
bool Vehicle:: operator==(const Vehicle& src)const { //copy vehicle if recieved vehicle is valid
int result = 0;
if (src.plate[0] != '\0' && src.mnm != nullptr && src.pspot != 0) {
string nplate = plate;
string tplate = src.plate;
int nlen = nplate.size(); //stores size of plate from "this" car to a signed int
int tlen = tplate.size(); //stores size of plate recieved to a signed int
for (int i = 0; i < nlen; i++) //set to lower case for comparison
{
nplate[i] = tolower(nplate[i]);
}
for (int i = 0; i < tlen; i++)
{
tplate[i] = tolower(tplate[i]);
}
result = strcmp(nplate.c_str(), tplate.c_str()); //comparison
if (result == 1) {
return false;
}
else return true;
}
else {
return false;
}
}
std::istream& Vehicle::read(std::istream& in) {
if (ReadWritable::isCsv()) { //if comma seperated is yes
string spt;
string plt;
string mknmd;
//take user input in strings if comma seprarated
getline(in, spt, ',');
getline(in, plt, ',');
getline(in, mknmd, ',');
if (spt[0] != '\0') {
pspot = stoi(spt);
}
if (strlen(plt.c_str()) > 8) {
plt.substr(0, 8);
}
//std::for_each(plt.begin(), plt.end(), [](char& d) {
// d = ::toupper(d);
// });
int plen = plt.length();
for (int i = 0; i < plen; i++) {
plt[i] = toupper(plt[i]);
}
if (strlen(mknmd.c_str()) > 60) { //take only 60 chars from the string
mknmd.substr(0, 60);
}
//copy plate converted to uppercase
strcpy(plate, plt.c_str());
delete[] mnm;
mnm = nullptr;
mnm = new char[strlen(mknmd.c_str()) + 1];
strcpy(mnm, mknmd.c_str());
}
else { //if comma seperated is no
cout << "Enter Licence Plate Number: ";
cin.ignore(2000, '\n');
string ntemp;
getline(in, ntemp, '\n'); //get user input for plate
while (strlen(ntemp.c_str()) > 8 || strlen(ntemp.c_str()) < 1) {
cout << "Invalid Licence Plate, try again: ";
getline(in, ntemp, '\n');
}
int len = ntemp.length(); //store len of plate entered by user
for (int i = 0; i < len; i++) {
ntemp[i] = toupper(ntemp[i]);
}
strcpy(plate, ntemp.c_str());
cout << "Enter Make and Model: ";
string temp;
getline(in, temp, '\n'); //get user input for make n model
while (strlen(temp.c_str()) < 2) {
cout << "Invalid Make and model, try again: ";
getline(in, temp, '\n');
}
delete[] mnm;
mnm = nullptr;
mnm = new char[strlen(temp.c_str()) + 1];
strcpy(mnm, temp.c_str());
return in;
}
return in;
}
std::ostream& Vehicle::write(std::ostream& os) const { //write vehicle object to console if vehicle not empty
if (isEmpty()) {
cout << "Invalid Vehicle Object" << endl;
}
else if (isCsv()) {
os << pspot << "," << plate << "," << mnm << ",";
}
else {
cout << "Parking Spot Number: ";
if (pspot == 0) {
os << "N/A" << endl;
}
else {
os << pspot << endl;
}
os << "Licence Plate: " << plate << endl;
os << "Make and Model: " << mnm << endl;
}
return os;
}
void Vehicle::setEmpty() { //set vehicle to empty
plate[0] = '\0';
mnm = nullptr;
pspot = 0;
}
bool Vehicle::isEmpty()const { //check if vehicle is empty
if (mnm != nullptr && plate[0] != '\0') {
return false;
}
else {
return true;
}
}
char* Vehicle::gotLicencePlate() { //return plate of vehicle
return plate;
}
char* Vehicle::getMakeModel() { //return make an model of vehicle
return mnm;
}
void Vehicle::setMakeModel(const char* nn) { //set make an model of vehicle
if (nn != nullptr) {
delete[] mnm;
mnm = nullptr;
mnm = new char[strlen(nn) + 1];
strcpy(mnm, nn);
}
else {
mnm = nullptr;
}
}
}