-
Notifications
You must be signed in to change notification settings - Fork 0
/
cs135_A9.cpp
188 lines (153 loc) · 4.84 KB
/
cs135_A9.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
//Captain-Price-TF-141
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include<limits>
using namespace std;
//global constant
const int MAX_CAPACITY = 50;
//read file contents and stores it in items array of object
void getItems(string itemNames[], int quantities[], double prices[], int &size, ifstream &infile)
{
size = 0;
//checks if the file unable to open for reading display's error message and stop
while (!infile.eof())
{
infile >> itemNames[size] >> prices[size] >> quantities[size];
size++;
//opens the file for reading
if (infile.eof())
break;
}
}
//sort the items based on quantity
void sortItemByQuantity(string itemNames[], int quantities[], double prices[], int size)
{
int min;
//loops till number of items
for (int i = 0; i < size - 1; i++)
{
min = i;
//loops till number items minus outer loop value minus one
for (int j = i + 1; j < size; j++)
{
//checks if current items quantity is greater than the next item quantity
if (quantities[j] < quantities[min])
min = j;
}
if (min != i)
{
//swapping values
string tempItem = itemNames[i];
itemNames[i] = itemNames[min];
itemNames[min] = tempItem;
int tempQuantity = quantities[i];
quantities[i] = quantities[min];
quantities[min] = tempQuantity;
double tempPrice = prices[i];
prices[i] = prices[min];
prices[min] = tempPrice;
}
}
}
//display an item
void outputItem(string itemName, int itemLen, double price, int priceLen)
{
cout << left << setw(itemLen) << itemName << "$" << right << setw(priceLen) << fixed << setprecision(2) << price << endl;
}
int main() {
string filename;
ifstream fin;
//array of items of size MAX
string itemNames[MAX_CAPACITY];
int quantities[MAX_CAPACITY];
double prices[MAX_CAPACITY];
//store number of records
int size = 0;
int selectionID, quantity;
double totalPrice = 0;
//store user choice
char choice;
//ifstream object
cout << "Enter filename : ";
cin >> filename;
fin.open(filename.c_str());
while (!fin.is_open())
{
cout << "Enter filename : ";
cin >> filename;
fin.open(filename.c_str());
}
cout << "Welcome to the Krusty Krab!" << endl;
//calls the function
getItems(itemNames, quantities, prices, size, fin);
fin.close();
do {
//calls the function
sortItemByQuantity(itemNames, quantities, prices, size);
cout << endl;
for (int i = 0; i < size; i++)
{
if (quantities[i] > 0)
{
cout << left << setw(3) << (i + 1);
//calls the function
outputItem(itemNames[i], 20, prices[i], 10);
}
}
//quantity input
cout << "\nMake a selection : ";
cin >> selectionID;
while (cin.fail() || selectionID < 1 || selectionID > size || (quantities[selectionID - 1] == 0))
{
cin.clear(); // back in 'normal' operation mode
cin.ignore(); // and remove the bad input
cout << "Make a selection : ";
cin >> selectionID;
}
//quantity accepted
cout << "Alright, great choice! " << endl;
cout << "\nHow many orders of this item would you like ? ";
cin >> quantity;
while (cin.fail() || quantity < 1)
{
cin.clear(); // back in 'normal' operation mode
cin.ignore(); // and remove the bad input
cout << "How many orders of this item would you like ? ";
cin >> quantity;
}
if (quantity > quantities[selectionID - 1])
{
cout << "We only have " << quantities[selectionID - 1] << " of these " << endl;
}
else
{
cout << "Ok we'll bring that right out" << endl;
//updates the quantity
quantities[selectionID - 1] = quantities[selectionID - 1] - quantity;
//calculate the amount
totalPrice += (prices[selectionID - 1] * quantity);
}
cout << "\nKitchen is still open, will this complete you order? ";
cin >> choice;
//if choice is 'N' or 'n' and checks if choice is 'Y' or 'y'
while (cin.fail() || (choice != 'n' && choice != 'N' && choice != 'y' && choice != 'Y'))
{
cin.clear(); // back in 'normal' operation mode
cin.ignore(); // and remove the bad input
//invalid choice will loop
cout << "\nI'm going to need a better answer" << endl;
cout << "WILL THIS COMPLETE YOUR ORDER ??? ";
cin >> choice;
}
} while (choice == 'n' || choice == 'N');
cout << "\nNever mind, the kitchen JUST closed, now get out" << endl << "But you have to pay first!!!" << endl;
//calculates tax
double tax = (totalPrice *0.0725);
//displays details
cout << left << setw(10) << "Amount" << "$" << right << setw(8) << fixed << setprecision(2) << totalPrice << endl;
cout << left << setw(10) << "Tax" << "$" << right << setw(8) << fixed << setprecision(2) << tax << endl;
cout << left << setw(10) << "Total" << "$" << right << setw(8) << fixed << setprecision(2) << (tax + totalPrice) << endl;
return 0;
}