-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSource.cpp
More file actions
286 lines (238 loc) · 9.7 KB
/
Source.cpp
File metadata and controls
286 lines (238 loc) · 9.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
#include "PriorityQueue.h"
#include "Heap.h"
#include <limits>
#include <iomanip>
#include <string>
#include <sstream>
using namespace std;
enum Menu { SHOW_INVENTORY = 1, ENTER_NEW_ORDER, PROCESS_DAILY_ORDERS, EXIT };
void placeNewOrder(int& numWidgetsRemaining, PriorityQueue& orderQueue, int& orderNumber);
void processDailyOrders(int numWidgetsRemaining, PriorityQueue& orderQueue);
int getUserNumWidgets(int numWidgetsRemaining);
RushStatus getUserRushStatus(bool& isOrderCancelled);
void purgeInputErrors(string errorMessage);
void printCurrentInventory(int numWidgetsRemaining);
stringstream getCurrentInventoryHeaderSS();
void printIndividualOrder(Order& order);
stringstream getIndividualOrderHeaderSS();
int main() {
cout << "Welcome to the Widget Ordering System\n\n";
int selection;
int orderNumber = FIRST_ORDER_NUM;
int numWidgetsRemaining = DEFAULT_INVENTORY_SIZE;
PriorityQueue orderQueue(MAX_HEAP_SIZE);
do {
cout << "MAIN MENU\nPlease select one of the following:\n"
"1. Show Inventory\n"
"2. Place New Order\n"
"3. Process Daily Orders\n"
"4. Exit\n"
"Selection: ";
cin >> selection;
switch (selection) {
case SHOW_INVENTORY:
printCurrentInventory(numWidgetsRemaining);
break;
case ENTER_NEW_ORDER:
placeNewOrder(numWidgetsRemaining, orderQueue, orderNumber);
break;
case PROCESS_DAILY_ORDERS:
processDailyOrders(numWidgetsRemaining, orderQueue);
break;
case EXIT:
cout << "\nGoodbye!\n";
break;
default:
purgeInputErrors("\nError: Invalid Menu Option\n");
}
} while (selection != EXIT && selection != PROCESS_DAILY_ORDERS);
cout << "Terminating Program\n";
return 0;
}
void placeNewOrder(int& numWidgetsRemaining, PriorityQueue& orderQueue, int& orderNumber) {
if (numWidgetsRemaining <= 0) {
cout << "\nNumber of Widget's In-Stock Today: " << numWidgetsRemaining << ". Please Process Daily Orders And Try Again Tomorrow.\n\n";
}
else {
cout << "\nNEW ORDER\n";
int numWidgetsToPurchase = getUserNumWidgets(numWidgetsRemaining);
if (numWidgetsToPurchase <= 0 || numWidgetsToPurchase > numWidgetsRemaining) {
cout << "Order Cancelled. Returning to Main Menu\n\n";
}
else {
// Get the rush status
bool isOrderCancelled = false;
RushStatus rushStatus = getUserRushStatus(isOrderCancelled);
if (isOrderCancelled == true) {
cout << "\nOrder Cancelled\nReturning to Main Menu\n\n";
}
else {
int warehouseCost = numWidgetsToPurchase * WIDGET_PRICE;
Order order(orderNumber, numWidgetsToPurchase, warehouseCost, rushStatus);
try {
orderQueue.enqueue(order);
numWidgetsRemaining -= numWidgetsToPurchase;
stringstream header = getIndividualOrderHeaderSS();
cout << header.str();
printIndividualOrder(order);
orderNumber++;
}
catch (FullPQ& e) {
cerr << "Error: " << e.what() << "\n";
}
}
}
}
}
void processDailyOrders(int numWidgetsRemaining, PriorityQueue& orderQueue) {
if (orderQueue.isEmpty()) {
cout << "\nNo Orders Placed Today\n\n";
}
else {
try {
double totalMarkup = 0.0;
double totalCostToCustomer = 0.0;
double totalCostToWarehouse = 0.0;
cout << "\n-DAILY ORDERS-\n\n";
// Testing Copy Constructor
PriorityQueue tempPQ(orderQueue.getMaxItems());
Order order;
while (!orderQueue.isEmpty()) {
orderQueue.dequeue(order);
tempPQ.enqueue(order);
totalMarkup += order.getMarkupAmount();
totalCostToCustomer += order.getCustomerCost();
totalCostToWarehouse += order.getWarehouseCost();
}
stringstream header = getIndividualOrderHeaderSS();
cout << header.str();
while (!tempPQ.isEmpty()) {
tempPQ.dequeue(order);
printIndividualOrder(order);
orderQueue.enqueue(order);
}
cout << setfill('.') << endl;
cout << setw(DAILY_TOTALS_WIDTH) << left << "Total Widgets Sold Today"
<< setw(TABLE_WIDTH - DAILY_TOTALS_WIDTH) << right << (DEFAULT_INVENTORY_SIZE - numWidgetsRemaining) << '\n'
<< setw(DAILY_TOTALS_WIDTH) << left << "Total Markup"
<< setw(TABLE_WIDTH - DAILY_TOTALS_WIDTH) << right << "$" + to_string(totalMarkup).substr(0, to_string(totalMarkup).find('.') + 3) << '\n'
<< setw(DAILY_TOTALS_WIDTH) << left << "Total Cost to Customer"
<< setw(TABLE_WIDTH - DAILY_TOTALS_WIDTH) << right << "$" + to_string(totalCostToCustomer).substr(0, to_string(totalCostToCustomer).find('.') + 3) << '\n'
<< setw(DAILY_TOTALS_WIDTH) << left << "Total Cost to Warehouse"
<< setw(TABLE_WIDTH - DAILY_TOTALS_WIDTH) << right << "$" + to_string(totalCostToWarehouse).substr(0, to_string(totalCostToWarehouse).find('.') + 3);
cout << "\n\nSuccessfully Processed All Orders\n\n";
}
catch (EmptyPQ& e) {
cerr << "Error: " << e.what() << "\n";
}
catch (FullPQ& e) {
cerr << "Error: " << e.what() << "\n";
}
}
}
int getUserNumWidgets(int numWidgetsRemaining) {
int numWidgetsToPurchase;
string input;
string errorMessage;
cout << "Please enter the number of widgets to order: ";
cin >> input;
try {
numWidgetsToPurchase = stoi(input);
if (numWidgetsToPurchase <= 0) {
errorMessage = "\nError: Quantity must be greater than 0";
purgeInputErrors(errorMessage);
numWidgetsToPurchase = SENTINEL;
}
else if (numWidgetsToPurchase > numWidgetsRemaining) {
errorMessage = "\nError: Only " + to_string(numWidgetsRemaining) + " widgets are available for purchase today";
purgeInputErrors(errorMessage);
numWidgetsToPurchase = SENTINEL;
}
}
catch (invalid_argument& e) {
errorMessage = "\nError: Invalid Input";
purgeInputErrors(errorMessage);
numWidgetsToPurchase = SENTINEL;
}
catch (out_of_range& e) {
errorMessage = "\nError: Invalid Input";
purgeInputErrors(errorMessage);
numWidgetsToPurchase = SENTINEL;
}
return numWidgetsToPurchase;
}
// Remove option to cancel, if they enter incorrectly aback to main
RushStatus getUserRushStatus(bool& isOrderCancelled) {
RushStatus rush = STANDARD;
int userInput;
cout << "\nPlease select the Delivery Speed:\n"
"1. Standard \n"
"2. Expedite\n"
"3. Extreme\n"
"4. Cancel Order\n"
"Selection: ";
cin >> userInput;
switch (userInput) {
case STANDARD:
case EXPEDITE_RUSH:
case EXTREME_RUSH:
rush = static_cast<RushStatus>(userInput);
cout << '\n';
break;
default:
purgeInputErrors("\nError: Invalid Delivery Speed");
isOrderCancelled = true;
}
return rush;
}
void purgeInputErrors(string errorMessage) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << errorMessage << endl;
}
void printCurrentInventory(int numWidgetsRemaining) {
cout << "\n-CURRENT INVENTORY-\n\n";
// Printing the table header
stringstream headerSS = getCurrentInventoryHeaderSS();
cout << headerSS.str();
int offset = AVAILABLE_WIDGETS_WIDTH - AVAILABLE_WIDGETS_LEN;
// Printing the table data
cout << right << setw(AVAILABLE_WIDGETS_WIDTH - offset)
<< numWidgetsRemaining << setw(offset) << "" << right << setw(WIDGET_PRICE_WIDTH) << fixed
<< setprecision(2) << WIDGET_PRICE << "\n\n";
}
stringstream getCurrentInventoryHeaderSS() {
stringstream ss;
// Printing the table header
ss << left << setw(AVAILABLE_WIDGETS_WIDTH) << "Available Widgets"
<< right << setw(WIDGET_PRICE_WIDTH) << "Price Per Widget ($)\n";
// Printing the line separator
ss << setfill('-') << setw(AVAILABLE_WIDGETS_WIDTH) << "" << setw(WIDGET_PRICE_WIDTH) << ""
<< setfill(' ') << "\n";
return ss;
}
void printIndividualOrder(Order& order) {
// Printing the table data
cout << left << setw(ORDER_WIDTH) << order.getOrderNumber()
<< left << setw(STATUS_WIDTH) << RUSH_STATUS_STRINGS[order.getRushStatus()]
<< right << setw(QUANT_WIDTH) << order.getQuantity()
<< right << setw(MARKUP_WIDTH) << fixed << setprecision(2) << order.getMarkupPercent()
<< right << setw(WH_COST_WIDTH) << order.getCustomerCost()
<< right << setw(WH_COST_WIDTH) << order.getWarehouseCost()
<< right << setw(MARKUP_WIDTH) << order.getMarkupAmount()
<< "\n\n";
}
stringstream getIndividualOrderHeaderSS() {
stringstream ss;
// Store the table header
ss << left << setw(ORDER_WIDTH) << "Order #"
<< left << setw(STATUS_WIDTH) << "Rush Status"
<< right << setw(QUANT_WIDTH) << "Quantity Ordered"
<< right << setw(MARKUP_WIDTH) << "Percent Markup (%)"
<< right << setw(WH_COST_WIDTH) << "Total Customer Cost ($)"
<< right << setw(WH_COST_WIDTH) << "Total Warehouse Cost ($)"
<< right << setw(MARKUP_WIDTH) << "Amount Markup ($)" << "\n";
// Store the line separator
ss << setfill('-') << setw(TABLE_WIDTH) << "" << setfill(' ') << "\n";
return ss;
}