-
Notifications
You must be signed in to change notification settings - Fork 0
/
AmaApp.cpp
421 lines (326 loc) · 9.72 KB
/
AmaApp.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
#include <cstring>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <limits>
using namespace std;
#include "AmaApp.h"
#include "Utilities.h"
#define _CRT_SECURE_NO_WARNINGS
namespace ama {
//custom constructor
AmaApp::AmaApp(const char fileName[]) :m_filename{ '\0' }, m_products{ nullptr }, m_noOfProducts(0){
if (fileName!= nullptr&&fileName[0]!='\0') {
std::strncpy(m_filename, fileName, 255);
m_filename[256] = '\0';
}
//load the prduct records from the file
loadProductRecords();
}
//function to deallocate all dynamic memory
//the function traverse through m_Products array
//and deallocate memory for object
void AmaApp::deleteElements() {
for (int i = 0; i < m_noOfProducts; i++) {
delete m_products[i];
m_products[i] = nullptr;
}
}
//destructor
AmaApp::~AmaApp() {
//deallocate all dynamic memory
deleteElements();
}
//function pause()
//this function pauses the execution at console
void AmaApp::pause()const {
cout << "Press Enter to continue...";
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
//function menu()
//show the menue to the user and accept user input
//returns -1 if invalid input
int AmaApp::menu()const {
int userSelection = 0;
cout << "Disaster Aid Supply Management Program" << '\n' <<
"--------------------------------------" << '\n' <<
"1- List products" << '\n' <<
"2- Search product" << '\n' <<
"3- Add non-perishable product" << '\n' <<
"4- Add perishable product" << '\n' <<
"5- Add to product quantity" << '\n' <<
"6- Delete product" << '\n' <<
"7- Sort products" << '\n'<<
"0- Exit program" << '\n' <<
"> ";
cin >> userSelection;
//clear the input buffer
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
//validate user input
if (userSelection<0&&userSelection>7) {
userSelection = -1;
}
return userSelection;
}
//function loadProductRecords()
//this funciton loads product information from the file
//if the file is not found it creates an empty file
void AmaApp::loadProductRecords() {
int readIndex = 0;
//delete existing content from m_product
deleteElements();
std::ifstream file(m_filename);
if (file.is_open()) {
//read m_products_size number of records
//and until eof is reached
//the loop reads one line from the file
for (int i = 0; i < m_products_size && (!file.eof()); i++) {
bool continueReading = false;
//read first character from file
//use this character to create instance of
//either product or perishable object
char tempvar = file.get();
iProduct* temp = createInstance(tempvar);
//check if object is created
//if created store it in m_products array
//continue with further reading
//if not created clear the whole line
if (temp != nullptr) {
m_products[i] = temp;
continueReading = true;
}
else {
file.clear();
file.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
//when object creation successful
//read the rest of the line
//store it in the member variables
if (continueReading) {
file.ignore(2000,',');
//read in non-interractive mode
m_products[i]->read(file, false);
file.ignore(2000, '\n');
readIndex++;
}
} //loop
file.clear();
file.close();
}
else {
//file not opened
//create empty file
std::ofstream file(m_filename);
file.close();
}
//update the no of products in the file
m_noOfProducts = readIndex;
}
//this function saves the product record
//stored in the m_products array into the file
void AmaApp::saveProductRecords()const {
//check if there are records in the array
if (m_noOfProducts > 0) {
ofstream file(m_filename);
if (file.is_open()) {
//write data in condensed mode
for (int i = 0; i < m_noOfProducts; i++) {
m_products[i]->write(file, write_condensed);
file << endl;
}
}
}
}
//function to print title into the console
void AmaApp::printTitle()const{
cout << "------------------------------------------------------------------------------------------------" << '\n'
<< "|" << setw(4) << right << "Row" << " |"
<< setw(8) << right << "SKU" << " |"
<< setw(17) << left << " Product Name" << " |"
<< setw(11) << left << " Unit" << " |"
<< setw(8) << right << "Price" << " |"
<< setw(4) << right << "Tax" << " |"
<< setw(7) << right << "QtyA" << " |"
<< setw(7) << right << "QtyN" << " |"
<< setw(11) << left << " Expiry" << " |" << '\n'
<< "|-----|---------|------------------|------------|---------|-----|--------|--------|------------|" << endl;
}
//this function prints the footer including the total cost
void AmaApp::printFooter(double totalCost)const
{
cout << "------------------------------------------------------------------------------------------------" << '\n'
<< "|" << setw(83) << right << "Total cost of support ($): | "
<< setprecision(2) << fixed << setw(10) << right << totalCost << " |" << '\n'
<< "------------------------------------------------------------------------------------------------" << endl << endl;
}
//this function prints the product infromation
//stored in the file into the console in table format
void AmaApp::listProducts()const {
double totalCost=0;
printTitle();
//print product information
for (int i = 0; i < m_noOfProducts; i++) {
cout << "|" << setw(4) << right << i + 1 << " |";
m_products[i]->write(cout, write_table);
cout << endl;
//calculate total cost
totalCost += *m_products[i];
}
//print the footer with total cost
printFooter(totalCost);
pause();
}
//this function finds the SKU parameter in the m_products array
//returns the address if found, otherwise nullptr
iProduct* AmaApp::find(const char* skuToFind)const {
iProduct* result=nullptr;
bool skuFound=false;
for (int i = 0; i < m_noOfProducts && !skuFound; i++) {
if (*(m_products[i]) == skuToFind) {
result = m_products[i];
skuFound = true;
}
}
return result;
}
//function addQty
//this function display the existing record of the product parameter
//then ask user to input desired quantity
//adds the quantity to the quantity on hand
//save the updated record in the file
//the maximum quantity acceptable is qtyNeeded - qtyAvailable
void AmaApp :: addQty(iProduct* product) {
int qty;
int maxQty;
cout << *product << endl << endl
<< "Please enter the number of purchased items: ";
cin >> qty;
//if the reading was successful, find the max qty needed
//if reading was not successful print error msg
if (!cin.fail()) {
maxQty = product->qtyNeeded() - product->qtyAvailable();
//only accept qty which is less than/equal max qty
if (qty <= maxQty) {
*product += qty;
}
else {
//if there is extra qty print message
*product += maxQty;
cout << "Too many items; only " << maxQty
<< " is needed. Please return the extra "
<< (qty - maxQty) << " items." << endl;
}
//update the file
saveProductRecords();
cout << endl << "Updated!" << endl;
}
else {
//prints error message
cout<<"Invalid quantity value!"<<endl;
}
//clear error state
cin.clear();
//flush buffer
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
//function to add product
//this function adds new product into the array
//get product information from the user
//update the file
void AmaApp::addProduct(char tag) {
//create instance
iProduct* newProduct = createInstance(tag);
if (newProduct != nullptr) {
//read information from the stream
cin >> *newProduct;
}
//check if reading was successful
if (cin.rdstate()==0) {
//add to the iProducts array
m_products[m_noOfProducts] = newProduct;
m_noOfProducts++;
//save product info ino the file
saveProductRecords();
cout << endl << "Success!" << endl;
}
else {
//print error message
cin.clear();
cin.ignore(2000, '\n');
cout << endl<<*newProduct<<endl;
}
}
//function to get SKU input from user
//this function ask user to input a SKU
//returns the SKU
const char* AmaApp::getSKUfromUser(char* skuToFind)const {
cout << "Please enter the product SKU: ";
cin.get(skuToFind, max_length_sku + 1, '\n');
//clear error state if any
cin.clear();
//clear buffer
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
return(skuToFind[0] != '\0' ? skuToFind : nullptr);
};
//function run()
//this function provides the user interaction functionality
int AmaApp::run() {
int userSelection = 0;
iProduct* tempProduct;
char skuToFind[max_length_sku + 1];
do {
//show the menu
userSelection = menu();
switch (userSelection){
case 1:
listProducts();
break;
case 2:
getSKUfromUser(skuToFind);
tempProduct = find(skuToFind);
if (tempProduct != nullptr) {
//display product info in human redable form
cout << endl<<*tempProduct<<endl;
}
else {
cout <<endl<< "No such product!" << endl;
}
pause();
break;
case 3:
addProduct('N');
loadProductRecords();
break;
case 4:
addProduct('P');
loadProductRecords();
break;
case 5:
getSKUfromUser(skuToFind);
tempProduct = find(skuToFind);
cout << endl;
if (tempProduct != nullptr) {
addQty(tempProduct);
}
else {
cout << "No such product!" << endl;
}
break;
case 6:
cout<<"This feature has not been implemented"<<endl;
break;
case 7:
cout<<"This feature has not been implemented"<<endl;
break;
case 0:
cout << "Goodbye!";
break;
default:
cout << "~~~Invalid selection, try again!~~~" << endl;
pause();
}
cout << endl;
} while (userSelection != 0);
return 0;
}
}