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

**********************************Grocery Store********************************

ID: 3541329 • Letter: #

Question

**********************************Grocery Store********************************

Our store, Cougar Mart, maintains its inventory in a text file. Not being very tech savvy, the owner of Cougar Mart needs you to build a C program to read from and maintain the list  of currently stocked items. The program will also simulate a simple shopping cart in which customers may add items.

### File Format ###

Your program should read a file called "grocery_list.txt" (see included file for an example file). Each line of the file contains one grocery item. Each item contains three pieces of information: the name of the item, the item's quantity, and the item's cost. Each piece of information is separated by a colon (:). For example, "cheese:5:6" means that the item's name is "cheese", that there are 5 pieces of cheese in stock, and that each piece of cheese costs $6. Recall that one possible method for breaking up the information is to use .find() and .substr() (see http://www.cplusplus.com for more information). Alternatively, you can just manually track the location of the colons and make substrings as needed using a FOR loop


The following is just how the output of the program suppose to be





****************************************************************************

### Sample Output ###

### Below is the desired output of your script ###

Welcome to Cougar Mart!

How much money do you have?: a

Invalid input. Please try again

How much money do you have?: 4

Here's what we have in stock today:

*** apples $1 (qty: 5)

*** milk $2 (qty: 3)

*** bread $1 (qty: 3)

*** candy $1 (qty: 10)

*** cheese $6 (qty: 5)

*** oranges $2 (qty: 4)

*** cherries $2 (qty: 3)

You have $4

What would you like to put into your cart: oranges

Would you like to continue shopping? (y/n): y

Here's what we have in stock today:

*** apples $1 (qty: 5)

*** milk $2 (qty: 3)

*** bread $1 (qty: 3)

*** candy $1 (qty: 10)

*** cheese $6 (qty: 5)

*** oranges $2 (qty: 3)

*** cherries $2 (qty: 3)

You have $2

What would you like to put into your cart: apples

Would you like to continue shopping? (y/n): y

Here's what we have in stock today:

*** apples $1 (qty: 4)

*** milk $2 (qty: 3)

*** bread $1 (qty: 3)

*** candy $1 (qty: 10)

*** cheese $6 (qty: 5)

*** oranges $2 (qty: 3)

*** cherries $2 (qty: 3)

You have $1

What would you like to put into your cart: milk

Sorry, you don't have enough money!

Would you like to continue shopping? (y/n): y

Here's what we have in stock today:

*** apples $1 (qty: 4)

*** milk $2 (qty: 3)

*** bread $1 (qty: 3)

*** candy $1 (qty: 10)

*** cheese $6 (qty: 5)

*** oranges $2 (qty: 3)

*** cherries $2 (qty: 3)

You have $1

What would you like to put into your cart: butter

Sorry, we're all out of butter today.

Would you like to continue shopping? (y/n): y

Here's what we have in stock today:

*** apples $1 (qty: 4)

*** milk $2 (qty: 3)

*** bread $1 (qty: 3)

*** candy $1 (qty: 10)

*** cheese $6 (qty: 5)

*** oranges $2 (qty: 3)

*** cherries $2 (qty: 3)

You have $1

What would you like to put into your cart: candy

Would you like to continue shopping? (y/n): n

Here's what you've added to your shopping cart:

* oranges

* apples

* candy

You spent $4 and have $0 left over.

Thank you, come again!

****************************************************************************



### Program Flow

Your script should operate in the following manner:

· Open the file "grocery_list.txt".

· Read the names and quantities of items from the text file.

· Prompt the user for the amount of money that they currently have.

· Until the shopper decides to exit, do the following:

o Display a menu with the currently stocked items, their cost, and their quantities.

o Ask the shopper what they'd like to add to their cart.

o If the item exists and there is sufficient quantity, add the item to their cart.

o If the item does not exist, let the user that the item is not in stock.

o If the item costs more than what the users has, let the user know that they have

insufficient funds to make the purchase.

o Update the list of items currently in stock to reflect the new quantities.

· After the user has decided to exit, display the current contents of the shopping cart along with

how much was spent and how much money the user has left over.

· Note that the program should also write back to "grocery_list.txt" with the updated quantity of

items.



NOTE:

I need the full program that contains (Header.h + function.ccp + main.ccp + grocery_list.txt)

Explanation / Answer

/*

the file grocery_list.txt must be in the following format

eg. data of file

Apple :2 :100

Boy :6 :200

Cat :5: 30



here first element show the name of item second one available quantity of item

and third item show the price of single item

*/


#include<iostream>

#include<map>

#include<string>

#include<fstream>

#include<vector>

#include <stdlib.h>

using namespace std;


//use to store item name and their available quantity

map<string,int> itemList;


//use to store item name and their price for one quantity

map<string,float> priceList;


struct cart

{ int userAmount;

map<string,int> itemList;

map<string,float> amount;

};

//co0ntain user cart info

struct cart cartItem;


