· 10 years ago · Sep 01, 2016, 03:58 AM
1/*******************************************************************************
2 Author: Matthew Pogue
3 Student ID: h735f787
4 Program: #1
5
6 Description of the problem:
7 A program to manage the inventory of my personal store.
8
9 Psuedo Code
10
11 Read file, and check that it exists
12 if file doesn't exists
13 create a blank file with the defalt item 0 and done
14 gwhile previous itemid++ = next id
15 create new structure with id
16 read file into structure
17 itemNum
18 item
19 quantity
20 price
21 total
22 display selection
23 switch
24 1)
25 list structures into a table
26 for i in parts
27 output itemid, item, quantity, cost, total
28 2)
29 adds another item
30 Get item:
31 Get quantity:
32 Get cost:
33 3)
34 Modifies a part
35 Displays that part, selectd by part number
36 Get item:
37 Get quantity:
38 Get cost:
39 4)
40 outputs total cost of inventory
41
42
43
44
45 Class Name:
46 Data:
47 Mutator Functions:
48 Accessor Functions:
49 Functions:
50 numToken:
51 input: string, a line from the file
52 output: none
53 return: the number of tokens in a string
54 description: returns the number of tokens in string
55 token - splites a string into muiltiple strings in an array
56 main - executes the file
57
58*******************************************************************************/
59
60// - include -
61#include <iostream>
62#include <fstream>
63#include <string>
64#include <iomanip>
65using namespace std;
66
67#define DEBUG true
68#define debugvar(name, var) if(DEBUG) { cout << name << " = " << var << endl; }
69#define debugsay(msg) if(DEBUG) { cout << msg << endl; }
70
71
72// structures -
73// part is a struct for every part in the IMP
74typedef struct
75{
76 int partNum;
77 string item;
78 int quantity;
79 float price;
80 float total;
81} part;
82
83// === FUNCTIONS ===
84// == MENU FUNCTION ==
85// Displays the selection menu
86int displayMenu()
87{
88 int option = 6;
89 while(option == 6 || option == 0)
90 {
91 if(option == 0)
92 {
93 cout << endl << "This wasn't a valid option, please try again.";
94 }
95 debugvar("option", option);
96 cout << endl;
97 cout << "1. Display items" << endl;
98 cout << "2. Add item" << endl;
99 cout << "3. Modify item" << endl;
100 cout << "4. Inventory total" << endl;
101 cout << "5. Quit IMP" << endl;
102 cout << "Please enter an option: ";
103 option = 0;
104 cin >> option;
105 cout << endl;
106 }
107 return option;
108}
109
110// displays cash including the space before the $
111// i.e. var = 123.456, return string is `| $123.45 |`
112void displayCash(float var)
113{
114 int space = 0;
115 if(var >= 0 && var < 10)
116 space = 5;
117 else if(var >= 10 && var < 100)
118 space = 4;
119 else if(var >= 100 && var < 1000)
120 space = 3;
121 else if(var >= 1000 && var < 10000)
122 space = 2;
123 else if(var >= 10000 && var < 100000)
124 space = 1;
125 cout << right << setw(space) << '$' << setprecision(3) << var << ' ';
126}
127
128// displays a single item's partNum, item, quantity, price, and total price
129// for the item
130void displayItemSingle(part parts[], int numParts, int num, bool seperator)
131{
132 char sep = ' ';
133 if(seperator == true)
134 sep='|';
135 cout << sep << right << setw(4) << parts[num].partNum << ' ' << sep;
136 cout << left << setw(20) << parts[num].item << ' ' << sep;
137 cout << right << setw(4) << parts[num].quantity << ' ' << sep;
138 displayCash(parts[num].price);
139 cout << sep;
140 displayCash(parts[num].total);
141 cout << parts[num].price << sep << endl;
142}
143
144// == CASE 1 ==
145void displayItems (part parts[], int numParts)
146{
147 if(numParts == 0)
148 {
149 cout << endl << "There were no items in inventory, please use option ";
150 cout << "2 to add an item." << endl;
151 }
152 else
153 {
154 cout << setfill('-') << setw(80) << '-' << setfill(' ') << endl;
155 for(int i=0; i<numParts; i++)
156 {
157 displayItemSingle(parts, numParts, i, true);
158 }
159 cout << setfill('-') << setw(80) << '-' << setfill(' ') << endl;
160 }
161}
162
163// == CASE 2 and 3 ==
164// changes as item as part of its item ID. This is used in both modify part and
165// adding parts. [case 2 and 3]
166void changeItem(part parts[], int numParts, int partid)
167{
168 cout << "Enter item: ";
169 cin >> parts[partid].item;
170 cout << "Enter quantity: ";
171 cin >> parts[partid].quantity;
172 cout << "Enter cost: ";
173 cin >> parts[partid].price;
174 parts[partid].total = parts[partid].price * parts[partid].quantity;
175}
176
177// adds an item using the changeItem function
178void addItem(part parts[], int numParts)
179{
180 changeItem(parts, numParts, numParts);
181 cout << "Item added:" << endl;
182 displayItemSingle(parts, numParts, numParts, false);
183 cout << endl;
184 numParts++;
185 cout << "In case of error, use option 3 to alter item from menu." << endl;
186}
187
188// modifys an item determined by a user entered part ID
189void modifyPart(part parts[], int numParts)
190{
191 cout << "Enter part to modify: ";
192 int partid;
193 cin >> partid;
194 displayItemSingle(parts, numParts, partid, false);
195 cout << endl;
196 changeItem(parts, numParts, partid);
197 cout << endl;
198}
199
200// == CASE 4 ==
201// adds up the cost of all the parts and otuputs it for the user
202void totalOfInventory(part parts[], int numParts)
203{
204 float total = 0;
205 for(int i = 0; i < numParts; i++)
206 {
207 total+=parts[i].total;
208 }
209 cout << "Total cost of inventory: $" << setprecision(3) << total << endl;
210}
211
212// - main -
213int main()
214{
215 int numParts = 0;
216 part parts[100];
217
218 // starts the infile stream to inventory.txt
219 ifstream infile;
220 infile.open("inventory.txt");
221 infile >> numParts;
222
223 // if the file doesn't exist, then it creats the file for the user with
224 // nothing inside
225 if(infile.fail())
226 {
227 cout << "An inventory.txt was not found so one was created\n";
228 debugvar("numParts", numParts);
229 }
230
231 // loops for how many items are in the inventory file and puts them into an
232 // array of parts
233 for(int i = 0; i < numParts; i++)
234 {
235 string next;
236 infile >> parts[i].partNum >> next;
237 while(next != ";")
238 {
239 parts[i].item.append(" ");
240 parts[i].item.append(next);
241 infile >> next;
242 }
243 infile >> parts[i].quantity >> parts[i].price >> parts[i].total;
244 }
245
246 // stops the outfile stream to inventory.txt
247 infile.close();
248
249 // main menu
250 bool stop = false;
251 while(stop != true)
252 {
253 switch(displayMenu())
254 {
255 case 1:
256 displayItems(parts, numParts);
257 break;
258 case 2:
259 addItem(parts, numParts);
260 break;
261 case 3:
262 modifyPart(parts, numParts);
263 break;
264 case 4:
265 totalOfInventory(parts, numParts);
266 break;
267 case 5:
268 stop = true;
269 break;
270 default:
271 cout << "That wasn't a valid option, please try again" << endl;
272 break;
273 }
274 }
275
276 // starts the outfile stream to inventory.txt
277 ofstream outfile;
278 outfile.open("inventory.txt");
279
280 outfile << numParts << endl;
281 for(int i = 0; i < numParts; i++)
282 {
283 outfile << parts[i].partNum << parts[i].item << ';';
284 outfile << parts[i].quantity << parts[i].price << parts[i].total;
285 outfile << endl;
286 }
287
288
289 // closes the outfile stream to inventory.txt
290 outfile.close();
291
292 return 0;
293}