Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Inventory Processing.cpp : Defines the entry point 4 the console application.
- //
- //1st I make sure I have the headers files I need
- #include "stdafx.h"
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <sstream>
- #include <iomanip>
- #include <vector>
- #include <stdlib.h>
- using namespace std;
- double calcProductsNonTax(string, double, string isTax, int);
- double calcProductsTax(string, double, string isTax, int);
- double calcTax(string, double, string isTax, int);
- bool isValid(int);
- int main()
- {
- //2nd I make sure I can open the file. I use 140-grades code as a reference
- ifstream infile;
- ofstream outputfile;
- string line; //one line of text
- string token; //ehhh i will just try with this...
- string receiptName;
- int tokenCount = 0;
- cout << "Enter the name of your receipt" << endl;
- getline(cin, receiptName);
- receiptName = receiptName + ".txt";
- cout << "your receipt name is " << receiptName << endl;
- infile.open("Minimart-Inventory.txt"); //oops I 4got the .txt and it gave me an error message XD
- outputfile.open(receiptName);
- int i = 0;
- string priceTemp[26]; //to be converted
- string item[26];
- double price[26];
- string tax[26];
- //double helpMe;
- //menu variables
- string enterAnother = "y";
- int userNum;
- double total = 0;
- double totalTax = 0;
- double grandTotalTax = 0;
- double grandTotal = 0;
- string nonTaxItem[1000];
- int nonTaxNumItems[1000];
- double nonTaxPrice[1000];
- string nonTaxTax[1000];
- string taxItem[1000];
- int taxNumItems[1000];
- double taxPrice[1000];
- string taxTax[1000];
- int iNonTax = 0;
- int iTax = 0;
- int units;
- int totalUnits = 0;
- int totalOrdersTax = 0;
- int totalOrdersNonTax = 0;
- bool isValidCode;
- cout << "below are available items you can purchase" << endl;
- cout << endl;
- if (!infile)
- {
- cout << "Could not open file to read. Ending Program" << endl;
- return -1;
- }
- while (!infile.eof()) //will return falses if you aren't at the the end of a file. so, it needs to true by placing ! 4 actions to keep occuring
- {
- getline(infile, line); //would need #include string
- //debugging // cout << "line: " << line << endl; //would need #include string
- stringstream ss(line);
- while (getline(ss, token, ' '))
- {
- switch (tokenCount)
- {
- tokenCount = 0;
- case 0:
- cout << "* * * * * * * * * * * * * * * * * " << endl;
- cout << i << ") ";
- cout << "Item: " << token;
- item[i] = (token);
- break;
- case 1:
- cout << "; Price: " << token;
- priceTemp[i] = (token);
- break;
- case 2:
- cout << "; Taxability: " << token << endl;
- tax[i] = (token);
- }
- tokenCount++;
- }
- i++;
- tokenCount = 0;
- }
- for (int y = 0; y < 26; y++)
- {
- price[y] = atof(priceTemp[y].c_str());
- //cout << price[y] << endl; //debug if it is stored
- }
- //cout << "done with inputting data from file... time to convert!" << endl;
- cout << endl;
- cout << "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -" << endl;
- cout << "above are available items you can purchase" << endl;
- cout << "input the corresponding number to the item to make your purchase" << endl;
- cout << "* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * " << endl;
- //debug if the items are still there
- /*
- for (int e = 0; e < 26; e++)
- {
- cout << item[e] << endl;
- cout << price[e] << endl;
- cout << tax[e] << endl;
- }
- */
- while (enterAnother == "y")
- {
- cout << "Enter your number (0-25): ";
- cin >> userNum;
- isValidCode = isValid(userNum);
- while (isValidCode == false)
- {
- cout << "re-enter please" << " ";
- cin >> userNum;
- isValidCode = isValid(userNum);
- }
- if (tax[userNum] == "N")
- {
- nonTaxItem[iNonTax] = item[userNum];
- nonTaxPrice[iNonTax] = price[userNum];
- nonTaxTax[iNonTax] = tax[userNum];
- cout << "You ordered this item: ";
- cout << nonTaxItem[iNonTax] << " ";
- cout << nonTaxPrice[iNonTax] << " ";
- cout << nonTaxTax[iNonTax] << " " << endl;
- cout << "how many units of this item do you want? ";
- cin >> units;
- while (units < 0)
- {
- cout << "Invalid, enter a number that is positive: ";
- cin >> units;
- }
- cout << endl;
- totalUnits = totalUnits + units;
- //cout << "you ordered this many units " << units << endl;
- nonTaxNumItems[iNonTax] = units;
- iNonTax++;
- total = calcProductsNonTax(item[userNum], price[userNum], tax[userNum], units);
- cout << "Total is (from main) " << "$" << total << endl;
- totalOrdersNonTax = totalOrdersNonTax + 1;
- cout << "this is totalOrdersNonTax " << totalOrdersNonTax << endl;
- }
- else if (tax[userNum] == "T")
- {
- taxItem[iTax] = item[userNum];
- taxPrice[iTax] = price[userNum];
- taxTax[iTax] = tax[userNum];
- cout << "You ordered this item: ";
- cout << taxItem[iTax] << " ";
- cout << taxPrice[iTax] << " ";
- cout << taxTax[iTax] << " " << endl;
- cout << "how many units of this item do you want? ";
- cin >> units;
- cout << endl;
- totalUnits = totalUnits + units;
- //cout << "you ordered this many units " << units << endl;
- taxNumItems[iTax] = units;
- iTax++;
- totalTax = calcTax(item[userNum], price[userNum], tax[userNum], units);
- total = calcProductsTax(item[userNum], price[userNum], tax[userNum], units);
- cout << "Total is (from main) " << setprecision(2) << fixed << "$" << total << endl;
- totalOrdersTax = totalOrdersTax + 1;
- cout << "This is the totalOrdersTax " << totalOrdersTax << endl;
- grandTotalTax = grandTotalTax + totalTax;
- }
- cout << endl;
- grandTotal = grandTotal + total;
- cout << "Grand Total Tax is " << setprecision(2) << fixed << "$" << grandTotalTax << endl;
- cout << "Grand total is (w/o tax)" << setprecision(2) << fixed << "$" << grandTotal << endl;
- //cout << "The total number of items you bought are " << totalUnits << endl;
- cout << "* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * " << endl;
- cout << "do you want to continue another purchase?" << endl;
- cout << "(Press y to continue or press n to end then press enter)" << endl;
- cin >> enterAnother;
- cin.ignore();
- cout << "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -" << endl;
- if (!(enterAnother == "y"))
- {
- cout << "Program ending, Here's you receipt saved as " << receiptName << endl;
- cout << "These are your purchased items: " << endl;
- outputfile << "These are your purchased items: " << endl;
- grandTotal = grandTotal + grandTotalTax;
- int u = 0;
- while (u < totalOrdersNonTax)
- {
- cout << nonTaxItem[u] << " ";
- outputfile << nonTaxItem[u] << " ";
- cout << "x" << nonTaxNumItems[u] << " ";
- outputfile << "x" << nonTaxNumItems[u] << " ";
- cout << setprecision(2) << fixed << "$" << nonTaxPrice[u] << " ";
- outputfile << setprecision(2) << fixed << "$" << nonTaxPrice[u] << " ";
- cout << nonTaxTax[u] << " " << endl;
- outputfile << nonTaxTax[u] << " " << endl;
- u++;
- }
- int o = 0;
- while (o < totalOrdersTax)
- {
- cout << taxItem[o] << " ";
- outputfile << taxItem[o] << " ";
- cout << "x" << taxNumItems[o] << " ";
- outputfile << "x" << taxNumItems[o] << " ";
- cout << setprecision(2) << fixed << "$" << taxPrice[o] << " ";
- outputfile << setprecision(2) << fixed << "$" << taxPrice[o] << " ";
- cout << taxTax[o] << " " << endl;
- outputfile << taxTax[o] << " " << endl;;
- o++;
- }
- cout << endl;
- outputfile << endl;
- cout << "Here's the final tax cost " << setprecision(2) << fixed << "$" << grandTotalTax << endl;
- outputfile << "Here's the final tax cost " << setprecision(2) << fixed << "$" << grandTotalTax << endl;
- cout << "Here's the final cost with tax added " << setprecision(2) << fixed << "$" << grandTotal << endl;
- outputfile << "Here's the final cost with tax added " << setprecision(2) << fixed << "$" << grandTotal << endl;
- cout << "Thank you for your online order." << endl;
- outputfile << "Thank you for your online order." << endl;
- }
- }
- infile.close();
- outputfile.close();
- return 0;
- }
- double calcProductsNonTax(string item, double priceInDollars, string isTax, int numOfItems)
- {
- double total;
- total = priceInDollars * numOfItems;
- //cout << "The total is (from function)" << setprecision(2) << fixed << "$" << total << endl;
- return total;
- }
- double calcProductsTax(string item, double priceInDollars, string isTax, int numOfItems)
- {
- double total;
- total = priceInDollars * numOfItems;
- //cout << "The total is (from function)" << setprecision(2) << fixed << "$" << total << endl;
- return total;
- }
- double calcTax(string item, double priceInDollars, string isTax, int numOfItems)
- {
- double tax;
- tax = priceInDollars * 0.06 * numOfItems;
- cout << "The tax for this item is " << setprecision(2) << fixed << "$" << tax << endl;
- return tax;
- }
- bool isValid(int userNum)
- {
- while (userNum < 0 || userNum > 25)
- {
- cout << "Invalid, enter a number from 0-25: ";
- return false;
- }
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement