-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCashier.cpp
295 lines (244 loc) · 6.15 KB
/
Cashier.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
///////////////////////////////////////////////////////////
// Cashier.cpp
// Implementation of the Class Cashier
// Author: Robert T
///////////////////////////////////////////////////////////
#include <iostream>
#include <string>
#include <atltime.h>
#include <iomanip>
#include "Cashier.h"
using namespace std;
Cashier::Cashier()
{
purchased_books = new string *[1];
}
Cashier::~Cashier()
{
}
void Cashier::menu()
{
int choice;
bool exit = false;
do
{
system("cls");
cout << "*****************************************" << endl;
cout << " Booksellers " << endl;
cout << " Cashier " << endl
<< endl;
cout << "1. Cash register" << endl;
cout << "2. Return to Main Menu " << endl;
cout << "*****************************************" << endl
<< endl;
cout << "Enter Your Choice:";
while (!(cin >> choice))
{
cin.clear();
cin.ignore(INT_MAX, '\n');
cout << "Invalid choice, please enter a whole number:";
}
if (choice == 1)
{ // Inventory Listing
purchaseBooks();
}
else
{
deleteMemory();
exit = true;
}
} while (exit != true);
}
void Cashier::purchaseBooks()
{
//listBooks();
deleteMemory();
bool exit = false;
string choice = "yes";
string isbn;
do
{
if (choice == "yes")
{
string keyword;
int quantity;
cout << "Please enter book (ISBN, title):";
cin >> keyword;
int searchRange[] = {0, 1}; // Searching book's ISBN, Title
int searchRangeSize = sizeof(searchRange) / sizeof(searchRange[0]);
int key_found = -1;
int index = 0;
do
{
selectionSort(getBookArray(), searchRange[index]);
key_found = binarySearch(getBookArray(), keyword, searchRange[index]);
index++;
} while (key_found == -1 && index < searchRangeSize);
//cout << "key:" << key_found << endl;
if (key_found != -1)
{
showbook(books[key_found], true);
int book_quantity = stoi(books[key_found][5]);
if (book_quantity > 0)
{
cout << "Please enter quantity (1-" << book_quantity << "):";
if (!(cin >> quantity) || quantity < 1 || quantity > book_quantity)
{
do
{
cin.clear();
cin.ignore(INT_MAX, '\n');
cout << "Error, please enter a whole number between 1 and " << book_quantity << ":";
} while (!(cin >> quantity) || quantity < 1 || quantity > book_quantity);
}
isbn = books[key_found][0];
addBookToCart(isbn, to_string(quantity));
}
else
{
cout << "Sorry! This book is sold out." << endl;
}
}
else
{
cout << "There are no books matching " << keyword << endl;
}
cout << "\n\n"
<< endl;
cout << "Add more book? (yes/no):";
cin >> choice;
}
else
{
exit = true;
}
} while (exit == false);
if (purchased_books_row > 0)
{
system("cls");
displayShopppingCart();
}
system("pause");
}
bool Cashier::addBookToCart(string isbn, string quantity)
{
bool book_existed = false;
// Find, weather book exists
for (int i = 0; i < purchased_books_row; i++)
{
if (purchased_books[i][0] == isbn)
{
int qty = stoi(purchased_books[i][1]) + stoi(quantity);
purchased_books[i][1] = to_string(qty); // quantity
book_existed = true;
}
}
if (book_existed == false)
{
purchased_books[purchased_books_row] = new string[2];
purchased_books[purchased_books_row][0] = isbn; // book isbn
purchased_books[purchased_books_row][1] = (quantity); // quantity
purchased_books_row++;
}
return true;
}
bool Cashier::placeOrder()
{
int key_found, new_qty;
for (int i = 0; i < purchased_books_row; i++)
{
// Find book by ISBN
key_found = binarySearch(getBookArray(), purchased_books[i][0], 0);
if (key_found > -1)
{
new_qty = stoi(books[key_found][5]) - stoi(purchased_books[i][1]);
books[key_found][5] = to_string(new_qty);
}
}
exportDatafile();
return true;
}
void Cashier::displayShopppingCart()
{
cout << endl;
cout << "Serendipity Book Sellers\n"
<< endl;
CTime time = CTime::GetCurrentTime();
string date = to_string(time.GetMonth()) + "/" + to_string(time.GetDay()) + "/" + to_string(time.GetYear());
cout << "Date:" << date << endl
<< endl;
cout << setw(15) << left << "Qty";
cout << setw(15) << left << "ISBN";
cout << setw(60) << left << "Title";
cout << setw(20) << left << "Price";
cout << setw(20) << left << "Total";
cout << endl;
cout << "--------------------------------------------------------------------------------------------------------------------------------" << endl;
// set precision
cout.setf(ios::showpoint);
cout.precision(2);
cout.setf(ios::fixed);
// Loop to show books
selectionSort(getBookArray(), 0);
int key_found = -1;
double amount = 0, subtotal = 0, tax = 0, total = 0;
for (int i = 0; i < purchased_books_row; i++)
{
// Find book by ISBN
key_found = binarySearch(getBookArray(), purchased_books[i][0], 0);
if (key_found > -1)
{
amount = stod(purchased_books[i][1]) * stod(books[key_found][7]);
subtotal += amount;
cout << setw(15) << left << purchased_books[i][1]; // qty
cout << setw(15) << left << books[key_found][0]; // isbn
cout << setw(60) << left << books[key_found][1]; // title
cout << setw(10) << right << books[key_found][7]; // price
cout << setw(20) << right << amount; // price
cout << endl;
}
}
tax = calculateTax(subtotal);
total = subtotal + tax;
cout << "\n\n\n";
cout << setw(80) << right << "Subtotal: ";
cout << setw(8) << right << subtotal << endl;
cout << setw(80) << right << "Tax: ";
cout << setw(8) << right << tax << endl;
cout << setw(80) << right << "Total: ";
cout << setw(8) << right << total << endl;
cout << "Thank you for Shopping at Serendipity!" << endl;
cout << "Confirmed checkout? (yes/no):";
string choice;
cin >> choice;
if (choice == "yes")
{
placeOrder();
cout << "Order placed." << endl;
}
else
{
cout << "Order cancelled." << endl;
}
}
void Cashier::deleteMemory()
{
// Delete array
for (int i = 0; i < purchased_books_row; i++)
{
delete[] purchased_books[i];
}
purchased_books_row = 0;
}
template <class T>
T Cashier::calculateTax(T subtotal)
{
if (subtotal > 0)
{
return taxRate * subtotal;
}
else
{
throw InvalidSubtotal();
}
}