-
Notifications
You must be signed in to change notification settings - Fork 0
/
Route.cpp
288 lines (174 loc) · 4.88 KB
/
Route.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
#include "Route.h"
#include <string>
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
// default constructor
Route::Route(){
// initialize variables
m_start = NULL;
m_size = 0;
}
// destructor
Route::~Route(){
Clear();
}
// inserts stop in route
void Route::InsertAt(string name, int location, int riders, double cost){
// create stop to add
Stop* newStop = new Stop(name, location, riders, cost);
// check is list is empty
if(IsEmpty()){
m_start = newStop;
} else{
// get last stop in route
Stop* m_end = new Stop();
for(Stop* current = m_start; current != NULL; current = current->m_next){
m_end = current;
}
// add to beginning
if(location < m_start->m_location){
newStop->m_next = m_start;
m_start = newStop;
// add to end
} else if(location > m_end->m_location){
m_end->m_next = newStop;
// add in middle
} else{
// find location to add the new stop
Stop* previous = new Stop();
Stop* next = new Stop();
for(Stop* current = m_start; !(location > previous->m_location && location < current->m_location); current = current->m_next){
previous = current;
next = current->m_next;
}
// place new stop in between two others
previous->m_next = newStop;
newStop->m_next = next;
}
}
// increase size
m_size++;
}
// display stops in route
void Route::DisplayRoute(){
if(IsEmpty()){
cout << "Route is empty";
} else{
// print each stop and its location
int count = 1;
for(Stop* current = m_start; current != NULL; current = current->m_next){
cout << "Stop " << count << " - " << current->m_name << " (" << current->m_location << ")" << endl;
count++;
}
}
}
// check if route is empty
bool Route::IsEmpty(){
return m_size == 0;
}
// calculate earnings for route
double Route::GetRouteEarnings(double riderFare){
double earnings = 0;
// add earnings from each stop to total and return the total
for(Stop* current = m_start; current != NULL; current = current->m_next){
earnings += (current->m_riders * riderFare);
}
return earnings;
}
//calculate expenses for route
double Route::GetRouteExpenses(){
double expenses = 0;
// add expenses from each stop to total and return the total
for(Stop* current = m_start; current != NULL; current = current->m_next){
expenses += current->m_cost;
}
return expenses;
}
// clear route of all stops
void Route::Clear(){
// start from beginning and delete previous stops
Stop* current = m_start;
Stop* previous = m_start;
while(current != NULL){
current = current->m_next;
delete previous;
previous = current;
m_size--;
}
// set all to NULL
current = previous = NULL;
m_start = NULL;
}
// delete stops that lose money
void Route::OptimizeRoute(double riderFare){
// calculate net profit/loss for each stop and remove if there is a loss
for(Stop* current = m_start; current != NULL; current = current->m_next){
double earning = current->m_riders * riderFare;
double expense = current->m_cost;
double total = earning - expense;
if(total < 0){
RemoveStop(current->m_location);
}
}
}
// remove stop from route
void Route::RemoveStop(int stopLocation){
// get last stop in route
Stop* m_end = new Stop();
for(Stop* current = m_start; current != NULL; current = current->m_next){
m_end = current;
}
// remove first stop
if(stopLocation == m_start->m_location){
m_start = m_start->m_next;
// remove last stop
} else if(stopLocation == m_end->m_location){
Stop* current = m_start;
Stop* previous = new Stop();
while(current->m_next != NULL){
previous = current;
current = current->m_next;
}
m_end = previous;
previous->m_next = NULL;
// remove in the middle
} else{
Stop* current = m_start;
Stop* previous = new Stop();
for(Stop* temp = m_start; temp->m_location != stopLocation; temp = temp->m_next){
previous = current;
current = current->m_next;
}
previous->m_next = current->m_next;
}
m_size--;
}
// display stop earnings, expenses, and total loss/gain
void Route::DisplayStopData(double riderFare){
cout << "Earnings: " << GetRouteEarnings(riderFare) << endl;
cout << "Expenses: " << GetRouteExpenses() << endl;
// calculate total for each stop
for(Stop* current = m_start; current != NULL; current = current->m_next){
double earning = current->m_riders * riderFare;
double expense = current->m_cost;
double total = earning - expense;
cout << current->m_name << endl;
cout << " Earnings: " << current->m_riders << "@" << current->m_cost << " = $" << earning << endl;
cout << " Expenses: $" << expense << endl;
cout << " Total: $" << total << endl;
}
cout << endl;
}
// get size of route
int Route::GetSize(){
return m_size;
}
// extraction for displaying a route
ostream &operator<<(ostream &output, Route &myRoute){
// call display route
myRoute.DisplayRoute();
output << "END" << endl << endl;
return output;
}