In C++ One of the things we rely on computers to do for us is to store informati
ID: 3833376 • Letter: I
Question
In C++
One of the things we rely on computers to do for us is to store information. We keep files, movies and music (all legally obtained of course...), and a host of data to help us keep organized.
You'll manage a grocery, hardware store, and chore list all in the same program. You'll use files to store those lists so that they're somewhat permanent, and vectors to read those lists in to display for the user.
Your list manager will minimally hit the following benchmarks:
Menus! Control the interface
Select a list to edit
Quit the program
Allow users to add or remove items
Functions!
Entering items to add to a list
Deleting items from a list
Menus
File Operations! Read and use files
grocery
hardware store
chores
Loops!
Add as many items as necessary
Delete as many items as necessary
Move between lists as often as my little heart desires
Vectors!
Read items from files into vectors
Add items to lists - who's length is unknown
Delete selected items from lists
Display lists in a way that makes sense to a user
Good User Design
Make sure I know what's going on as the user. That means good user prompting, control of the outputs to the screen, clearing where necessary, and keeping the user engaged as they edit those lists!
Explanation / Answer
Hi,
Please find below the answer-
Please save the below files before running them-
Store.cpp--
#include <iostream>
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <vector>
#include "product.h"
using namespace std;
Product invoice[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 << invoice[p].getPLU() << " " << invoice[p].getProductName() << " " << invoice[p].getType() << " " << invoice[p].getPrice() << " " << invoice[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 (invoice[p].getPLU() == input)
{
PLU = p;
break;
}
}
/*if (PLU == -1)
{
cout << "Please enter a valid PLU" << endl;
continue;
}*/
if (invoice[PLU].getType() == 1)// Determine whether product is sold by weight or units
{
cout << "Weight: ";
}
else
{
cout << "Quantity: ";
}
cin >> quantity;
total += quantity * invoice[PLU].getPrice();
invoice[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);
invoice[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-
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;
}