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
|
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <sstream>
#include "product.h"
#include "Tokenizer.h"
#include "LookupTable.h"
LookupTable<Product *> table;
double checkout(int inputPLU, double weight)
{
double checkTotal = 0.0;
Product *temp = table[inputPLU];
checkTotal = temp->get_price() * weight;
return checkTotal;
}
int main() {
//Variable initialization
int plu = 0;
int in = 0;
int input = 0;
bool byWeight = 0;
double price = 0;
double inventory = 0;
double total = 0;
double weight = 0;
bool flag = false;
string name, line;
//Creates two different ranges for PLU code
table.addRange(0, 9999);
table.addRange(90000, 99999);
//Input and output file streams
ifstream myfile("inventory.csv");
ofstream outfile("output.csv");
//Opens file then reads and writes input until it runs out of lines
if (myfile.is_open())
{
while (getline(myfile, line))
{
Tokenizer tok(line, ",");
string token = tok.next();
istringstream(token) >> plu;
istringstream(token) >> name;
istringstream(token) >> byWeight;
istringstream(token) >> price;
istringstream(token) >> inventory;
table[plu] = new Product(plu, name, byWeight, price, inventory);
}
}
while (true)
{
do
{
std::cout << "Enter PLU code or 0 to exit: ";
outfile << "Enter PLU code or 0 to exit: ";
std::cin >> input;
outfile << input;
if (input == 0)
{
break;
}
else
{
std::cout << "Enter weight: ";
outfile << "Enter weight: ";
std::cin >> weight;
outfile << weight;
total += checkout(input, weight);
std::cout << "Your current total is: " << total << endl;
}
} while (input != 0);
std::cout << "Enter 1 to start a new purchase, or press 0 to quit./n";
std::cin >> in;
if (in == 1)
{
return true;
}
else if (in == 0)
{
return false;
}
else
{
while (flag)
{
std::cout << "I'm sorry, but you need to enter either 1 or 0./n";
std::cout << "Please enter 1 to continue, or 0 to quit.";
std::cin >> in;
if (in == 0 || in == 1)
{
flag = false;
}
else
flag = true;
}
}
}
std::cout << "Thank you for your purchase! We hope to see you soon!";
//later, in a loop to write output for each product, also do
//... delete table[plu];
}
|