-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAidApp.cpp
More file actions
277 lines (227 loc) · 7.68 KB
/
AidApp.cpp
File metadata and controls
277 lines (227 loc) · 7.68 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
269
270
271
272
273
274
275
276
277
/*
AidApp.cpp
Name: Yuansheng Lu
Student ID: 136654167
E-mail: ylu140@myseneca.ca
*/
#define _CRT_SECURE_NO_WARNINGS
#include <cstring>
#include <iomanip>
#include "AidApp.h"
using namespace std;
namespace sict {
// constructor
//
AidApp::AidApp() {
filename_[0] = '\0';
for (int i = 0; i < MAX_NO_RECS; i++)
product_[i] = nullptr;
noOfProducts_ = 0;
loadRecs();
}
AidApp::AidApp(const char* filename) {
strcpy(filename_, filename);
for (int i = 0; i < MAX_NO_RECS; i++)
product_[i] = nullptr;
noOfProducts_ = 0;
loadRecs();
}
// private function
//
void AidApp::pause()const {
cout << "Press Enter to continue..." << endl;
while (cin.get() != '\n') {} // wait for user pressing enter
}
int AidApp::menu() {
int choice;
int rv;
cout << "Disaster Aid Supply Management Program" << endl;
cout << "1- List products" << endl;
cout << "2- Display product" << endl;
cout << "3- Add non-perishable product" << endl;
cout << "4- Add perishable product" << endl;
cout << "5- Add to quantity of purchased products" << endl;
cout << "0- Exit program" << endl;
cout << "> ";
cin >> choice;
if (choice >= 0 && choice <= 5)
rv = choice;
else
rv = -1;
cout << endl;
cin.ignore(2000, '\n'); // clear input buffer
return rv;
}
void AidApp::loadRecs() {
int readIndex = 0;
char id;
datafile_.open(filename_, ios::in); // open a file to read
if (datafile_.fail()) { // if the file does not exist
datafile_.clear();
datafile_.close();
datafile_.open(filename_, ios::out); // create a new file
datafile_.close();
} else { // if the file exists
do {
//delete [] product_[readIndex]; // deallocate memory
datafile_ >> id;
if (!datafile_.fail()) {
if (id == 'P') {
product_[readIndex] = new AmaPerishable;
}
else if (id == 'N') {
product_[readIndex] = new AmaProduct;
}
if (id == 'P' || id == 'N') {
datafile_.ignore(); // skip the comma
product_[readIndex]->load(datafile_);
readIndex++;
}
}
} while (!datafile_.fail());
noOfProducts_ = readIndex;
datafile_.close();
}
}
void AidApp::saveRecs() {
datafile_.open(filename_, ios::out); // open a file to write
for (int i = 0; i < noOfProducts_; i++) {
product_[i]->store(datafile_);
}
datafile_.close();
}
void AidApp::listProducts()const {
double total = 0;
cout << " Row | SKU | Product Name | Cost | QTY| Unit |Need| Expiry " << endl;
cout << "-----|--------|--------------------|-------|----|----------|----|----------" << endl;
for (int i = 0; i < noOfProducts_; i++) {
cout << setw(4) << right << i + 1 << " | ";
cout.unsetf(ios::right);
//product_[i]->write(cout, true);
cout << *product_[i] << endl;
total += *product_[i];
if ((i + 1) % 10 == 0)
pause();
}
cout << "---------------------------------------------------------------------------" << endl;
cout << "Total cost of support: $" << fixed << setprecision(2) << total << endl << endl;
}
int AidApp::SearchProducts(const char* sku)const {
int index = -1;
for (int i = 0; i < noOfProducts_ && index == -1; i++) {
if (*product_[i] == sku)
index = i;
}
return index;
}
void AidApp::addQty(const char* sku) { // update quantity
int index = SearchProducts(sku);
int amount, amountLeft;
if (index == -1){
cout << "Not found!" << endl;
} else {
product_[index]->write(cout, false);
cout << endl << endl;
cout << "Please enter the number of purchased items: ";
cin >> amount;
cout << endl;
if (cin.fail()) {
cout << "Invalid quantity value!" << endl;
cin.clear();
} else {
amountLeft = product_[index]->qtyNeeded() - product_[index]->quantity();
if (amount <= amountLeft) {
*product_[index] += amount;
} else {
cout << "Too many items; only ";
cout << amountLeft;
cout << " is needed, please return the extra ";
cout << amount - amountLeft;
cout << " items." << endl << endl;
amount = amountLeft;
*product_[index] += amount;
}
saveRecs();
cout << "Updated!" << endl << endl;
cin.ignore(2000, '\n'); // clear input buffer
}
}
}
void AidApp::addProduct(bool isPerishable) {
if (isPerishable) {
AmaPerishable p_product;
cin >> p_product;
if (cin.fail()) {
cin.clear();
cin.ignore(2000, '\n'); // clear input buffer
cout << endl << p_product << endl << endl;
} else {
product_[noOfProducts_] = &p_product;
noOfProducts_++;
saveRecs();
cout << endl << "Product added" << endl << endl;
}
} else {
AmaProduct n_product;
cin >> n_product;
if (cin.fail()) {
cin.clear();
cin.ignore(2000, '\n'); // clear input buffer
cout << endl << n_product << endl << endl;
} else {
product_[noOfProducts_] = &n_product;
noOfProducts_++;
saveRecs();
cout << endl << "Product added" << endl << endl;
}
}
}
int AidApp::run() {
bool exit = false;
int choice, index;
char sku[MAX_SKU_LEN + 1];
while (!exit) {
choice = menu();
switch (choice) {
case 1:
listProducts();
pause();
break;
case 2:
cout << "Please enter the SKU: ";
cin >> sku;
cin.ignore(2000, '\n'); // clear input buffer
cout << endl;
index = SearchProducts(sku);
if (index == -1)
cout << "Not found!";
else
product_[index]->write(cout, false);
cout << endl << endl;
pause();
break;
case 3:
addProduct(false);
loadRecs();
break;
case 4:
addProduct(true);
loadRecs();
break;
case 5:
cout << "Please enter the SKU: ";
cin >> sku;
cout << endl;
addQty(sku);
break;
case 0:
exit = true;
cout << "Goodbye!!" << endl;
break;
default:
cout << "===Invalid Selection, try again.===" << endl;
}
}
return 0;
}
}