-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSalesSystemVer2.cpp
More file actions
457 lines (423 loc) · 16.7 KB
/
SalesSystemVer2.cpp
File metadata and controls
457 lines (423 loc) · 16.7 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
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
#include <iostream>
#include <string>
#include <fstream>
#include <chrono>
#include <ctime>
using namespace std;
/***************************************************Structures****************************************************************/
struct Toys{
string name;
int quantity;
double price;
};
struct Billing{
string name;
int quantity;
double price;
};
/*****************************************************************************************************************************/
/*************************************************Input Validation************************************************************/
int inputValid(bool t, int num, int type){
while(true){
if(t == true){
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n');
if(type == 1 ){
cout<<"\t\t\t\t\t"<<"Please enter the available option : ";
cin>>num;
}else if(type ==2){
cout<<"\t\t\t\t"<<"Please enter a valid no. : ";
cin>>num;
}else if(type ==3 ){
cout<<"\t\t\t\t\t"<<"Please enter a valid discount % (0-100) : ";
cin>>num;
}else if(type ==4 ){
cout<<"\t\t\t\t\t"<<"Please enter a valid amount of payment : ";
cin>>num;
}else{
cout<<"\t\t\t\t"<<"Please enter a valid input : ";
cin>>num;
}
}
if(!cin.fail()){
break;
}
}
return num;
}
/*****************************************************************************************************************************/
/*************************************************Add items and bills*********************************************************/
//Add new item to the store
Toys* addItem(Toys t[], int size){
cout<<"\t\t\t\t\t"<<"Name: ";
cin>> t[size].name;
cout<<"\t\t\t\t\t"<<"Quantity: ";
cin>> t[size].quantity;
// Input validation
t[size].quantity = inputValid(cin.fail(), t[size].quantity, 5 );
cout<<"\t\t\t\t\t"<<"Price: ";
cin>> t[size].price;
// Input validation
t[size].price = inputValid(cin.fail(), t[size].price, 5 );
return t;
}
// Add an item to the billing statement
Billing* addBill(Billing b[], Toys t[], int num, int quantity, int size){
b[size].name = t[num].name;
b[size].quantity = quantity;
b[size].price = t[num].price;
return b;
}
/****************************************************************************************************************************/
/*************************************************Display items and bills****************************************************/
// Display all the current items in the store
void displayItems(Toys t[], int size){
double total =0;
cout<<"\t\t\t\t\t"<<" List of items in Toys store"<<endl;
cout<<"\t\t\t\t"<<"----------------------------------------------------"<<endl;
cout<<"\t\t\t\t"<<"\tName\t\tQuantity\tPrice"<<endl;
for(int i = 0 ; i < size+1 ; i++){
if(t[i].quantity==0){
cout<<"\t\t\t\t"<< t[i].name << " is out of stock."<<endl;
continue;
}else{
cout<<"\t\t\t\t"<<i+1<<".\t"<< t[i].name <<"\t\t "<< t[i].quantity <<"\t\t "<< t[i].price <<"\t "<<endl;
}
}
cout<<endl;
}
// Display billing statement
void displayBill(Billing b[], int size){
double total =0;
cout<<"\t\t\t\t"<<"\tName\t\tQuantity\tPrice\tTotalPrice"<<endl;
for(int i = 0 ; i < size+1 ; i++){
cout<<"\t\t\t\t"<<i+1<<".\t"<< b[i].name <<"\t\t "<< b[i].quantity <<"\t\t "<< b[i].price <<"\t "<< b[i].price*b[i].quantity <<endl;
total += b[i].price*b[i].quantity;
}
cout<<endl;
cout<<"\t\t\t\t"<<"----------------------------------------------------------------"<<endl;
cout<<"\t\t\t\t"<<"Total :\t\t\t\t\t\t "<<total<<endl;
cout<<"\t\t\t\t"<<"----------------------------------------------------------------"<<endl;
}
/****************************************************************************************************************************/
/*************************************Update necessary information for items and bills***************************************/
// Update existing items
Toys* updateItem(Toys t[], int num){
Toys* ptr;
ptr = t;
ptr = addItem(t, num);
return ptr;
}
// Update quantity of the items after adding the selected item to the billing statement
Toys* updateQuantity(Toys t[], int num, int quantity){
t[num].quantity = t[num].quantity - quantity;
return t;
}
// Update the toys data
void updateData(Toys t[], int size){
ofstream writeFile;
writeFile.open("StockList.txt", ios::trunc);
writeFile<<"Name\tQuantity\tPrice"<<endl;
for(int i =0; i < size + 1; i++){
if( i == size){
writeFile<< t[i].name <<"\t"<< t[i].quantity <<"\t\t"<< t[i].price;
}else{
writeFile<< t[i].name <<"\t"<< t[i].quantity <<"\t\t"<< t[i].price <<endl;
}
}
writeFile.close();
}
// Delete selected item from the billing statement and update the latest billing statement
Billing* deleteBill(Billing b[],int num, int size){
for(int i = num ; i < size ; i++){
b[i].name = b[i+1].name;
b[i].quantity = b[i+1].quantity;
b[i].price = b[i+1].price;
}
return b;
}
/****************************************************************************************************************************/
/*************************************Print and Display transaction history**************************************************/
// Display current transaction and write the billing statement to the TransactionHistory file
void printTransaction(Billing b[], int size, int discount, int transaction, double payment){
double total =0;
time_t now = time(0);
char* dt = ctime(&now);
cout<<"\t\t\t"<<"************************************************************"<<endl;
cout<<"\t\t\t\t\tTransaction Number :\t#"<<transaction<<endl;
cout<<"\t\t\t\t\t "<<dt<<endl;
cout<<"\t\t\t"<<"\tName\t\tQuantity\tPrice\tTotalPrice"<<endl;
for(int i = 0 ; i < size+1 ; i++){
cout<<"\t\t\t"<<i+1<<".\t"<< b[i].name <<"\t\t "<< b[i].quantity <<"\t\t "<< b[i].price <<"\t "<< b[i].price*b[i].quantity <<endl;
total += b[i].price*b[i].quantity;
}
total = total *(100-discount)/100;
int change = payment - total;
cout<<endl;
cout<<"\t\t\t"<<"------------------------------------------------------------"<<endl;
cout<<"\t\t\t"<<"Discount \t\t\t\t\t "<< discount <<"%"<<endl;
cout<<"\t\t\t"<<"Total \t\t\t\t\t\t "<< total <<endl;
cout<<"\t\t\t"<<"Payment \t\t\t\t\t "<< payment <<endl;
cout<<"\t\t\t"<<"Change \t\t\t\t\t\t "<< change <<endl;
cout<<"\t\t\t"<<"************************************************************"<<endl;
cout<<endl;
cout<<endl;
// Write file
ofstream writeFile;
writeFile.open("TransactionHistory.txt", ios::app);
writeFile<<"************************************************************"<<endl;
writeFile<<"\t\tTransaction Number :\t#"<<transaction<<endl;
writeFile<<"\t\t "<<dt<<endl;
writeFile<<"\tName\t\tQuantity\tPrice\tTotalPrice"<<endl;
for(int i =0; i < size + 1; i++){
writeFile<<i+1<<".\t"<< b[i].name <<"\t\t "<< b[i].quantity <<"\t\t "<< b[i].price <<"\t "<< b[i].price*b[i].quantity <<endl;
}
writeFile<<endl;
writeFile<<"------------------------------------------------------------"<<endl;
writeFile<<"Discount \t\t\t\t\t "<< discount <<"%"<<endl;
writeFile<<"Total \t\t\t\t\t\t "<< total <<endl;
writeFile<<"Payment \t\t\t\t\t "<< payment <<endl;
writeFile<<"Change \t\t\t\t\t\t "<< change <<endl;
writeFile<<"************************************************************"<<endl;
writeFile<<endl;
writeFile<<endl;
writeFile<<endl;
}
// Display all the transactions made
void showTransactionHistory(){
string txt;
// Read file
ifstream readFile;
readFile.open("TransactionHistory.txt", ios::in);
while (getline (readFile, txt)) {
cout << txt <<endl;
}
}
/****************************************************************************************************************************/
/****************************************Obtain necessary data for the system************************************************/
// Read the data
Toys* readData(Toys t[]){
string txt;
ifstream readFile;
readFile.open("StockList.txt", ios::in);
int i = 0;
getline(readFile, txt); // Skip one line
while(!readFile.eof()){
readFile >> t[i].name;
readFile >> t[i].quantity;
readFile >> t[i].price;
i++;
}
readFile.close();
return t;
}
// get the numbers of types of the toys in the store
int getSize(Toys t[]){
string txt;
ifstream readFile;
readFile.open("StockList.txt", ios::in);
getline(readFile, txt);// Skip one line
int i = 0;
int size = 0;
while(!readFile.eof()){
readFile >> t[i].name;
readFile >> t[i].quantity;
readFile >> t[i].price;
i++;
size++;
}
readFile.close();
return size;
}
// Get the transaction number
int getTransactionNumber(){
string line;
ifstream readFile;
readFile.open("TransactionHistory.txt");
int num = 0;
if(readFile.is_open()){
while (getline(readFile, line)){
auto position = line.find("#");
if( position <= line.size() )
{
num= stoi( line.substr( position + 1));
}
}
}else{
num = 11035;
}
num++;
readFile.close();
return num;
}
/****************************************************************************************************************************/
/*******************************************Billing Loop*********************************************************************/
void billingLoop(Toys t[], int number, int size){
Toys* ptr = t;
string tab = "\t\t\t\t\t";
// For bills
Billing b[100];
Billing* ptrB;
ptrB = b;
int sizeB = 0;
int quantity = 0;
int discount = 0;
int transaction = getTransactionNumber(); // Auto-increment Transaction number
double payment = 0;
int input;
do{
cout<<tab<<"\tBilling System\n"<<endl;
cout<<tab<< "1. Select item to the bill" <<endl;
cout<<tab<< "2. Delete item from the bill" <<endl;
cout<<tab<< "3. Calculate and print the bill" <<endl;
cout<<tab<< "4. Exit Billing Menu" <<endl;
cout<<tab<< "Option : ";
cin>>input;
input = inputValid(cin.fail(), input, 1);
cout<<endl;
switch(input){
case 1: // Select item into billing statement
displayItems(ptr, size-1);
cout<<"\t\t\t\t"<<"Type -1 to exit this section"<<endl;
// While loop to continue inserting selected item to the billing statement
while(number!=-1){
cout<<"\t\t\t\t"<<"Select an item (no.) to be added to the bill : ";
cin >> number;
// Input validation
number = inputValid(cin.fail(), number, 2);
if(number>size){
cout<<"\t\t\t\t"<<"Invalid option. Please select the current available option."<<endl;
cout<<endl;
break;
}else if(number<1){
cout<<endl;
cout<<endl;
break;
}
cout<<"\t\t\t\t"<<"Quantity to be sold : ";
cin >> quantity;
// Input validation
quantity = inputValid(cin.fail(), quantity, 5);
cout<<endl;
//Insert selected item into the billing statement
ptrB = addBill(ptrB, ptr, number-1, quantity, sizeB);
ptr = updateQuantity(ptr, number-1, quantity);
sizeB++;
}
updateData(ptr, size-1);
displayBill(ptrB, sizeB-1);
number = 0;
cout<<"\n\n\n";
break;
case 2: // Select item to be delected from the billing statement
displayBill(ptrB, sizeB-1);
cout<<"\t\t\t\t"<<"Select which item (no.) to be deleted from the bill : ";
cin >> number;
// Input validation
number = inputValid(cin.fail(), number, 2);
// Delete selected item from the billing statement
ptrB = deleteBill(ptrB, number-1, sizeB);
sizeB--;
displayBill(ptrB, sizeB-1);
break;
case 3: // Finalize the billing statement with required inputs(discount, payment)
cout<<tab<<"Discount % : ";
cin >> discount;
// Input validation
discount = inputValid(cin.fail(), discount, 3);
cout<<tab<<"Amount Paid : ";
cin >> payment;
// Input validation
payment = inputValid(cin.fail(), payment, 4);
cout<<endl;
// Print current transaction and write the details into TransactionHistory file
printTransaction(ptrB, sizeB-1, discount,transaction,payment);
transaction++;
//Clear the billing statement
sizeB = sizeB - sizeB;
input = 4;
break;
case 4:// Exit Billing system
cout<<tab<<"Exiting Billing System...."<<endl;
break;
default:
cout<<tab <<"Please enter the available option."<<endl;
break;
}
}while(input!=4);
}
/****************************************************************************************************************************/
/************************************************************MAIN************************************************************/
int main(){
// For toys
Toys t[100];
Toys* ptr;
ptr = t;
ptr = readData(ptr);
int input = 0;
int size = getSize(ptr);
int number =0;
string tab = "\t\t\t\t\t";
// Do while loop for user to select the available options
do{
cout<<"\n\n"<<tab<<" Welcome to the POS System\n"<<endl;
cout<<tab<< "1. View all toys information" <<endl;
cout<<tab<< "2. Add new item to the store" <<endl;
cout<<tab<< "3. Update item" <<endl;
cout<<tab<< "4. Billing System"<<endl;
cout<<tab<< "5. Show transaction history"<<endl;
cout<<tab<< "6. Exit"<<endl;
cout<<tab<< "Option : ";
cin>>input;
// Input validation
input = inputValid(cin.fail(), input, 1);
cout<<"\n\n";
switch(input){
case 1 :// Display all the toys information
displayItems(ptr, size-1);
break;
case 2 ://add new item
ptr = addItem(ptr, size);
cout<<endl;
cout<<"\t\t\t\t"<<" *** New "<<t[size].quantity<<" <"<<t[size].name<<"> are added to the store ***"<<endl;
size++;
//Update the StockList file
updateData(ptr, size-1);
break;
case 3://update existing item
displayItems(ptr, size-1);
number = size + 1;
// while loop for repeating input until it is valid
while(number>size){
cout<<"\t\t\t\t"<<"Which item (no.) do you wish to update : ";
cin>> number;
// Input validation
number = inputValid(cin.fail(), number, 2);
if(number>size){
cout<<"\t\t\t\t"<<"Invalid option. Please select the current available option."<<endl;
cout<<endl;
continue;
}
ptr = updateItem(ptr, number-1);
//Update the StockList file
updateData(ptr, size-1);
cout<<endl;
cout <<"\t\t\t\t"<<"The list has updated to the latest information."<<endl;
}
break;
case 4:// Managing the billing statement
billingLoop(ptr, number, size);
break;
case 5:// Display all the transactions made
showTransactionHistory();
break;
case 6://Exit POS System
cout<<tab<< "Exiting...";
break;
default:
cout<<tab<< "Please enter the available option.";
break;
}
}while(input!=6);
return 0;
}