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

Introduction In this assignment you will write a small C++ program. Your program

ID: 3681577 • Letter: I

Question

Introduction

In this assignment you will write a small C++ program. Your program should compile correctly and produce the specified output.

Please note that the computer program should comply with the commenting and program structure rules as has been done in class. For example, there should be a header for the whole program that gives the author’s name, class name, date, and description, and comments on each function. Please ask if you have any questions.

The Program

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.

.

.

.

Write a program to perform grocery check-out procedure for a simple store. Use a dynamic array of pointers to structures (Up to 100) to store this information. For each product we store the following information:

            - PLU code: Product Look Up Code. Unique for each product, stored as a string.

            - Product Name,

            - Product Sales Type: 0 = per unit, 1 = per pound

            - Price Per Pound or Price Per Unit,

            - Current Inventory Level.

           

Your program should:

On initialization, read inventory from products.txt (also on eLearning). Do not drive-direct this file; open it as simply “products.txt” and put it where it will be opened without having to specify a drive and folder. You will have to allocate a new structure and put its pointer in your array every time you read an item.

Display a menu with two items:

Check out

Close the store and exit

Do check-out functionality in a separate method/function.

Enable the user to purchase several products in each checkout.

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 ($).

User can enter 0 for PLU code to indicate the end of checkout.

If the total purchase exceeds $50, apply 5% discount to the total.

A list of items and their prices should be displayed along with the total due before and after discount

You should perform input validation (i.e. PLU should be all digits, no symbols or letters are valid as PLU, unit price, sales type or stock).

You should also make sure the user doesn’t try to buy more of something than you have on hand.

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 products.txt.

Guidelines

Define a constant SIZE to store the maximum number of products

Declare a global variable to be used as your 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. Allocate a new Product structure for each valid item read, and keep track of how many you have.

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 by rewriting the entire Products.txt file 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.

Deliverables

You will turn in two files:

A screen shot as a JPG showing your program running for checking out two customers for 3 items each before closing, with one of the customers buying over $50.00 total.

A C++ source code file.

The C++ source code file should:

Comply with all of the good programming practice

Implement the functions as described above

product.txt:

Explanation / Answer

main.cpp

#include<iostream>
#include<fstream>
#include <string>
#include <sstream>
#include <stdlib.h>
#include <ctype.h>
using namespace std;
const int SIZE=100;
int TotalUsed=0; //variable to keep track of total products available;
int ProductsBought=0;// keep track of where in receipts it is.
//Create structure for data
struct products{
   int plu;
   string name;
   int type;
   double price;
   double amount;
} items [SIZE];
struct receipts{
   int plu;
   string name;
   double price;
} receipt[SIZE];

