-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWaitList.cpp
More file actions
268 lines (225 loc) · 6.31 KB
/
WaitList.cpp
File metadata and controls
268 lines (225 loc) · 6.31 KB
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
/*
* File: WaitList.cpp
*
* This class contains the implementation of all the methods of the WaitList class
*
* Designed by: Team 4
*
* List of methods:
* constructor and destructor
* save: save the waitlist into the file
* load: load the waitlist from the file
* add: add new ticket to the end of the waitlist
* remove: remove the first ticket from the waitlist
* remove: remove the ith ticket from the waitlist
* getSize: return the size of the waitlist
* isEmpty: check if the wait list is empty
* getFirst: return the first ticket of the waitlist
* get: return the ith ticket of the waitlist
*
*/
#include "WaitList.h"
#include "LinkedList.cpp"
#include <iostream>
#include <fstream> // work with files
using namespace std;
/****************************************************
* CONSTRUCTOR
*****************************************************/
WaitList::WaitList(string flight)
{
waitlist = new LinkedList<Ticket>();
flightFileName = flight;
load();
}
/****************************************************
* DESTRUCTOR
*****************************************************/
WaitList::~WaitList()
{
save();
delete waitlist;
}
/****************************************************
* function: save
* parameter:
* return: void
* save the current waitlist back into the file
* and override the old file
*****************************************************/
void WaitList::save()
{
ofstream outputFile(flightFileName + "_waitlist.txt");
// loop each ticket in the waitlist to write it to the file
for (int i = 0; i < waitlist->getSize(); i++)
{
//Ticket* ticket = waitlist->get(i);
outputFile << waitlist->get(i)->toString() << "---------- ----------\n";
//delete ticket;
}
outputFile.close();
}
/****************************************************
* function: load
* parameter:
* return: void
* read data from the file to construct the waitlist
*****************************************************/
void WaitList::load()
{
// try to open the waitlist.txt file as input mode
ifstream inputFile(flightFileName + "_waitlist.txt");
// cannot open the file
if (inputFile.fail())
cout << "Cannot open the file.\n";
// open successfully
else
{
string category = "", data = "", name = "", flightCode = "", confirmationCode = "";
int age = 0, seatNumber = 0, ticketNumber = 0, size = 0, seatCount = 0;
double price = 0;
//bool readStuff = false;
Seat* seats = nullptr;
// start reading from the file
inputFile >> category;
inputFile >> data;
// keep reading till the end of the file
while (!inputFile.eof())
{
//readStuff = true;
if (category == "Ticket:")
{
ticketNumber = stoi(data);
seatCount = 0;
}
else if (category == "Size:")
{
size = stoi(data);
// allocate space for the seat array;
seats = new Seat[size];
}
else if (category == "Flight:")
{
flightCode = data;
}
else if (category == "Name:")
{
name = data;
/*
* keep reading data for name because it may contain white spaces
* stop whenever the data is "Age:" (new category)
*/
inputFile >> data;
while (data != "Age:")
{
name += " " + data;
inputFile >> data;
}
/*
* done reading the name
* set the category to be the current data,
* then go to the READ_DATA label to read in the new data
*/
category = data;
goto READ_DATA;
}
else if (category == "Age:")
{
age = stoi(data);
}
else if (category == "Seat:")
{
seatNumber = stoi(data);
}
else if (category == "Price:")
{
price = stod(data);
// fill the seat in the array
seats[seatCount].setReserver(new Person(name, age));
seats[seatCount].setSeatNumber(seatNumber, price);
seatCount++;
// done getting all seats, add the ticket to the waitlist
if (seatCount == size)
{
Ticket* ticket = new Ticket(seats, size, flightCode, ticketNumber);
add(ticket);
}
}
inputFile >> category;
READ_DATA: // label to jump to
inputFile >> data;
} // end while (not eof)
// close the file after finish reading
inputFile.close();
} // end else (open successfully)
}
/****************************************************
* function: getSize
* parameter:
* return: int
* return the length of the waitlist
*****************************************************/
int WaitList::getSize()
{
return waitlist->getSize();
}
/****************************************************
* function: add
* parameter: ticket: Ticket pointer
* return: void
* add new ticket to the end of the waitlist
*****************************************************/
void WaitList::add(Ticket *ticket)
{
waitlist->addLast(ticket);
}
/****************************************************
* function: remove
* parameter:
* return: Ticket pointer
* remove and return the first ticket in the waitlist
*****************************************************/
Ticket* WaitList::remove()
{
return waitlist->removeFirst();
}
/****************************************************************
* function: remove (overloaded)
* parameter: index: int
* return: Ticket pointer
* remove and return the ticket at a given index in the waitlist
*****************************************************************/
Ticket* WaitList::remove(int index)
{
return waitlist->remove(index);
}
/****************************************************
* function: get
* parameter:
* return: Ticket pointer
* return, but not remove, the ticket at a given index
*****************************************************/
Ticket* WaitList::get(int index)
{
return waitlist->get(index);
}
/******************************************************
* function: getFirst
* parameter:
* return: Ticket pointer
* return, but not remove, the first ticket in the list
*******************************************************/
Ticket* WaitList::getFirst()
{
return waitlist->getFirst();
}
/******************************************************
* function: isEmpty
* parameter:
* return: bool
* check if the wait list is empty
*******************************************************/
bool WaitList::isEmpty()
{
return waitlist->isEmpty();
}