Write a program in c++ Your script should operate in the following manner: ? Ope
ID: 3541322 • Letter: W
Question
Write a program in c++
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.
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
remember txt file must not contain spaces in the middle of a entry.
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;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.