bool readInventory(string filename){
   /*
   * Read in products from the specified text, then cut each product to add it to the products struc
   */
int local=0;
string line;
ifstream profile (filename.c_str());
if (profile.is_open())
{
    while ( getline (profile,line) )
    {
  
    int length=line.length();
   //seperate the PLU
   int place=line.find(" ");
   string plu=line.substr(0,place);
   //cut string, redefine line to the smaller string, cuts one past the space after plu
   line=line.substr(place+1,length);
   //get name, via finding the space after the name, and then cut
   int name_place=line.find(" ");
   string name=line.substr(0,name_place);
   //cut string
   line=line.substr(name_place+1,length);
   //type
   int type_place=line.find(" ");
   string type=line.substr(0,type_place);
   //cut string
   line=line.substr(type_place+1,length);
   //cost
   int cost_place=line.find(" ");
   string cost=line.substr(0,cost_place);
   //cut string
   line=line.substr(cost_place+1,length);
   //amount
   int amount_place=line.find(" ");
   string amount=line.substr(0,amount_place);
   //cut string
   line=line.substr(amount_place+1,length);
   //int place2=line.find(" ");
   if(atoi(plu.c_str()) && atof(cost.c_str()) && atof(amount.c_str())){
       items[local].plu= atoi(plu.c_str());
       items[local].name=name;
       items[local].type=atoi(type.c_str());
       items[local].price=atof(cost.c_str());
       items[local].amount= atof(amount.c_str());
   }
   local+=1;
   TotalUsed+=1;
   //cout<<local<<endl;
    }
    profile.close();
    return true;
}
return false;
}
double checkout(){
   /*
   * Take input of a PLU, then search for that PLU in the products data structure, once found do calculations
   * regarding amount and return the total purchase price to the main()
   */
   string string_plu;
   int plu;
   int location;
   int type;
   double amount;
   double sale=0;
   bool running=true;
   bool found;
   do{
       cout<<"PLU: ";
       cin>>string_plu;
       //search:
       found=false;
       if(atoi(string_plu.c_str())==0 && string_plu.find_first_of("0123456789")<=string_plu.length()){
           running=false;
           break;
       }
       if(atoi(string_plu.c_str())){
           plu=atoi(string_plu.c_str());
           for(int x=0; x<TotalUsed;x++){
               if(plu==items[x].plu){
                   location=x;
                   found=true;
                   break;
               }else{
                   found=false;
               }
           }
           if(found){
               type=items[location].type;
               cout<<"Item: "<<items[location].name<<endl;
               cout<<"Number Available: "<<items[location].amount<<endl;
               cout<<"Enter 0 if wrong item"<<endl;
               if(type==0){
                   cout<<"Enter Number of Units: ";
               }else{
                   cout<<"Enter Number of Pounds: ";
               }
               cin>>amount;
               if(amount!=0){
                   if((items[location].amount-amount)>=0){ //check to make sure not buying more then available;
                       sale+=items[location].price * amount;
                       items[location].amount-=amount;
                       //add to receipt
                       receipt[ProductsBought].plu=items[location].plu;
                       receipt[ProductsBought].name=items[location].name;
                       receipt[ProductsBought].price=items[location].price * amount;
                       ProductsBought+=1;
                   }else{
                       cout<<"We do not have that many items"<<endl;
                   }
               }
           }else{
               cout<<"PLU Not Found."<<endl;
           }
       }else{
           cout<<"That PLU is not valid"<<endl;
       }
   }while(running);
   return sale;
}
bool updateInventory(string filename){
   /*
   * Update the inventory file on closure of store, merely rewrite the file.
   * In theory for a real store this function should be called at the end of each checkout in order
   * to keep the store inventory synced across registers, however at that point a database should be used
   */
   ofstream profile;
    profile.open (filename.c_str());
       for(int y=0;y<TotalUsed;y++){
           profile <<items[y].plu<<" "<<items[y].name<<" "<<items[y].type<<" "<<items[y].price<<" "<<items[y].amount<<" ";
       }
       profile.close();
       return true;
}
int main(){
   //constants/definitions
   int total_sale_amount=0;
   double discount_min=50.00; //amount for 5% discount
   string file="Products.txt";
  
   //Inport products
   if(readInventory(file)){
       //log successs
   }else{
       cout<<"Products could not be imported"<<endl;
   }
  
   int option=0; //options for menu; 1= checkout, 2 is close  
   while(option!=2){
       cout<<"1. Checkout Enter 1"<<endl;
       cout<<"2.Close Store Enter 2"<<endl;
       cout<<"Choice: ";
       cin>>option;
       if(option==1){  
           double price=checkout();
           if(price>discount_min){
               price = price -(price*.05);
               cout << "You earned a 5% discount!"<<endl;
           }  
               cout<<"PlU    "<<"Name    "<<"Price "<<endl;
           for(int x=0;x<=ProductsBought; x++){
               cout <<receipt[x].plu<<" "<<receipt[x].name<<" "<<receipt[x].price<<endl;
           }
           cout<<"Total: "<<price<<endl;
           ProductsBought=0; //reset receipt;
           option=0;
       }else if(option==2){
           updateInventory(file);
           cout<<"Inventory Updated"<<endl;
       }
   }
}


Products.txt
0 0 0 0
4021 DELICIOUS_GDN_REG 1 0.89 44.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 104.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 105.5
0 0 0 0
4383 MINNEOLAS 1 0.79 7.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
0 0 0 0
0 0 0 0


sample output


1. Checkout Enter 1                                                                                                                                         
2.Close Store Enter 2                                                                                                                                       
Choice: 1                                                                                                                                                   
PLU: 0                                                                                                                                                      
PlU    Name    Price                                                                                                                                        
0 0                                                                                                                                                        
Total: 0                                                                                                                                                    
1. Checkout Enter 1                                                                                                                                         
2.Close Store Enter 2                                                                                                                                       
Choice: 1                                                                                                                                                   
PLU: 4021                                                                                                                                                   
Item: DELICIOUS_GDN_REG                                                                                                                                     
Number Available: 44.2                                                                                                                                      

Enter 0 if wrong item                                                                                                                                       
Enter Number of Pounds: 1                                                                                                                                   
PLU: 4021                                                                                                                                                   
Item: DELICIOUS_GDN_REG                                                                                                                                     
Number Available: 43.2                                                                                                                                      
Enter 0 if wrong item                                                                                                                                       
Enter Number of Pounds: 2                                                                                                                                   
PLU: 0                                                                                                                                                      
PlU    Name    Price                                                                                                                                        
4021 DELICIOUS_GDN_REG 0.89                                                                                                                                 
4021 DELICIOUS_GDN_REG 1.78                                                                                                                                 
0 0                                                                                                                                                        
Total: 2.67                                                                                                                                                 
1. Checkout Enter 1                                                                                                                                         
2.Close Store Enter 2                                                                                                                                       
Choice:                                                                                                                                                     

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote