-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstore.cpp
276 lines (243 loc) · 6.21 KB
/
store.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
/*
* store.cpp is part of the Movie Store Simulator, a C++ program that
* offers the function of borrow, return, or stock with up to 10,000
* customers and 26 genres of movies
*
* @author Bill Zhao, Lucas Bradley
* @date March 12th
*/
#include "store.h"
#include <fstream>
// Default constructor
Store::Store() = default;
// Takes three strings which are file names and fills in all fo the
// data according to the contents of the files
Store::Store(const std::string &Movies, const std::string &Customers,
const std::string &Commands) {
std::ifstream InputStream;
InputStream.open(Movies.c_str());
if (!InputStream.is_open()) {
return;
}
std::string Code;
char Char;
InputStream.get(Char);
while (!InputStream.eof()) {
if (Char != '\0' && Char != '\n') {
Code += Char;
} else {
insertFilm(Code);
Code.erase();
}
InputStream.get(Char);
}
insertFilm(Code);
Code.erase();
InputStream.close();
InputStream.open(Customers.c_str());
if (!InputStream.is_open()) {
return;
}
InputStream.get(Char);
while (!InputStream.eof()) {
if (Char != '\0' && Char != '\n') {
Code += Char;
} else {
insertCustomer(Code);
Code.erase();
}
InputStream.get(Char);
}
insertCustomer(Code);
Code.erase();
InputStream.close();
InputStream.open(Commands.c_str());
if (!InputStream.is_open()) {
return;
}
InputStream.get(Char);
while (!InputStream.eof()) {
if (Char != '\0' && Char != '\n') {
Code += Char;
} else {
action(Code);
Code.erase();
}
InputStream.get(Char);
}
action(Code);
Code.erase();
InputStream.close();
}
// Store destructor
Store::~Store() {
for (auto &Customer : Customers) {
delete Customer.second;
}
}
// Inserts film in the correct postion of inventory
bool Store::insertFilm(std::string Code) {
Movie *Film = F.buildMovie(std::move(Code));
if (Film == nullptr) {
return false;
}
return Inventory.insert(Film);
}
// Checks if film exists in the inventory
bool Store::findFilm(Movie *Film) { return Inventory.find(Film); }
// Inserts customer int he correct positon of Customers
bool Store::insertCustomer(std::string Code) {
auto *Cust = new Customer(Code);
int CustomerCode = 0;
int It = 0;
while (Code[It] != ' ') {
CustomerCode *= 10;
CustomerCode += Code[It] - '0';
It++;
}
if (Customers.find(CustomerCode) != Customers.end()) {
delete Cust;
return false;
}
Customers[CustomerCode] = Cust;
return true;
}
// Displays the contents of the inventory in sorted order
void Store::inventoryDisplay() {
std::cout << "Inventory:\n\n";
// F,D,C
std::map<std::string, Movie *> *GenreMapF = Inventory.genreMap('F');
std::cout << "Comedy: " << std::endl;
std::cout << "Genre, Stock, Director, Title, Year of Release, Borrowed\n";
auto FItF = GenreMapF->begin();
while (FItF != GenreMapF->end()) {
std::cout << FItF->second->inventoryDisplayHelper() << std::endl;
FItF++;
}
std::cout << std::endl;
std::map<std::string, Movie *> *GenreMapD = Inventory.genreMap('D');
std::cout << "Drama: " << std::endl;
std::cout << "Genre, Stock, Director, Title, Year of Release, Borrowed\n";
auto FItD = GenreMapD->begin();
while (FItD != GenreMapD->end()) {
std::cout << FItD->second->inventoryDisplayHelper() << std::endl;
FItD++;
}
std::cout << std::endl;
std::map<std::string, Movie *> *GenreMapC = Inventory.genreMap('C');
std::cout << "Classic: " << std::endl;
std::cout
<< "Genre, Stock, Director, Title, Major Actor Release Date, Borrowed\n";
auto FItC = GenreMapC->begin();
while (FItC != GenreMapC->end()) {
std::cout << FItC->second->inventoryDisplayHelper() << std::endl;
FItC++;
}
std::cout << std::endl;
}
// Displays specific customers history
void Store::historyDisplay(int CustomerCode) {
if (Customers.find(CustomerCode) == Customers.end()) {
return;
}
Customers[CustomerCode]->printHistory();
}
// Borrow film will take command and edit Customers and
// inventory accordingly
bool Store::borrowFilm(std::string Code) {
int It = 2;
// int CustomerCode = 0;
while (Code[It] != ' ') {
It++;
}
It += 3;
char MovieType = Code[It];
It += 2;
std::string SortingElement;
while (Code[It] != '\0') {
SortingElement += Code[It];
It++;
}
std::map<std::string, Movie *> *GenreMap = Inventory.genreMap(MovieType);
if (GenreMap == nullptr) {
return false;
}
if (GenreMap->find(SortingElement) == GenreMap->end()) {
return false;
}
if (!GenreMap->at(SortingElement)->borrowMovie()) {
return false;
}
addTransaction(Code);
return true;
}
// Return film will take command and edit Customers and
// inventory accordingly
bool Store::returnFilm(std::string Code) {
int It = 2;
// int CustomerCode = 0;
while (Code[It] != ' ') {
It++;
}
It += 3;
char MovieType = Code[It];
It += 2;
std::string SortingElement;
while (Code[It] != '\0') {
SortingElement += Code[It];
It++;
}
std::map<std::string, Movie *> *GenreMap = Inventory.genreMap(MovieType);
if (GenreMap == nullptr) {
return false;
}
if (GenreMap->find(SortingElement) == GenreMap->end()) {
return false;
}
if (!GenreMap->at(SortingElement)->returnMovie()) {
return false;
}
addTransaction(Code);
return true;
}
// Takes a code and adds the information to the
// customers history
bool Store::addTransaction(std::string Code) {
int CustomerCode = 0;
int It = 2;
while (Code[It] != ' ') {
CustomerCode *= 10;
CustomerCode += Code[It] - '0';
It++;
}
if (Customers.find(CustomerCode) == Customers.end()) {
return false;
}
Customers[CustomerCode]->addTransaction(Code);
return true;
}
// Determines which action to do depending on code
bool Store::action(std::string Code) {
if (Code[0] == 'B') {
return borrowFilm(Code);
}
if (Code[0] == 'R') {
return returnFilm(Code);
}
if (Code[0] == 'H') {
int It = 2;
int CustomerCode = 0;
while (Code[It] != '\0') {
CustomerCode *= 10;
CustomerCode += Code[It] - '0';
It++;
}
historyDisplay(CustomerCode);
return true;
}
if (Code[0] == 'I') {
inventoryDisplay();
return true;
}
return false;
}