-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcs135_A10.cpp
234 lines (186 loc) · 5.33 KB
/
cs135_A10.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
//Captain-Price-TF-141
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>
#include <string>
#define MAX 50
using namespace std;
//structure to store item information
struct menuType
{
string itemName;
int quantity;
double price;
};
//read file contents and stores it in items array of object
void getItems(menuType items[], int &size, ifstream &infile)
{
//store file name
string fileName;
//loops till valid file name entered by the user
do
{
//accepts the file name
cout << "Enter filename: ";
cin >> fileName;
//opens the file for reading
infile.open(fileName.c_str());
//checks if the file unable to open for reading display's error message and stop
if (!infile)
{
fflush(stdin);
continue;
}
//otherwise valid file name come out of the loop
else
break;
} while (1);
//loops till end of the file for reading
while (!infile.eof())
{
//reads item name
infile >> items[size].itemName;
//reads item price
infile >> items[size].price;
//reads item quantity
infile >> items[size].quantity;
//increase the word counter
size++;
}
}
//sort the items based on quantity
void sortItemsByQuantity(menuType items[], int size)
{
//loops till number of items
for (int r = 0; r < size; r++)
{
//loops till number items minus outer loop value minus one
for (int c = 0; c < size - r - 1; c++)
{
//checks if current items quantity is greater than the next item quantity
if (items[c].quantity > items[c + 1].quantity)
{
//swapping values
menuType temp = items[c];
items[c] = items[c + 1];
items[c + 1] = temp;
}
}
}
}
//display an item
void outputItem(string itemName, int itemLen, double price, int priceLen)
{
cout << left << setw(itemLen) << itemName << setw(5) << "$" << right << fixed << setprecision(2) << setw(priceLen) << price << endl;
}
//accept a valid id index
void selectID(menuType items[], int size, int &index)
{
cout << "\nMake a selection : ";
cin >> index;
while (cin.fail() || index < 1 || index > size || (items[index - 1].quantity == 0))
{
cin.clear(); // back in 'normal' operation mode
cin.ignore(); // and remove the bad input
cout << "Make a selection : ";
cin >> index;
}
}
//display all the items
void displayItems(menuType items[], int size, int &index)
{
//loops till number of records
for (int x = 0; x < size; x++)
{
//checks if current item quantity is not zero then display the item by calling the function
if (items[x].quantity != 0)
{
cout << left << setw(5) << (x + 1);
outputItem(items[x].itemName, 25, items[x].price, 6);
}
}
//calls the function to select an id
selectID(items, size, index);
}
int main()
{
//array of items of size MAX
menuType items[MAX];
//store number of records
int size = 0;
int index = 0, qty;
double amount = 0.0;
//ifstream object declared
ifstream infile;
//store user choice
char ch;
//calls the function
getItems(items, size, infile);
cout << "Welcome to the Krusty Krab!" << endl;
//loops till user choice is 'N' or 'n'
do
{
//calls the function
sortItemsByQuantity(items, size);
//calls the function
displayItems(items, size, index);
//accepts the quantity
cout << "Alright, great choice!" << endl;
//loops till positive number quantity is entered by the user
do
{
cin.clear(); // back in 'normal' operation mode
cin.ignore(); // and remove the bad input
//accepts the quantity
cout << "How many orders of this item would you like? ";
cin >> qty;
//checks if quantity is greater than zero then come out of the loop
if (qty > 0)
{
//checks if user entered quantity is greater than the available quantity
//display message and come out of the loop
if (qty > items[index - 1].quantity)
{
cout << "We only have " << items[index - 1].quantity << " of these." << endl;
break;
}
cout << "Ok we'll bring that right out" << endl;
//calculate the amount
amount += (qty * items[index - 1].price);
//updates the quantity
items[index - 1].quantity -= qty;
break;
}
} while (cin.fail() || qty < 1);
//accepts the choice
cout << "Kitchen is still open, will this complete your order? ";
cin >> ch;
//loops till valid choice
do
{
//checks if choice is 'Y' or 'y' then come out of the loop
if (ch == 'Y' || ch == 'y')
break;
//checks if choice is 'N' or 'n' then come out of the loop
else if (ch == 'N' || ch == 'n')
break;
//invalid choice will loop
else
{
cout << "I'm going to need a better answer" << endl;
cout << "WILL THIS COMPLETE YOUR ORDER ??? ";
cin >> ch;
}
} while (1);
} while (ch == 'N' || ch == 'n');
//calculates tax
double tax = amount * 0.0725;
//displays details
cout << "Never mind, the kitchen JUST closed, now get out" << endl;
cout << "But you have to pay first!!!" << endl;
cout << left << setw(10) << "Amount" << setw(5) << "$" << right << setw(6) << amount << endl;
cout << left << setw(10) << "Tax" << setw(5) << "$" << right << setw(6) << tax << endl;
cout << left << setw(10) << "Total" << setw(5) << "$" << right << setw(6) << (amount + tax);
return 0;
}