//this function will read all data from grocery_list.txt file

int readDataFromFile()

{

ifstream myfile;

myfile.open("grocery_list.txt");

if(!myfile.is_open())

{

cout<<"Error in file opening grocery_list.txt";

getchar();

return 0;

}

while(myfile.good())

{

string wholeLine;

myfile>>wholeLine;

char *str[3];

str[0]=new char[wholeLine.length()];

str[1]=new char[wholeLine.length()];

str[2]=new char[wholeLine.length()];

int j=0,k=0;

for(int i=0;i<wholeLine.length();i++)

{

if(wholeLine[i]==':')

{

str[j][k]='';

j++;k=0;

continue;

}

str[j][k]=wholeLine[i];

k++;

}

str[j][k]='';

if(j==2)

{

string item=str[0];

int quantity = atoi(str[1]);

float price = atoi(str[2]);

itemList[item]=quantity;

priceList[item]=price;

}

}

myfile.close();

}


//this function will show all available item

void showItemAvailable()

{

map<string,int>::iterator it;

map<string,float>::iterator it2;

cout<<"Here's what we have in stock today"<<endl;

for(it=itemList.begin(),it2=priceList.begin();it!=itemList.end();it++,it2++)

{

cout<<endl<<"***"<<it->first<<" "<<"$"<<it2->second<<"(qty:"<<it->second<<")"<<endl;

}

cout<<"You have $"<<cartItem.userAmount<<endl;

}


//shows user cart info when he select quit

void showuserCartInfo()

{

cout<<"Here's what you've added to your shopping cart:"<<endl;

map<string,int>::iterator itforitem=cartItem.itemList.begin();

map<string,float>::iterator itforprice=cartItem.amount.begin();

float totalSpend=0;

for(;itforitem!=cartItem.itemList.end();itforitem++,itforprice++)

{

cout<<itforitem->first<<" $"<<itforprice->second<<" (qty"<<itforitem->second<<")"<<endl;

totalSpend=totalSpend+(itforprice->second)*(itforitem->second);

}

cout<<endl<<"You spent $"<<totalSpend<<"and have $"<<cartItem.userAmount<<" left over."<<endl;

cout<<"Thank you, come again!"<<endl;

cout<<"****************************************************************************";

}


//this function wil update file after user shopping

void updateFileInfo()

{

ofstream outfile;

outfile.open("grocery_list.txt");

map<string,int>::iterator it;

map<string,float>::iterator it2;

for(it=itemList.begin(),it2=priceList.begin();it!=itemList.end();it++,it2++)

{

outfile<<it->first<<":"<<it->second<<":"<<it2->second<<endl;

}

outfile.close();

}


void readUserAmount()

{

string temp;

cout<<"How much money do you have?:";

cin>>temp;

for(int i=0;i<temp.length();i++)

{

if(!isdigit(temp[i]))

{

cout<<"Invalid input. Please try again"<<endl;

return readUserAmount();

}

}

char *str;

str = new char[temp.length()];

for(int i=0;i<temp.length();i++)

{

str[i]=temp[i];

}

cartItem.userAmount=atoi(str);

delete str;

}


//its check wheather item is out of stock or other condition

//return 1 if user can purchase it or false when he cannot

int validateUserItem(string userItem)

{

int isItemPresent=itemList.count(userItem);

if(isItemPresent==0)

{

cout<<"Sorry, we're all out of "<<userItem<<"today."<<endl;

return 0;

}

if(cartItem.userAmount<priceList[userItem])

{

cout<<"Sorry, you don't have enough money!"<<endl;

return 0;

}

//update info now user can purchase it

if(cartItem.itemList.count(userItem))

{

cartItem.itemList[userItem]+=1;

cartItem.amount[userItem]+=priceList[userItem];

cartItem.userAmount-=priceList[userItem];

}

//when purchasing item first time

else

{

cartItem.itemList[userItem]=1;

cartItem.amount[userItem]=priceList[userItem];

cartItem.userAmount-=priceList[userItem];

}

//update itemlist of shop

itemList[userItem]-=1;

if(itemList[userItem]==0)

{

map<string,int>::iterator itforitem;

map<string,float>::iterator itforprice;

itforitem=itemList.find(userItem);

itemList.erase(userItem);

itforprice=priceList.find(userItem);

priceList.erase(itforprice);

}

}

int main()

{


readDataFromFile();

//ask amount from user

cout<<"Welcome to Cougar Mart!"<<endl;

readUserAmount();

//menu

while(1)

{

char choice;string userItem;

showItemAvailable();

//ask user choice

cout<<"What would you like to put into your cart:";

cin>>userItem;

validateUserItem(userItem);

do

{

cout<<"Would you like to continue shopping? (y/n):";

cin>>choice;

}while(!(choice!='y'||choice!='Y'||choice!='n'||choice!='N'));

if(choice=='n'||choice=='N')

{

showuserCartInfo();

updateFileInfo();

getchar();

break;

}

}

}