Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <fstream>
- #include <string>
- #include <sstream>
- #include <vector>
- #include <stdio.h>
- using namespace std;
- //Variables
- int shopSize;
- int orders = 0;
- int numbers[150];
- //We use orders as a counter, in order to know when we should stop looking at the array
- //Structures
- struct itemEntry {
- int itemNumber;
- string name;
- double cost;
- int quantity;
- };
- //Dynamic Memory for shop and cart
- itemEntry * shop;
- itemEntry * cart;
- //Functions
- bool Read(); //Reads from the .csv and puts it into the shop arrays
- void Display(); //Displays the receipt
- int search(int match, itemEntry *list); //Returns the array LOCATION of the barcode (item number)
- void printCart();
- void writeFile();
- int main() {
- //Calls read to put the data from the csv into the shop arrays
- Read();
- int userItemNum; //value the user inputs into the console to tell us what product # he wants
- int userItemAmount; //value the user inputs into the console to tell us how many of the product he wants
- int arrayNum; // a position variable, where the value is held in the array location wise
- string userInput = "";
- bool keepGo = true;
- cout << "at any time, enter cart to view your cart" <<endl;
- //loop keeps going until keepGo breaks
- while (keepGo) {
- cout << "Enter the Item Number" << endl;
- //takes the users input, checks if its cart, and stores it in userItemNum
- cin >> userInput;
- if (userInput == "cart") {
- printCart();
- }
- stringstream(userInput) >> userItemNum;
- if (userItemNum == 0) { //escape loop, finished buying
- keepGo = false; //this is the break condition for the while loop
- }
- //searches for the position in the array the product is in and stores it in arrayNum
- arrayNum = search(userItemNum, shop);
- if (arrayNum == -1) {
- cout << "Error, product not found" << endl;
- }
- else { //if the product was found
- //check if there's a duplicate in the cart
- int storage = shop[arrayNum].quantity;
- int duplicate = search(userItemNum, cart);
- if (duplicate != -1) {
- storage = storage - cart[duplicate].quantity;
- }
- //print product information
- cout << "Product name is: " << "Product " << (arrayNum + 1) << endl;
- cout << "Price per unit is: " << shop[arrayNum].cost << endl;
- cout << "amount in stock is: " << storage << endl;
- //asks the user how many they want until its a valid number
- do {
- cout << "How many would you like?" << endl;
- if (storage == 0) {
- cout << "We are out of stock of " << "Product " << (arrayNum + 1) << endl;
- userItemAmount = 0;
- }
- else {
- //takes the users input, checks if its cart, and stores it in userItemAmount
- cin >> userItemAmount;
- if (userItemAmount > storage) { //check if the request is larger than the stock
- cout << "There is not enough available please re-enter " << endl;
- }
- }
- } while (userItemAmount > storage); //keeps asking for an amount that is less than the storage
- if (duplicate != -1) {
- cart[duplicate].quantity = cart[duplicate].quantity + userItemAmount;
- }
- else {
- cart[orders].itemNumber = userItemNum; //puts in the cart the barcode of the product
- cart[orders].name = shop[arrayNum].name;
- cart[orders].cost = shop[arrayNum].cost;
- cart[orders].quantity = userItemAmount; //puts in the cart the amount of product bought
- numbers[orders] = arrayNum;
- orders++; //increases the shopping cart size counter
- }
- }
- }
- Display(); //displays receipt
- cout << "press enter to quit" << endl;
- cin >> userInput;
- }
- //linear search looking for the location of the product within the array using itemNumber
- int search(int match, itemEntry *list) { //the parameter 'match' is the user's inputted bar code (item number)
- for (int i = 0; i < shopSize; i++) { //both arrays are size shopSize so it loops up to then
- if (match == list[i].itemNumber) { //if 'match' is equivalent to the array value stored at i, then return i
- return i;
- }
- }
- return -1; //this happens when we've looped through the entire array and didn't find a match
- }
- bool Read() {
- //array values
- int itemNu;//value that goes into the array
- string nam = "";//value that goes into the array
- double pric;//value that goes into the array
- int amoun;//value that goes into the array
- string translate;//value we read from the file to be translated / discarded
- //File Opener
- ifstream inFile;
- int a = 0;//counter used to parse through the array
- inFile.open("C:\\Users\\Miguel\\Desktop\\ProductData.csv"); //*change destination later*
- if (!inFile) { //file didn't open
- cout << "error! Couldn't open file!";
- return false;// terminate with error
- }
- //getLine( SOURCE, Variable you're putting the value in, and a 'delimiter' (where the line stops) )
- //Catches the number of data entries in the csv file
- getline(inFile, translate, '\n');
- stringstream(translate) >> shopSize; //use StringStream to feed int variable 'shopSize' numbers from our string 'translate'
- //Initializes the Dynamic Memory allocations for the shop inventory and the cart inventory
- shop = new itemEntry[shopSize];
- cart = new itemEntry[shopSize];
- //goes through the headers, and does nothing with them
- getline(inFile, translate, ','); //The source is 'inFile', because that's what we named our file opener
- getline(inFile, translate, ','); //The variable we're storing these lines in is 'translate'
- getline(inFile, translate, ','); //The delimiter is ',' that is, it stops reading after the comma
- getline(inFile, translate, '\n'); //The .csv has 4 columns for each row, so after the 4th get line the delimiter would be '\n'
- while (inFile.good()) { //goes through file while it's not broken
- //Obtain untranslated values from the CSV and translates immedietly
- getline(inFile, translate, ',');//first value in the row is the item number
- stringstream(translate) >> itemNu;
- getline(inFile, nam, ',');//second value in the row is the name of the product
- getline(inFile, translate, ',');//third value in the row is the price
- stringstream(translate) >> pric;
- getline(inFile, translate, '\n');//final is the amount, and then we go to a new line
- stringstream(translate) >> amoun;
- //fill the array with translated values
- shop[a].itemNumber = itemNu;
- //shop[a].name = nam;
- shop[a].cost = pric;
- shop[a].quantity = amoun;
- //new row so increase 'a' by one so we can still put the values in the right location of the array
- a++;
- }
- inFile.close();
- return true;
- }
- void Display() {
- string infoOne; //name
- string infoTwo; //Street Address
- string infoThree; //City, Zipcode
- double billb = 0;
- getline(cin, infoOne);
- //takes input for name
- cout << "Enter Name" << endl;
- getline(cin, infoOne);
- //takes input for street address
- cout << "Enter Street Address" << endl;
- getline(cin, infoTwo);
- //takes input for city zipcode
- cout << "Enter City, State Zipcode" << endl;
- getline(cin, infoThree);
- //opens an output file to print this too, every line is repeated to print to console and to the text file
- ofstream myfile;
- myfile.open("C:\\Users\\Miguel\\Desktop\\Invoie.txt");
- //formatting and output
- cout << "Customer Name: " << infoOne << endl;
- myfile << "Customer Name: " << infoOne << endl;
- cout << "Customer Address : " << infoTwo << endl;
- myfile << "Customer Address : " << infoTwo << endl;
- cout << " " << infoThree << endl;
- myfile << " " << infoThree << endl;
- printCart();
- myfile << "Item Number Item Name Qty Unit cost Total" << endl;
- myfile << "......................................................................." << endl;
- myfile << "..............." << endl;
- for (int i = 0; i < orders; i++) { //uses the counter, 'orders' to know when stop reading from the array
- double itemCash = 0; //total cost of this product (amount * price)
- myfile << "Item Number " << i << " ";
- myfile << "Product " << numbers[i];
- myfile << cart[i].quantity << " ";
- myfile << cart[i].cost << " ";
- //Calculate total cost of this individual product
- itemCash = cart[i].quantity * cart[i].cost;
- myfile << itemCash << endl;
- //adds the total cost of this individual product to the total bill
- billb = billb + itemCash;
- }
- myfile << "......................................................................." << endl;
- myfile << "..............." << endl;
- myfile << " Total:$" << billb << endl;
- myfile.close();
- writeFile();
- delete[] shop;
- delete[] cart;
- }
- void writeFile() {
- //remove("C:\\Users\\Miguel\\Desktop\\ProductData.csv");
- ofstream csvfile;
- csvfile.open("C:\\Users\\Miguel\\Desktop\\ProductData.csv");
- csvfile << shopSize << endl;
- csvfile << "Item Number, Name, Cost, Qty" << endl;
- for (int i = 0; i < shopSize; i++) {
- int take = 0;
- int findtake = search(shop[i].itemNumber, cart);
- if (findtake != -1) {
- take = cart[findtake].quantity;
- }
- csvfile << shop[i].itemNumber << ",";
- csvfile << "Product " << numbers[i] << ",";
- csvfile << shop[i].cost << ",";
- csvfile << shop[i].quantity - take << endl;
- }
- }
- void printCart() {
- double bill = 0; //Bill size
- cout << "shopping cart:" << endl;
- cout << "Item Number Item Name Qty Unit cost Total" << endl;
- cout << "......................................................................." << endl;
- cout << "..............." << endl;
- for (int i = 0; i < orders; i++) { //uses the counter, 'orders' to know when stop reading from the array
- double itemCash = 0; //total cost of this product (amount * price)
- cout << "Item Number " << i << " ";
- cout << "Product " << numbers[i] << " ";
- cout << cart[i].quantity << " ";
- cout << cart[i].cost << " ";
- //Calculate total cost of this individual product
- itemCash = cart[i].quantity * cart[i].cost;
- cout << itemCash << endl;
- //adds the total cost of this individual product to the total bill
- bill = bill + itemCash;
- }
- cout << "......................................................................." << endl;
- cout << "..............." << endl;
- cout << " Total:$" << bill << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement