Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Programming Assignment 1 You will write, run, and test a C++ program to simulate

ID: 3662179 • Letter: P

Question

Programming Assignment 1

You will write, run, and test a C++ program to simulate the operation of a grocery store checkout system. Your program should first build a data structure to contain information on all the products available in the store. Then the program should print the cash register receipt for each customer.

Input

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 "Invent.dat", in the following format:

<product number> escription> <price> <tax>

where is a five-digit positive (nonzero) integer, is a string of at most 12 characters with no embedded blanks, is a real number, and is a character ('T' if the product is taxable; 'N' if it is not taxable). The data in the inventory file is ordered from smallest to largest product number.

The customer transactions are input from the keyboard, in the following format:

<product number> <times>

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 forindicates the end of the customer's order. (The store will soon be using a scanner to input the product number, but for now it is typed in by the cashier.)

Output

The program outputs should be written to a text file called "Receipts.out."

The inventory information is echo printed to the output file.

The cash register 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. 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.)

Processing

The program first reads in all of the inventory information from file "Invent.in." echo printing the information to the output file.

The program then prompts the cashier to begin inputting the order for the first customer.

This customer's order is processed, and the receipt is printed to the output file.

After each customer's order is processed, the program should ask the cashier if another customer is to be processed. If the answer is 'Y', the program sets up to process the next customer; if the answer is 'N', the program terminates with a friendly message.

Error Checking

The following input errors are possible and should be handled by an exception class:

Duplicate in inventory file. Write an error message to the output file and skip the second entry.

not in inventory file. Write an error message on the receipt, ignore that product number, and continue with the next item.

not in the specified range. Write an error message to the output file, ignore that line, and continue with the next item.

Example

From File "Invent.in":

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-br 0.99 N

From keyboard (one customer):

11110 2

40012 3

20115 1

0

To "Receipts.out" file:

------------------------------------------------------

SEP 10, 1998 6:00 pm (* date and time optional *)

Customer 1

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

Data Structures

The inventory information can be stored in an array of product records. Assume that the maximum number of products is 50, for purposes of writing this program. You should specify this data structure as an ADT, as described in the chapter; define and implement a set of operations to encapsulate it. The ADT should be tested using a test driver.

Deliverables

Your design (either object-oriented or top-down)

A listing of the ADT

A listing of the test driver for the ADT

A listing of the test plan as input for the driver

A listing of the output from the test driver

A listing of your program

A listing of your test plan as input to the program

A listing of the output file

Explanation / Answer

#include <iostream>
#include <iomanip>
#include <string>
#include <istream>
#include <fstream>
#include <vector>
#include <ctime>

