-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBusCompany.cpp
184 lines (118 loc) · 3.31 KB
/
BusCompany.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
#include "BusCompany.h"
#include <fstream>
#include <string>
#include <iostream>
#include <cstdlib>
#include <vector>
using namespace std;
string END = "End Route";
// default constructor (should not be called)
BusCompany::BusCompany(){
// initialize route
m_newRoute = new Route();
}
// constructor with file
BusCompany::BusCompany(string fileName){
// initialize route, get stops and display menu
m_newRoute = new Route();
ReadFile(fileName);
MainMenu();
}
// destructor
BusCompany::~BusCompany(){
// clear all the routes
for(unsigned int i = 0; i < m_routes.size(); i++){
m_routes.at(i)->Clear();
}
}
// read stops from file
void BusCompany::ReadFile(string fileName){
// open file for reading
fstream file(fileName);
//initialize necessary variables
string name, newLine;
int location, riders, cost;
//loop through lines in file
while(getline(file, name, ',')){
// when still in a route
if(name != "End Route"){
// read in rest of the variables and remove newline character
file >> location >> riders >> cost;
getline(file, newLine);
m_newRoute->InsertAt(name, location, riders, cost);
} else {
//remove newline character
getline(file, newLine);
// end of route, add route to list and reinitialize
m_routes.push_back(m_newRoute);
cout << "Route " << m_routes.size() << " loaded with " << m_newRoute->GetSize() << " stops" << endl;
m_newRoute = new Route();
}
}
// close file
file.close();
}
// display menu
void BusCompany::MainMenu(){
int choice = 0;
cout << "Welcome to the UMBC Transit Simulator!" << endl << endl;
// continue until user quits
while(choice != 4){
// display choices and get input
cout << "What would you like to do?" << endl;
cout << "1. Display Routes" << endl;
cout << "2. Display Earnings vs Expenses By Route" << endl;
cout << "3. Optimize Route" << endl;
cout << "4. Exit" << endl;
cin >> choice;
cout << endl;
// do action based on input
switch(choice){
case 1:
DisplayRoutes();
break;
case 2:
DisplayRouteData();
break;
case 3:
OptimizeRoute();
break;
}
}
}
// display all stops in all routes
void BusCompany::DisplayRoutes(){
// display each route in m_routes
for(unsigned int i = 0; i < m_routes.size(); i++){
cout << "****ROUTE " << i + 1 << "****" << endl;
cout << *m_routes.at(i);
}
}
// remove routes that don't generate income
void BusCompany::OptimizeRoute(){
// if route is empty
if(m_routes.size() == 0){
cout << "The route is empty" << endl << endl;
// when there is only one route
} else if(m_routes.size() == 1){
m_routes.at(0)->OptimizeRoute(RIDER_FARE);
cout << "Route optimized" << endl << endl;
} else{
// get desired route from user and optimize it
unsigned int toOptimize = 0;
while(toOptimize < 1 || toOptimize > m_routes.size()){
cout << "Which route to you want to optimize (1 - " << m_routes.size() << ")? ";
cin >> toOptimize;
}
m_routes.at(toOptimize - 1)->OptimizeRoute(RIDER_FARE);
cout << "Route optimized" << endl << endl;
}
}
// display route earnings, expenses, total
void BusCompany::DisplayRouteData(){
// display data for each route in m_routes
for(unsigned int i = 0; i < m_routes.size(); i++){
cout << "****ROUTE " << i + 1 << "****" << endl;
m_routes.at(i)->DisplayStopData(RIDER_FARE);
}
}