Create the text data file (10 pts): The input for this program has two sources:
ID: 3684148 • Letter: C
Question
Create the text data file (10 pts): The input for this program has two sources: the inventory information is input from a text file, and the customer transactions are input from the keyboard. The information about each product carried by the store is listed on a single line in the inventory file “Input.txt", in the following format: 11012 gallon milk 1.99 N 11014 butter 2.59 N 11110 pie shells 0.99 N 20115 laundry soap 3.60 T 30005 homestyle bread 0.99 N 40000 Organic Eggs 2.99 N 40020 Organic Chicken Broth 3.99 N where is a five-digit positive (nonzero) integer, is a string of at most 12 characters with embedded blanks, is a real number, and is a character ('T' if the product is taxable; 'N' if it is not taxable). Create a text file with the above data. Processing: The program first reads in all of the inventory information from file "Input.txt" Save all records into the data structure array which is declared with the pointer variable. (10 pts) Your main function keeps working until the user enters the numeric value 0. The program prompts the user to begin inputting the order for the customer in the following format: Where is as described above, and is an integer in the range 1 to 100, indicating the quantity of this product desired. A zero input for indicates the end of the customer's order. (10 pts) Check the entered product is in the data structure. If exists, the program can process the transaction. Otherwise, write an error message on both the screen and receipt, ignore that product number, and continue with the next product item. You need to define the following functions and you need to decide the parameter list. (20 pts) void readDataFromFile(); //read data from the text file and then save them into the data structure. (10 pts) void printReceit(); // Receipt is printed to the output file. (10 pts) void displayResult(); // display result on to screen. (20 pts) bool validateItem(); // check if the enter item is in the inventory or not. The receipt for each customer is written to the output file. The receipts should be nicely formatted, with the product description (not the product number), number of items, item price, and total price for each product printed on a single line. (10 pts) At the end of each customer's order, the subtotal for all items, amount of tax on taxable items, and total bill should be printed, clearly labeled. (The tax rate is 7.5% for taxable items.) The program outputs should be written to a text file called "Output.txt" as well as displaying to the screen. Sample Results: From File "Input.txt": 11012 gallon milk 1.99 N 11014 butter 2.59 N 11110 pie shells 0.99 N 20115 laundry soap 3.60 T 30005 homestyle bread 0.99 N 40000 Organic Eggs 2.99 N 40020 Organic Chicken Broth 3.99 N From keyboard: 11110 2 40012 3 20115 1 0 To "Output.txt" file and to screen: pie-shells 2 0.99 1.98 *** item 40012 not in inventory *** laundry-soap 1 3.60 3.60 TX Subtotal 5.58 Tax 0.27 Total 5.85
Explanation / Answer
Can help you with this:
#include "stdafx.h"
#include "inventory.h"
#include <iostream>
#include <istream>
#include <fstream>
int main(int argc, char* argv[])
{
filebuf fbInventory;
fbInventory.open ("invent.dat",ios::in);
filebuf fbReceipt;
fbReceipt.open("receipt.dat", ios::out);
istream inv(&fbInventory);
ostream rec(&fbReceipt);
vector<CInventory> v;
CInventory item;
while(!inv.eof())
{
inv >> item;
if (item.get_description() != "") {
v.push_back(item);
}
}
for (int i = 0; i < v.size(); i++)
{
cout << v[i] << endl;
}
rec << CInventory::get_heading();
cout << CInventory::get_heading();
char input;
long lInventoryNo = 0;
int iQuantity = 0;
double dblTax = 0.0;
double dblSum = 0.0;
string strOutput = "";
do
{
input = 0;
lInventoryNo = 0;
iQuantity = 0;
cout << "Enter Inventory No plus Quantity separated by space > ";
cin >> lInventoryNo >> iQuantity;
if (iQuantity != 0) {
input = 'y';
strOutput = item.find_item(v, lInventoryNo, iQuantity, dblTax, dblSum);
cout << strOutput << endl;
rec << strOutput << endl;
}
else
{
rec << CInventory::get_footer(dblSum, dblTax);
cout << CInventory::get_footer(dblSum, dblTax);
cout << endl << "Help another customer > ";
cin >> input;
if (tolower(input == 'y')) {
rec << CInventory::get_heading();
cout << CInventory::get_heading();
dblSum = 0.0;
dblTax = 0.0;
}
}
cin.ignore();
}while(tolower(input) == 'y');
system("pause");
return 0;
}
inventoy.h
include <iostream>
#include <iomanip>
#include <string>
#include <istream>
#include <fstream>
#include <vector>
#include <ctime>
#if _MSC_VER > 1000
#pragma once
#endif
using namespace std;
class CInventory
{
private:
static int iCustomerCounter;
static const char *szError[];
static const double dblTaxes;
long lInventory;
string strDescription;
double dblPrice;
char cTax;
public:
enum error_msg {NOT_FOUND, IS_DUPLICATE, E_QUANTITY};
const long& get_inventory() const {return lInventory;}
const string& get_description() const {return strDescription;}
const double& get_price() const {return dblPrice;};
const char& get_tax() const {return cTax;}
CInventory::CInventory() : lInventory(10000), strDescription(""), dblPrice(0.0), cTax('N'){}
CInventory(const CInventory &inv) {*this = inv;}
virtual CInventory::~CInventory(){lInventory = 0; dblPrice = 0.0; cTax = 'N'; strDescription = "";}
void operator=(const CInventory &inv)
{
lInventory = inv.get_inventory();
strDescription = inv.get_description();
dblPrice = inv.get_price();
cTax = inv.get_tax();
}
bool operator==(const CInventory &item)
{
return item.get_tax() == cTax &&
item.get_price() == dblPrice &&
item.get_description() == strDescription &&
item.get_inventory() == lInventory;
}
friend bool is_inventory(vector<CInventory>& inv, const int&iNo)
{
for (int i = 0; i < inv.size(); i++)
{
if (inv[i].lInventory == iNo)
return true;
}
return false;
}
friend bool is_duplicate(vector<CInventory>& inv, const CInventory& invCheck)
{
for (int i = 0; i < inv.size(); i++) {
if (inv[i] == invCheck)
return true;
}
return false;
}
bool is_taxable()
{
return cTax == 'T';
}
friend istream& operator>>(istream& input, const CInventory &item)
{
operator>>(input, (CInventory &)item);
}
friend istream& operator>>(istream& input, CInventory &item)
{
if (!input.eof()) {
input >> item.lInventory >> item.strDescription >> item.dblPrice >> item.cTax;
}
return input;
}
static string get_heading()
{
string strReturn = CInventory::get_printdate();
strReturn += " ";
strReturn += "Customer ";
++CInventory::iCustomerCounter;
strReturn += CInventory::iCustomerCounter + '0';
strReturn += " =================================================== ";
return strReturn;
}
static string get_footer(double dblSum, double dblTaxes)
{
char szFooter[512];
sprintf(szFooter, " %s Sub Total: %10.2lf Taxes : %10.2lf %s Total : %10.2lf %s ",
"---------------------------------------------------",
dblSum,
dblTaxes,
"---------------------------------------------------",
dblSum + dblTaxes,
"===================================================");
string strRet = szFooter;
return strRet;
}
friend ostream& operator<<(ostream& o, CInventory &item)
{
o << setw(5) << item.get_inventory() << " " << setw(12) << item.get_description() << setw(8) << fixed << setprecision(2) << item.get_price() << " " << item.get_tax();
return o;
}
static string get_printdate()
{
struct tm *today;
char tmpbuf[128];
string strDate;
time_t ltime;
time(<ime);
today = localtime( <ime );
strftime( tmpbuf, sizeof(tmpbuf), "%b %d, %Y %I:%M", today );
strDate = tmpbuf;
return strDate;
}
static string get_item(CInventory &item, const int& iQuantity, double& dblPTax, double &dblSum)
{
string strReturn;
char szItemLine[256];
memset(szItemLine, 0, sizeof(szItemLine));
string strD = item.get_description();
double dblP = item.get_price();
string strTax = item.is_taxable() ? " TX" : " N";
sprintf(szItemLine, "%12.12s %3d @ %6.2lf %-2.2s %7.2lf",
strD.c_str(),
iQuantity,
dblP,
strTax.c_str(),
iQuantity * dblP);
dblSum+= iQuantity * item.get_price();
if (item.is_taxable()) {
dblPTax += dblTaxes * (iQuantity * item.get_price());
}
strReturn = szItemLine;
return strReturn;
}
static string get_error(int idx, long lInventory)
{
string strReturn;
char szErr[256];
if (idx >= NOT_FOUND || idx <= E_QUANTITY){
//sprintf(szErr, "*** item %ld %s ***", lInventory, idx == NOT_FOUND ? "not found!" : idx == IS_DUPLICATE ? "duplicate in Inventory!" : "wrong quantity!" );
sprintf(szErr, szError[idx], lInventory);
strReturn = szErr;
}
return strReturn;
}
static string find_item(vector<CInventory>& inv, const long& lInventoryNo, const int& iQuantity, double& dblPTax, double& dblPSum)
{
string strReturn;
bool bFound = false,
bDuplicate = false,
bQuantity = iQuantity > 0;
int iFound = 0;
if (!bQuantity) {
strReturn = get_error(E_QUANTITY, lInventoryNo);
}
for (int i = 0; i < inv.size(); i++) {
if (inv[i].get_inventory() == lInventoryNo) {
if (bFound) {
if (strReturn.length() > 0) {
strReturn += " ";
}
strReturn += get_error(IS_DUPLICATE, lInventoryNo);
bDuplicate = true;
}
else {
bFound = true;
iFound = i;
}
}
}
if (!bDuplicate && bQuantity && bFound) {
strReturn = get_item(inv[iFound], iQuantity, dblPTax, dblPSum);
}
if (!bFound)
{
strReturn = get_error(NOT_FOUND, lInventoryNo);
}
return strReturn;
}
};
inventory.cpp
#include "stdafx.h"
#include "Inventory.h"
const double CInventory::dblTaxes = 0.075;
int CInventory::iCustomerCounter = 0;
const char *CInventory::szError[] =
{
"%ld is not in inventory",
"%ld has duplicate inventory #",
"%ld QUANTITY less than 1"
};
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.