using namespace std;
class MyInventory
{
private:
static int myCustomerCounter; // counts the receipts
static const char *myError[]; // err_msg
static const double myTaxes; // i.e 7.5%

long myInventory;
string desc;
double price;
char tax;
public:
enum err_msg {NOT_FOUND, IS_DUPLICATE, E_QUANTITY};
// methods
const long& getIinventory() const {return myInventory;}
const string& getDescription() const {return desc;}
const double& getPrice() const {return price;};
const char& getTax() const {return tax;}
  
MyInventory::MyInventory() : myInventory(10000), desc(""), price(0.0), tax('N'){}
  
MyInventory(const MyInventory &inv) {*this = inv;}
virtual MyInventory::~MyInventory(){myInventory = 0; price = 0.0; tax = 'N'; desc = "";}


  
void operator=(const MyInventory &inv)
{
myInventory=inv.getIinventory();
desc=inv.getDescription();
price=inv.getPrice();
tax=inv.getTax();
}

  
bool operator==(const MyInventory &item)
{
return item.getTax()==tax&&
item.getPrice()==price&&
item.getDescription()==desc&&
item.getIinventory()==myInventory;
}

friend bool isInventory(vector<MyInventory>& inv, const int&iNo)
{
for (int i = 0; i < inv.size(); i++) {
if (inv[i].myInventory == iNo)
return true;
}
return false;
}

friend bool isDuplicate(vector<MyInventory>& inv, const MyInventory& invCheck)
{
for (int i = 0; i < inv.size(); i++) {
if (inv[i] == invCheck)
return true;
}
return false;
}

bool isTaxable()
{
return tax == 'T';
}

friend istream& operator>>(istream& input, const MyInventory &item)
{
operator>>(input, (MyInventory &)item);
}
friend istream& operator>>(istream& input, MyInventory &item)
{
if (!input.eof()) {
input >> item.myInventory >> item.desc >> item.price >> item.tax;
}
return input;
}

static string getHeading()
{
string returnStr= MyInventory::get_printdate();
returnStr+= " ";
returnStr+= "Customer ";
++MyInventory::myCustomerCounter;
returnStr+= MyInventory::myCustomerCounter + '0';
returnStr+= " ***************************** ";
return returnStr;
}
static string getFooter(double dblSum, double myTaxes)
{
char footer[512];
sprintf(footer, " %s Sub Total:%10.2lf Taxes:%10.2lf %s Total:%10.2lf %s ",
"***************************************************************",
dblSum,
myTaxes,
"***************************************************************",
dblSum + myTaxes,
"***************************************************************");
string strRet = footer;
return strRet;
}
friend ostream& operator<<(ostream& o, MyInventory &item)
{
o << setw(5) << item.getIinventory() << " " << setw(12) << item.getDescription() << setw(8) << fixed << setprecision(2) << item.getPrice() << " " << item.getTax();
return o;
}
  
static string getPrintdate()
{
struct tm *today;
char tmpbuf[128];
string strDate;
time_t longtime;
time(&longtime);
today= localtime( &longtime );
strftime(tmpbuf,sizeof(tmpbuf),"%b %d,%Y %I:%M",today );
strDate = tmpbuf;

return strDate;
}

static string getItem(MyInventory &item, const int& iQuant, double& dblPTax, double &dblSum)
{
string strReturn;
char ItemLine[256];
memset(ItemLine, 0, sizeof(ItemLine));
string strDesc = item.getDescription();
double dblPrice = item.getPrice();
string sTax = item.is_taxable() ? " TX" : " N";

sprintf(ItemLine, "%12.12s %3d @ %6.2lf %-2.5s %7.3lf",
strDesc.c_str(),
iQuant,
dblPrice,
sTax.c_str(),
iQuantity * dblPrice);
dblSum+= iQuant*item.getPrice();
if (item.is_taxable()) {
dblPTax += myTaxes * (iQuant*item.getPrice());
}
returnStr= ItemLine;
return strReturn;
}

static string getError(int idx, long myInventory)
{
string strReturn;
char Err[256];
if (idx >= NOT_FOUND || idx<=E_QUANTITY){
  
sprintf(Err, myError[idx], myInventory);
returnStr= Err;

}
return strReturn;
}


static string find_item(vector<MyInventory>& inv, const long& myInventoryNo, const int& iQuantity, double& dblPTax, double& dblPSum)
{
string strReturn;
bool bFound = false,
bDuplicate = false,
bQuantity = iQuantity > 0;
int iFound = 0;
if (!bQuantity) {
returnStr= get_error(E_QUANTITY, myInventoryNo);
}
for (int i = 0; i < inv.size(); i++) {
if (inv[i].getIinventory() == myInventoryNo) {
if (bFound) {
if (strReturn.length() > 0) {
returnStr+= " ";
}
returnStr+= get_error(IS_DUPLICATE, myInventoryNo);
bDuplicate = true;
}
else {
bFound = true;
iFound = i;
}
}
}
if (!bDuplicate && bQuantity && bFound) {
returnStr= get_item(inv[iFound], iQuantity, dblPTax, dblPSum);
}
if (!bFound) {
returnStr= get_error(NOT_FOUND, myInventoryNo);
}
return strReturn;
}
};