-
Notifications
You must be signed in to change notification settings - Fork 0
/
ParticipatorRegister.cpp
318 lines (272 loc) · 8.04 KB
/
ParticipatorRegister.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
//
// ParticipatorRegister.cpp
// newYearsCamp
//
// Created by Anders Akesson on 11/16/12.
// Copyright (c) 2012 Anders Akesson. All rights reserved.
//
#include "ParticipatorRegister.h"
#include <string>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <fstream>
// constructor
ParticipatorRegister::ParticipatorRegister()
{
nrOfParticipents = 0;
arraySize = 1;
participatorPtrPtr = new Participator*[arraySize];
for(int i = 0; i < arraySize; i++)
{
participatorPtrPtr[i] = new Participator();
}
}
// copy constructor
ParticipatorRegister::ParticipatorRegister(const ParticipatorRegister ®Ref)
{
nrOfParticipents = regRef.nrOfParticipents;
Participator** participatorPtrPtr;
participatorPtrPtr = new Participator*[nrOfParticipents];
for(int i = 0; i < nrOfParticipents; i++)
{
*participatorPtrPtr[i] = *regRef.participatorPtrPtr[i];
}
}
// misc functions
int ParticipatorRegister::getNrOfParticipents() const
{
return this -> nrOfParticipents;
}
void ParticipatorRegister::addParticipator(string nName, string aAddress, bool pPaid)
{
if(nrOfParticipents >= arraySize)
{
resize();
}
participatorPtrPtr[nrOfParticipents] = new Participator(nName, aAddress, pPaid);
nrOfParticipents++;
}
void ParticipatorRegister::resize()
{
Participator** temp;
arraySize = arraySize * 2;
temp = new Participator*[arraySize];
for(int i = 0; i < nrOfParticipents; i++)
{
temp[i] = participatorPtrPtr[i];
}
delete [] participatorPtrPtr;
participatorPtrPtr = temp;
}
void ParticipatorRegister::displayAllByName() const
{
for(int i = 0; i < nrOfParticipents; i++)
{
cout << participatorPtrPtr[i]->getName() << endl;
}
}
void ParticipatorRegister::displayAllPaid() const
{
cout << "All participants that have paid: " << endl;
for(int i = 0; i < nrOfParticipents; i++)
{
if(participatorPtrPtr[i]->getPaid())
{
cout << participatorPtrPtr[i]->toString();
}
}
}
void ParticipatorRegister::displayAllNotPaid() const
{
cout << "All participants that have not paid: " << endl;
for(int i = 0; i < nrOfParticipents; i++)
{
if(participatorPtrPtr[i]->getPaid() == false)
{
cout << participatorPtrPtr[i]->toString();
}
}
}
int ParticipatorRegister::findParticipator(string nName)
{
int find = -1;
for(int i = 0; i < nrOfParticipents; i++)
{
if(nName == participatorPtrPtr[i]->getName())
{
find = i;
}
}
return find;
}
void ParticipatorRegister::searchAndDisplay(string searchName)
{
int pos = 0;
pos = findParticipator(searchName);
if(pos != -1)
{
cout << participatorPtrPtr[pos]->toString();
}
else
{
cout << "Person not found. " << endl;
}
}
bool ParticipatorRegister::removeParticipator(string name)
{
bool success = false;
int pos;
pos = findParticipator(name);
if(pos != -1)
{
cout << "Removing " << participatorPtrPtr[pos]->getName() << "..." << endl;
participatorPtrPtr[pos] = participatorPtrPtr[--nrOfParticipents];
cout << "Done. " << endl;
success = true;
}
else
cout << "No participator with that name found. " << endl;
return success;
}
bool ParticipatorRegister::changeParticipatorInfo(string name)
{
bool success = false;
int pos;
pos = findParticipator(name);
if(pos != -1)
{
string nName;
cout << "Enter the new name of the participator: " << endl;
getline(cin, nName);
(*participatorPtrPtr[pos]).setName(nName);
string aAddress;
cout << "Enter the new address of the participator: " << endl;
getline(cin, aAddress);
(*participatorPtrPtr[pos]).setAddress(aAddress);
int pPaid;
cout << "Has the participator paid the fee? 1 for yes, 0 for no: " << endl;
cin >> pPaid;
cin.ignore();
while(pPaid < 0 || pPaid > 1 || !(cin.good()))
{
cin.clear(); // clear error condition
cin.ignore(100,'\n'); // remove characters))
cout << "Not a valid input, enter 1 or 0: " << endl;
cin >> pPaid;
cin.ignore();
}
(*participatorPtrPtr[pos]).setPaid(pPaid);
success = true;
}
else
cout << "No participator with that name found. " << endl;
return success;
}
void ParticipatorRegister::save(string filename)
{
ofstream out;
out.open(filename);
if(out.is_open())
{
cout << "Saving to file " << filename << "..." << endl;
for(int i = 0; i < nrOfParticipents; i++)
{
out << participatorPtrPtr[i]->toString();
}
out.close();
cout << "Done. " << endl;
}
else
cout << "Could not open " << filename << ", returning to main menu. " << endl;
}
void ParticipatorRegister::load(string filename)
{
ifstream in;
in.open(filename);
if(in.is_open())
{
cout << "Loading from file " << filename << "..." << endl;
for(int i = 0; i < nrOfParticipents; i++)
{
string name;
getline(in, name);
string address;
getline(in, address);
bool paid;
in >> paid;
participatorPtrPtr[nrOfParticipents] = new Participator(name, address, paid);
}
in.close();
cout << "Done. " << endl;
}
else
cout << "Could not open " << filename << ", returning to main menu. " << endl;
}
bool ParticipatorRegister::sort()
{
bool status = false;
Participator temp;
cout << "The names unsorted: " << endl;
for (int i = 0; i < nrOfParticipents; i++)
{
cout << participatorPtrPtr[i]->getName() << endl;
}
cout << "Sorting..." << endl;
char *a;
char *b;
for (int mi,i = 0; i < nrOfParticipents - 1; i++)
{
temp = *participatorPtrPtr[i];
a= new char[participatorPtrPtr[i]->getName().size()];
memcpy(a,participatorPtrPtr[i]->getName().c_str(),participatorPtrPtr[i]->getName().size());
mi = i;
for (int j=i+1; j < nrOfParticipents; j++)
{
b= new char[participatorPtrPtr[j]->getName().size()];
memcpy(b,participatorPtrPtr[j]->getName().c_str(),participatorPtrPtr[j]->getName().size());
if(strcmp(a ,b) > 0)
{
temp = (*participatorPtrPtr[j]);
status = true;
mi = j;
}
}
*participatorPtrPtr[mi] = *participatorPtrPtr[i];
*participatorPtrPtr[i] = temp;
}
delete a;
a = NULL;
delete b;
b = NULL;
cout << "The names sorted: " << endl;
for (int i = 0; i < nrOfParticipents; i++)
{
cout << participatorPtrPtr[i]->getName() << endl;
}
return status;
}
// overloaded operators
bool ParticipatorRegister::operator==(const ParticipatorRegister ®) const
{
return (reg.participatorPtrPtr == this -> participatorPtrPtr && reg.participatorPtrPtr == this -> participatorPtrPtr && reg.participatorPtrPtr == this -> participatorPtrPtr);
}
void ParticipatorRegister::operator= (const ParticipatorRegister ®)
{
if(nrOfParticipents > 0) delete [] participatorPtrPtr;
arraySize = reg.arraySize;
participatorPtrPtr = new Participator*[arraySize];
for(int i = 0; i < nrOfParticipents; i++)
participatorPtrPtr[i] = reg.participatorPtrPtr[i];
}
// destructor
ParticipatorRegister::~ParticipatorRegister()
{
for(int i = 0; i < nrOfParticipents; i++)
{
delete participatorPtrPtr[i];
participatorPtrPtr[i] = NULL;
}
delete participatorPtrPtr;
participatorPtrPtr = NULL;
}