--------------------------------------------------------------------------------
ID: 3533115 • Letter: #
Question
--------------------------------------------------------------------------------------------
Given an input file (named products.txt) with a list of products in the following format:
4011 BANANAS 1 0.49 123.2
4383 MINNEOLAS 1 0.79 187.3
3144 TANGERINES 1 1.19 135.5
4028 STRAWBERRIES_PINT 0 0.99 104
4252 STRAWBERRIES_HALF_CASE 0 3.99 53
4249 STRAWBERRIES_FULL_CASE 0 7.49 67.
.
.
.
A) Write a program to perform grocery check-out procedure for a simple store. Use a dynamic array of structures (Up to 100) to store this information. For each product we store the following information:
- PLU code: Price Look Up Code. Unique for each product
- Product Name,
- Product Sales Type: 0 = per unit, 1 = per pound
- Price Per Pound or Price Per Unit,
- Current Inventory Level.
For example:
4011 BANANAS 1 0.49 123.2
4383 MINNEOLAS 1 0.79 187.3
3144 TANGERINES 1 1.19 135.5
4028 STRAWBERRIES_PINT 0 0.99 104
4252 STRAWBERRIES_HALF_CASE 0 3.99 53
4249 STRAWBERRIES_FULL_CASE 0 7.49 67
B) Your program should:
i. On initialization, read inventory from products.txt (also on eLearning).
ii. Do check-out functionality in a separate method/function.
iii. Enable the user to purchase several products in each checkout.
iv. After the user enters PLU code, ask the user to input the weight or # of units for each product (depending on its sales type) & keep up with the total ($).
v. User can enter 0 for PLU code to indicate the end of checkout.
vi. If the total purchase exceeds $50, apply 5% discount to the total.
vii. A list of items and their prices should be displayed along with the total due before and after discount
viii. You should perform input validation (i.e. PLU should be integer, no symbols or letters are valid as PLU, unit price, sales type or stock).
You need to keep track of inventories automatically as well. So, keep updating the inventory data along with checkout operations. When the store closes every day, inventory product information should be displayed and updated in the file inventory.txt.
Thus you will have two menu items: check out, and exit.
Define a constant SIZE to store the maximum number of products
Declare a global variable to be used as dynamic array
Define a structure named Product to store product information
To read the file, call a function with the following prototype and description:
bool readInventory(string filename):
This function receives a string with the filename and location of products.txt. It should open the file, and return false if any errors occur. Once the file is open, a dynamic array of Products is created to store up to 100 elements, and then it reads the file content line by line, and validates the input. If for a given product, the information is incorrect (i.e. PLU is not an integer), skip the product and continue with the next line. Return true if no error was found and the file was read successfully
double checkout()
This function is called for every customer. It continues asking for the PLU, units/pounds (see part B) until 0 is entered as PLU, and returns the total amount ($) for the current sale.
bool upDateInventory(string filename)
This function updates the inventory before terminating the program. The file name and location should be the same as for readInventory. Returns true if the inventory file was successfully updated.
You will turn in two files, a Text file and a C++ source code file.
The Text file should include:
1) Instructions to compile and run your code
2) A sample output.
The C source code file should:
1) Comply with all of the formatting requirements already discussed.
2) Implement the functions as described above
--------------------------------------------------------------------------------------------
The products.txt looks like this
4101 BRAEBURN_REG 1 0.99 101.5
4021 DELICIOUS_GDN_REG 1 0.89 94.2
4020 DELICIOUS_GLDN_LG 1 1.09 84.2
4015 DELICIOUS_RED_REG 1 1.19 75.3
4016 DELICIOUS_RED_LG 1 1.29 45.6
4167 DELICIOUS_RED_SM 1 0.89 35.4
4124 EMPIRE 1 1.14 145.2
4129 FUJI_REG 1 1.05 154.5
4131 FUJI_X-LGE 1 1.25 164.1
4135 GALA_LGE 1 1.35 187.7
4133 GALA_REG 1 1.45 145.2
4139 GRANNY_SMITH_REG 1 1.39 198.2
4017 GRANNY_SMITH_LGE 1 1.49 176.5
3115 PEACHES 1 2.09 145.5
4011 BANANAS 1 0.49 123.2
4383 MINNEOLAS 1 0.79 187.3
3144 TANGERINES 1 1.19 135.5
4028 STRAWBERRIES_PINT 0 0.99 104
4252 STRAWBERRIES_HALF_CASE 0 3.99 53
4249 STRAWBERRIES_FULL_CASE 0 7.49 67
94011 ORGANIC_BANANAS 1 0.99 56.3
--------------------------------------------------------------------------------------------
here's the code that needs to be modified/fixed/edited/changed so that the program can be runned smoothly
//Store.cpp
#include <iostream>
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <vector>
#include "product.h"
using namespace std;
Product inv[100]; //array of 100 objects
int c = 100; //maximum products is 100
int menu(void) //display user options
{
int option;
cout << "Enter 1 to begin checkout or 0 to exit" << endl;
cin >> option;
return option;
};
void updateFile() //output results to a new file
{
ofstream newInvFile("newinventory.txt");
for (int p = 0; p < c; p++)
{
newInvFile << inv[p].getPLU() << " " << inv[p].getProductName() << " " << inv[p].getType() << " " << inv[p].getPrice() << " " << inv[p].getInventory() << endl;
}
}
void checkout(void)
{
double total = 0;
double quantity;
int input;
int PLU = 0;
do
{
PLU = 0;
cout << "Enter PLU code or (0) to exit" << endl;
cin >> input;
if (input == 0)
break;
for (int p = 0; p < c; p++)
{
if (inv[p].getPLU() == input)
{
PLU = p;
break;
}
}
/*if (PLU == -1)
{
cout << "Please enter a valid PLU" << endl;
continue;
}*/
if (inv[PLU].getType() == 1)// Determine whether product is sold by weight or units
{
cout << "Weight: ";
}
else
{
cout << "Quantity: ";
}
cin >> quantity;
total += quantity * inv[PLU].getPrice();
inv[PLU].updateInventory(quantity);
}
while (input != 0);
cout << "Total: $" << total << endl;
if (total > 50) //apply discount if total is over $50
{
total = total * 0.95;
cout << "Your purchase of over $50 qualifies you for a 5% discount. Total: $" << total << endl;
}
}
int main()
{
int plu;
string productName;
int type;
double price;
double inventory;
c = 0;
ifstream original ("inventory.txt"); //read from original inventory file
if (original.is_open())
{
while (!original.eof())
{
original >> plu >> productName >> type >> price >> inventory;
Product temp = Product(plu, productName, type, price, inventory);
inv[c] = temp;
c++;
}
original.close();
}
int option;
do
{
cout << "WELCOME!" << endl;
option = menu();
if (option == 1)
checkout();
else
updateFile();
}
while(option != 0);
system ("pause");
}
//product.cpp
#include "product.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
Product::Product()
{
plu = 0;
productName = "none yet";
type = 0;
price = 0;
inventory = 0;
}
Product::Product(int pl, string pn, int t, double pr, double i)
{
plu = pl;
productName = pn;
type = t;
price = pr;
inventory = i;
}
int Product::getPLU()
{ return plu;}
string Product::getProductName()
{ return productName;}
int Product::getType()
{ return type;}
double Product::getPrice()
{ return price;}
double Product::getInventory()
{ return inventory;}
double Product::updateInventory(double quantity)
{
if (quantity > inventory)
cout << "This item is not in stock." << endl;
else inventory -= quantity;
return inventory;
}
//product.h
#ifndef store_product_h
#define store_product_h
#include <string>
#include <iostream>
using namespace std;
class Product
{
private:
int plu;
string productName;
int type;
double price;
double inventory;
public:
Product();
Product(int, string, int, double, double);
int getPLU();
string getProductName();
int getType();
double getPrice();
double getInventory();
double updateInventory(double);
};
#endif
Explanation / Answer
//Code added with functions as asked
//Store.cpp
#include <iostream>
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <vector>
#include "product.h"
using namespace std;
Product inv[100]; //array of 100 objects
int c = 100; //maximum products is 100
int menu(void) //display user options
{
int option;
cout << "Enter 1 to begin checkout or 0 to exit" << endl;
cin >> option;
return option;
};
bool upDateInventory(string filename) //output results to a new file
{
ofstream newInvFile(filename,ios::trunc);
for (int p = 0; p < c; p++)
{
newInvFile << inv[p].getPLU() << " " << inv[p].getProductName() << " " << inv[p].getType() << " " << inv[p].getPrice() << " " << inv[p].getInventory() << endl;
}
return true;
}
double checkout(void)
{
double total = 0;
double quantity;
int input;
int PLU = 0;
do
{
PLU = 0;
cout << "Enter PLU code or (0) to exit" << endl;
cin >> input;
if (input == 0)
break;
for (int p = 0; p < c; p++)
{
if (inv[p].getPLU() == input)
{
PLU = p;
break;
}
}
/*if (PLU == -1)
{
cout << "Please enter a valid PLU" << endl;
continue;
}*/
if (inv[PLU].getType() == 1)// Determine whether product is sold by weight or units
{
cout << "Weight: ";
}
else
{
cout << "Quantity: ";
}
cin >> quantity;
total += quantity * inv[PLU].getPrice();
inv[PLU].updateInventory(quantity);
}
while (input != 0);
cout << "Total: $" << total << endl;
if (total > 50) //apply discount if total is over $50
{
total = total * 0.95;
}
return total;
}
bool readInventory(string filename)
{
ifstream original (filename); //read from original inventory file
if (original.is_open())
{
while (!original.eof())
{
original >> plu >> productName >> type >> price >> inventory;
Product temp = Product(plu, productName, type, price, inventory);
inv[c] = temp;
c++;
}
original.close();
}
return true;
}
int main()
{
int plu;
string productName;
int type;
double price;
double inventory;
c = 0;
double finalTotal;
bool ret;
if(readInventory("inventory.txt")
{
int option;
do
{
cout << "WELCOME!" << endl;
option = menu();
if (option == 1)
finalTotal = checkout();
else
ret = upDateInventory("inventory.txt");
}
while(option != 0);
cout << "Your purchase of over $50 qualifies you for a 5% discount. Total: $" << finalTotal << endl;
if(ret)
cout << "Inventory updated" << endl;
else
cout << "Error in updating inventory file" << endl;
}
else{ cout << "Error in reading inventory file" << endl; }
system ("pause");
}
//product.cpp
#include "product.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
Product::Product()
{
plu = 0;
productName = "none yet";
type = 0;
price = 0;
inventory = 0;
}
Product::Product(int pl, string pn, int t, double pr, double i)
{
plu = pl;
productName = pn;
type = t;
price = pr;
inventory = i;
}
int Product::getPLU()
{ return plu;}
string Product::getProductName()
{ return productName;}
int Product::getType()
{ return type;}
double Product::getPrice()
{ return price;}
double Product::getInventory()
{ return inventory;}
double Product::updateInventory(double quantity)
{
if (quantity > inventory)
cout << "This item is not in stock." << endl;
else inventory -= quantity;
return inventory;
}
//product.h
#ifndef store_product_h
#define store_product_h
#include <string>
#include <iostream>
using namespace std;
class Product
{
private:
int plu;
string productName;
int type;
double price;
double inventory;
public:
Product();
Product(int, string, int, double, double);
int getPLU();
string getProductName();
int getType();
double getPrice();
double getInventory();
double updateInventory(double);
};
#endif
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.