-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
286 lines (230 loc) · 10 KB
/
main.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
// Kyle Marler
// CIS 1111
// Christmas Shoppe Inventory Manager
/* This program is an inventory manager for a fictitious Christmas store with two locations at The Greene and the Dayton Mall. It allows the user to view inventory, input their own inventory, and obtain the total inventory at both locations. */
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;
using std::ofstream;
// Global constants
// Represents the number of store locations.
const int NUM_STORES = 2;
// Represents the maximum number of allowed items in the array.
const int MAX_ITEMS = 100;
// Beginning of a list of function prototypes.
/* This function named inputInventory accepts both arrays as arguements and allows the user to add elements to both of them. */
void inputInventory(int inventoryArray[MAX_ITEMS][NUM_STORES],string itemArray[MAX_ITEMS]);
/* This function reads the data from a saved file and displays the contents to the console. */
void displaySavedInventory();
/* This function reads the data from a saved file and calls the overloaded function below to calculate the sum. */
void sumOfInventory(int inventoryArray[MAX_ITEMS][NUM_STORES],string itemArray[MAX_ITEMS]);
/* This function sums all the values in inventory and returns the quantity. */
int sumOfInventory(int inventoryArray[MAX_ITEMS][NUM_STORES],int count);
int main()
{
// Array of ints to store the quantity in inventory at both store locations.
int inventoryArray[MAX_ITEMS][NUM_STORES];
// Array of strings to store the item names.
string itemArray[MAX_ITEMS];
int menuSelection; // Variable to hold the user's menu selection.
char repeatSelection; // Variable to hold user's response to repeat program.
cout << "Kyle's Christmas Shoppe\n";
cout << "Inventory Management System\n\n";
cout << "##### This program manages the inventory of various items\n";
cout << "at both our 'The Greene' and Dayon Mall locations. #####\n\n";
do
{
cout << "----------------------------\n";
cout << "1. Add Inventory\n";
cout << "2. Display Saved Inventory From a File\n";
cout << "3. Display Sum of Inventory\n";
cout << "4. Exit Program\n\n";
cout << "Please select from the listed menu options (1-4): ";
cin >> menuSelection;
// Validate user input.
while (menuSelection != 1 && menuSelection != 2 && menuSelection != 3 && menuSelection != 4)
{
cout << "Please try again and enter a number (1-4): ";
cin >> menuSelection;
}
switch(menuSelection)
{
case 1: // Input inventory
inputInventory(inventoryArray,itemArray);
cout << "\nWould you like to go back to the main menu?\n";
cout << "Enter Y for yes or N to quit the program: ";
cin >> repeatSelection;
break;
case 2: // Display saved inventory from a file
displaySavedInventory();
cout << "\nWould you like to go back to the main menu?\n";
cout << "Enter Y for yes or N to quit the program: ";
cin >> repeatSelection;
break;
case 3: // Display sum of inventory.
sumOfInventory(inventoryArray,itemArray);
cout << "\nWould you like to go back to the main menu?\n";
cout << "Enter Y for yes or N to quit the program: ";
cin >> repeatSelection;
break;
case 4: // Exit Program.
cout << "\nThank you. You are now exiting the program.";
repeatSelection = 'n';
break;
}
cout << endl;
} while (repeatSelection == 'Y' || repeatSelection == 'y');
cout << endl;
return 0;
}
// Beginning of a list of function definiitons.
/* This function named inputInventory accepts both arrays as arguements and allows the user to add elements to both of them. */
void inputInventory(int inventoryArray[MAX_ITEMS][NUM_STORES],string itemArray[MAX_ITEMS])
{
// Variable to store the name of the items.
string itemNames;
// Variable to store item quantities.
int itemQuantity;
cout << "\n----------------------------\n";
// Detailed instructions regarding item input criteria.
cout << "\nThis portion of the program will collect the names of items\n";
cout << "you wish to keep track of in inventory.\n\n";
cout << "*IMPORTANT*: Please only use one word with no spaces for the item name.\n";
cout << "When finished, enter F for finished.\n\n";
cout << "Item 1: ";
cin >> itemNames;
// Variable to count how many items have been added to inventory.
// Also serves as an index in the below if statement.
int count = 0;
// Detailed instructions regarding quantity input criteria.
if (itemNames != "F" && itemNames != "f")
{
itemArray[count] = itemNames;
cout << "\nNow, please enter the quantity in inventory\n";
cout << "for " << itemNames << " at The Greene location.\n";
cout << "\n*IMPORANT*: This should be a whole number above zero.\n";
cout << "Quantity for " << itemNames << ": ";
cin >> itemQuantity;
inventoryArray[count][0] = itemQuantity;
cout << "\nPlease enter the quantity\n";
cout << "in inventory for " << itemNames << " at the Dayton Mall location.\n";
cout << "Quantity for " << itemNames << ": ";
cin >> itemQuantity;
inventoryArray[count][1] = itemQuantity;
count++; // Increment the count.
cout << "\nPlease enter another item name to add to inventory, or F to finish: ";
cin >> itemNames;
}
// More concise prompts since the user knows the input criteria now.
while (itemNames != "F" && itemNames != "f" && count < MAX_ITEMS)
{
itemArray[count] = itemNames;
for (int index = 0; index < NUM_STORES; index++)
{
cout << "Enter inventory store " << (index + 1) << ": ";
cin >> itemQuantity;
inventoryArray[count][index] = itemQuantity;
}
count++; // Increment the count.
cout << "\nPlease enter another item name to add to inventory, or F to finish: ";
cin >> itemNames;
}
/* Saving/writing user input to a file. I had to look online for how to get the read position to the end of the file before writing
so that it wouldn't keep deleting the contents every time the user added an item to inventory. */
ofstream fout;
fout.open("inventory.txt", std::ios_base::app);
for (int items = 0; items < count; items++)
{
fout << itemArray[items] << "\t\t";
for (int stores = 0; stores < NUM_STORES; stores++)
{
fout << inventoryArray[items][stores] << "\t";
}
fout << endl;
}
fout.close();
}
/* This function reads the data in a saved file and displays the contents to the console. */
void displaySavedInventory()
{
// Reading data from a file and displaying to cout.
ifstream fin;
fin.open("inventory.txt");
// Variable to store the string contents of the file as it's being read.
string tempItems;
// Variable to store the int contents of the file as it's being read.
int tempInventory;
cout << "\n----------------------------\n";
cout << "\nThis portion of the program reads the data from a file that was previously saved.\n\n";
cout << setw(20) << left << "Item" << setw(20) << left
<< "The Greene Qty" << setw(20) << left << "Dayton Mall Qty\n";
cout << "---------------------------------------\n";
int count = 0; // Variable to count the number of items.
while (fin >> tempItems)
{
cout << setw(20) << left << tempItems;
fin >> tempInventory;
cout << setw(20) << left << tempInventory;
fin >> tempInventory;
cout << setw(20) << left << tempInventory;
cout << endl;
count++;
}
if (count == 0)
{
cout << "There is no inventory to display. The file is empty.\n";
}
}
/* This function reads the data from a saved file and calls the overloaded function below to calculate the sum. */
void sumOfInventory(int inventoryArray[MAX_ITEMS][NUM_STORES],string itemArray[MAX_ITEMS])
{
// Reading data from a file and displaying to cout.
ifstream fin;
fin.open("inventory.txt");
// Variable to store the string contents of the file as it's being read.
string tempItems;
// Variable to store the int contents of the file as it's being read.
int tempInventory;
// Variable to count how many items have been added to inventory.
// Also serves as an index in the below while loop.
int count = 0;
// Reading data from the file into both arrays.
while (fin >> tempItems)
{
itemArray[count] = tempItems;
fin >> tempInventory;
inventoryArray[count][0] = tempInventory;
fin >> tempInventory;
inventoryArray[count][1] = tempInventory;
count++; // Increment the count.
}
// Display if the file is empty.
if (count == 0)
{
cout << "\nYou have no items in inventory.\n";
}
if (count > 0)
{
int totalInventory = 0;
cout << "\n----------------------------\n";
cout << "\nThis portion of the program displays your total inventory\n";
cout << "at both store locations.\n";
totalInventory = sumOfInventory(inventoryArray, count);
cout << "\nTotal Inventory: " << totalInventory << endl;
}
}
/* This function sums all the values in inventory and returns the quantity. */
int sumOfInventory(int inventoryArray[MAX_ITEMS][NUM_STORES], int count)
{
int total = 0;
for (int items = 0; items < count; items++)
{
for (int stores = 0; stores < NUM_STORES; stores++)
{
total += inventoryArray[items][stores];
}
}
return total;
}