Introduction Assume you’re writing software for a grocery store to maintain its
ID: 3748075 • Letter: I
Question
Introduction
Assume you’re writing software for a grocery store to maintain its inventory. You are provided a listing of items from the latest shipment to the store with each items name, quantity, price, and whether it is taxable or not. The store wants you to build a program that is able to store each grocery item in an internal list and ultimately calculate the potential revenue, tax revenue, and total revenue for the entire shipment.
Objective
You are given a partial implementation of two classes. GroceryItem is a class that holds the information for each grocery item. GroceryInventory is a class that holds an internal listing of GroceryItems as well as the tax rate for the store. Your inventory could be 100 items it could be 9000 items. You won’t know until your program gets the shipment at runtime. For this you should use the vector class from the C++ standard library.
Complete the implementation of these classes, adding public/private member variables and functions as needed. You should choose an appropriate data structure to maintain this inventory with an unknown size known only at runtime. Your code is tested in the provided main.cpp
Source Code Files
You are given “skeleton” code files with declarations that may be incomplete and without any implementation. Implement the code and ensure that all the tests in main.cpp pass successfully.
GroceryItem.h: This is to be completed
GroceryInventory.h: This is to be completed
main.cpp: The main function tests the output of your functions. This is already complete and should not change. You may wish to add additional tests. During grading your main.cpp file will be replaced with the one you were provided with.
//GroceryItem.h
//GroceryInventory.h
Explanation / Answer
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
#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
Product.h
#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;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.