SHOW:
|
|
- or go back to the newest paste.
1 | #include <iostream> | |
2 | #include <iomanip> | |
3 | #include <fstream> | |
4 | #include <string> | |
5 | #include <sstream> | |
6 | #include <vector> | |
7 | #include <stdio.h> | |
8 | using namespace std; | |
9 | ||
10 | //Variables | |
11 | int shopSize; | |
12 | int orders = 0; | |
13 | int numbers[150]; | |
14 | //We use orders as a counter, in order to know when we should stop looking at the array | |
15 | ||
16 | //Structures | |
17 | struct itemEntry { | |
18 | int itemNumber; | |
19 | string name; | |
20 | double cost; | |
21 | int quantity; | |
22 | }; | |
23 | ||
24 | //Dynamic Memory for shop and cart | |
25 | itemEntry * shop; | |
26 | itemEntry * cart; | |
27 | ||
28 | //Functions | |
29 | bool Read(); //Reads from the .csv and puts it into the shop arrays | |
30 | void Display(); //Displays the receipt | |
31 | int search(int match, itemEntry *list); //Returns the array LOCATION of the barcode (item number) | |
32 | void printCart(); | |
33 | void writeFile(); | |
34 | ||
35 | int main() { | |
36 | //Calls read to put the data from the csv into the shop arrays | |
37 | Read(); | |
38 | ||
39 | int userItemNum; //value the user inputs into the console to tell us what product # he wants | |
40 | int userItemAmount; //value the user inputs into the console to tell us how many of the product he wants | |
41 | int arrayNum; // a position variable, where the value is held in the array location wise | |
42 | string userInput = ""; | |
43 | bool keepGo = true; | |
44 | cout << "at any time, enter cart to view your cart" <<endl; | |
45 | //loop keeps going until keepGo breaks | |
46 | while (keepGo) { | |
47 | cout << "Enter the Item Number" << endl; | |
48 | //takes the users input, checks if its cart, and stores it in userItemNum | |
49 | cin >> userInput; | |
50 | if (userInput == "cart") { | |
51 | printCart(); | |
52 | } | |
53 | stringstream(userInput) >> userItemNum; | |
54 | ||
55 | if (userItemNum == 0) { //escape loop, finished buying | |
56 | keepGo = false; //this is the break condition for the while loop | |
57 | } | |
58 | //searches for the position in the array the product is in and stores it in arrayNum | |
59 | arrayNum = search(userItemNum, shop); | |
60 | if (arrayNum == -1) { | |
61 | cout << "Error, product not found" << endl; | |
62 | } | |
63 | else { //if the product was found | |
64 | //check if there's a duplicate in the cart | |
65 | int storage = shop[arrayNum].quantity; | |
66 | int duplicate = search(userItemNum, cart); | |
67 | if (duplicate != -1) { | |
68 | storage = storage - cart[duplicate].quantity; | |
69 | } | |
70 | //print product information | |
71 | cout << "Product name is: " << "Product " << (arrayNum + 1) << endl; | |
72 | cout << "Price per unit is: " << shop[arrayNum].cost << endl; | |
73 | cout << "amount in stock is: " << storage << endl; | |
74 | ||
75 | //asks the user how many they want until its a valid number | |
76 | do { | |
77 | cout << "How many would you like?" << endl; | |
78 | if (storage == 0) { | |
79 | cout << "We are out of stock of " << "Product " << (arrayNum + 1) << endl; | |
80 | userItemAmount = 0; | |
81 | } | |
82 | else { | |
83 | //takes the users input, checks if its cart, and stores it in userItemAmount | |
84 | cin >> userItemAmount; | |
85 | if (userItemAmount > storage) { //check if the request is larger than the stock | |
86 | cout << "There is not enough available please re-enter " << endl; | |
87 | } | |
88 | } | |
89 | } while (userItemAmount > storage); //keeps asking for an amount that is less than the storage | |
90 | if (duplicate != -1) { | |
91 | cart[duplicate].quantity = cart[duplicate].quantity + userItemAmount; | |
92 | } | |
93 | else { | |
94 | cart[orders].itemNumber = userItemNum; //puts in the cart the barcode of the product | |
95 | cart[orders].name = shop[arrayNum].name; | |
96 | cart[orders].cost = shop[arrayNum].cost; | |
97 | cart[orders].quantity = userItemAmount; //puts in the cart the amount of product bought | |
98 | numbers[orders] = arrayNum; | |
99 | orders++; //increases the shopping cart size counter | |
100 | } | |
101 | } | |
102 | } | |
103 | Display(); //displays receipt | |
104 | cout << "press enter to quit" << endl; | |
105 | cin >> userInput; | |
106 | } | |
107 | ||
108 | //linear search looking for the location of the product within the array using itemNumber | |
109 | int search(int match, itemEntry *list) { //the parameter 'match' is the user's inputted bar code (item number) | |
110 | for (int i = 0; i < shopSize; i++) { //both arrays are size shopSize so it loops up to then | |
111 | if (match == list[i].itemNumber) { //if 'match' is equivalent to the array value stored at i, then return i | |
112 | return i; | |
113 | } | |
114 | } | |
115 | return -1; //this happens when we've looped through the entire array and didn't find a match | |
116 | } | |
117 | ||
118 | bool Read() { | |
119 | //array values | |
120 | int itemNu;//value that goes into the array | |
121 | string nam = "";//value that goes into the array | |
122 | double pric;//value that goes into the array | |
123 | int amoun;//value that goes into the array | |
124 | ||
125 | string translate;//value we read from the file to be translated / discarded | |
126 | ||
127 | //File Opener | |
128 | ifstream inFile; | |
129 | int a = 0;//counter used to parse through the array | |
130 | - | inFile.open("C:\\Users\\Miguel\\Desktop\\ProductData.csv"); //*change destination later* |
130 | + | inFile.open("ProductData.csv"); //*change destination later* |
131 | if (!inFile) { //file didn't open | |
132 | cout << "error! Couldn't open file!"; | |
133 | return false;// terminate with error | |
134 | } | |
135 | ||
136 | //getLine( SOURCE, Variable you're putting the value in, and a 'delimiter' (where the line stops) ) | |
137 | //Catches the number of data entries in the csv file | |
138 | getline(inFile, translate, '\n'); | |
139 | stringstream(translate) >> shopSize; //use StringStream to feed int variable 'shopSize' numbers from our string 'translate' | |
140 | //Initializes the Dynamic Memory allocations for the shop inventory and the cart inventory | |
141 | shop = new itemEntry[shopSize]; | |
142 | cart = new itemEntry[shopSize]; | |
143 | //goes through the headers, and does nothing with them | |
144 | getline(inFile, translate, ','); //The source is 'inFile', because that's what we named our file opener | |
145 | getline(inFile, translate, ','); //The variable we're storing these lines in is 'translate' | |
146 | getline(inFile, translate, ','); //The delimiter is ',' that is, it stops reading after the comma | |
147 | getline(inFile, translate, '\n'); //The .csv has 4 columns for each row, so after the 4th get line the delimiter would be '\n' | |
148 | ||
149 | while (inFile.good()) { //goes through file while it's not broken | |
150 | //Obtain untranslated values from the CSV and translates immedietly | |
151 | getline(inFile, translate, ',');//first value in the row is the item number | |
152 | stringstream(translate) >> itemNu; | |
153 | getline(inFile, nam, ',');//second value in the row is the name of the product | |
154 | getline(inFile, translate, ',');//third value in the row is the price | |
155 | stringstream(translate) >> pric; | |
156 | getline(inFile, translate, '\n');//final is the amount, and then we go to a new line | |
157 | stringstream(translate) >> amoun; | |
158 | ||
159 | //fill the array with translated values | |
160 | shop[a].itemNumber = itemNu; | |
161 | //shop[a].name = nam; | |
162 | shop[a].cost = pric; | |
163 | shop[a].quantity = amoun; | |
164 | //new row so increase 'a' by one so we can still put the values in the right location of the array | |
165 | a++; | |
166 | } | |
167 | inFile.close(); | |
168 | return true; | |
169 | } | |
170 | ||
171 | void Display() { | |
172 | string infoOne; //name | |
173 | string infoTwo; //Street Address | |
174 | string infoThree; //City, Zipcode | |
175 | double billb = 0; | |
176 | getline(cin, infoOne); | |
177 | //takes input for name | |
178 | cout << "Enter Name" << endl; | |
179 | getline(cin, infoOne); | |
180 | //takes input for street address | |
181 | cout << "Enter Street Address" << endl; | |
182 | getline(cin, infoTwo); | |
183 | //takes input for city zipcode | |
184 | cout << "Enter City, State Zipcode" << endl; | |
185 | getline(cin, infoThree); | |
186 | ||
187 | //opens an output file to print this too, every line is repeated to print to console and to the text file | |
188 | ofstream myfile; | |
189 | - | myfile.open("C:\\Users\\Miguel\\Desktop\\Invoie.txt"); |
189 | + | myfile.open("Invoie.txt"); |
190 | //formatting and output | |
191 | cout << "Customer Name: " << infoOne << endl; | |
192 | myfile << "Customer Name: " << infoOne << endl; | |
193 | cout << "Customer Address : " << infoTwo << endl; | |
194 | myfile << "Customer Address : " << infoTwo << endl; | |
195 | cout << " " << infoThree << endl; | |
196 | myfile << " " << infoThree << endl; | |
197 | printCart(); | |
198 | myfile << "Item Number Item Name Qty Unit cost Total" << endl; | |
199 | myfile << "......................................................................." << endl; | |
200 | myfile << "..............." << endl; | |
201 | for (int i = 0; i < orders; i++) { //uses the counter, 'orders' to know when stop reading from the array | |
202 | double itemCash = 0; //total cost of this product (amount * price) | |
203 | myfile << "Item Number " << i << " "; | |
204 | myfile << "Product " << numbers[i]; | |
205 | myfile << cart[i].quantity << " "; | |
206 | myfile << cart[i].cost << " "; | |
207 | //Calculate total cost of this individual product | |
208 | itemCash = cart[i].quantity * cart[i].cost; | |
209 | myfile << itemCash << endl; | |
210 | //adds the total cost of this individual product to the total bill | |
211 | billb = billb + itemCash; | |
212 | } | |
213 | myfile << "......................................................................." << endl; | |
214 | myfile << "..............." << endl; | |
215 | myfile << " Total:$" << billb << endl; | |
216 | ||
217 | myfile.close(); | |
218 | writeFile(); | |
219 | delete[] shop; | |
220 | delete[] cart; | |
221 | } | |
222 | void writeFile() { | |
223 | - | //remove("C:\\Users\\Miguel\\Desktop\\ProductData.csv"); |
223 | + | //remove("ProductData.csv"); |
224 | ofstream csvfile; | |
225 | - | csvfile.open("C:\\Users\\Miguel\\Desktop\\ProductData.csv"); |
225 | + | csvfile.open("ProductData.csv"); |
226 | csvfile << shopSize << endl; | |
227 | csvfile << "Item Number, Name, Cost, Qty" << endl; | |
228 | for (int i = 0; i < shopSize; i++) { | |
229 | int take = 0; | |
230 | int findtake = search(shop[i].itemNumber, cart); | |
231 | if (findtake != -1) { | |
232 | take = cart[findtake].quantity; | |
233 | } | |
234 | csvfile << shop[i].itemNumber << ","; | |
235 | csvfile << "Product " << numbers[i] << ","; | |
236 | csvfile << shop[i].cost << ","; | |
237 | csvfile << shop[i].quantity - take << endl; | |
238 | } | |
239 | } | |
240 | void printCart() { | |
241 | double bill = 0; //Bill size | |
242 | cout << "shopping cart:" << endl; | |
243 | cout << "Item Number Item Name Qty Unit cost Total" << endl; | |
244 | cout << "......................................................................." << endl; | |
245 | cout << "..............." << endl; | |
246 | for (int i = 0; i < orders; i++) { //uses the counter, 'orders' to know when stop reading from the array | |
247 | double itemCash = 0; //total cost of this product (amount * price) | |
248 | cout << "Item Number " << i << " "; | |
249 | cout << "Product " << numbers[i] << " "; | |
250 | cout << cart[i].quantity << " "; | |
251 | cout << cart[i].cost << " "; | |
252 | //Calculate total cost of this individual product | |
253 | itemCash = cart[i].quantity * cart[i].cost; | |
254 | cout << itemCash << endl; | |
255 | //adds the total cost of this individual product to the total bill | |
256 | bill = bill + itemCash; | |
257 | } | |
258 | cout << "......................................................................." << endl; | |
259 | cout << "..............." << endl; | |
260 | cout << " Total:$" << bill << endl; | |
261 | } |