You will create a GroceryItem class that contains the following data: item_name,
ID: 661138 • Letter: Y
Question
You will create a GroceryItem class that contains the following data:
item_name, item_price, quantity_on_hand, and qty_purchased. The GeroceryItem class will contan the following methods:
GroceryItem(): The default constructor.
set_ item_name(): Assign a value to the data member item_name.
set_item_price(): Assign a value to the data member item_price.
set_qty_on_hand(): Assign a value to the data member quantity_on_hand.
set_qty_purchased(): sets qty_purchased to zero before a custormer begins shopping.
get_ item_name(): Return the value of the data member item_name.
get_item_price(): Return the value of the data member item_price.
get_qty_on_hand(): Return the value of the data member quantity_on_hand.
get_qty_purchased(): Return the value of the data member qty_purchased.
Write a C++ program that creates an arry of 10 GroceryItem objectd. Read the data from the file Grocery.dat and assign values to the 10 GroceryItem objectd. Next display these items to allow a customer to select grocery items from the list and indicate the quantity of that item he or she would like to order. When the customer finishes shopping, the program should display the items and quantities he or she has chosen, then calculate the total bill. The program should also simulate delivering the order with acout statement that announces the customer
Explanation / Answer
Program:
#include<iostream>
#include<string>
#include<fstream>
const int NUM_ITEMS = 10;
using namespace std;
class GroceryItem
{
public:
GroceryItem();//default constructor
void set_item_name(string);
void set_item_price(double);
void set_qty_on_hand(int);
void set_qty_purchased(int);
string get_item_name();
double get_item_price();
int get_qty_on_hand();
int get_qty_purchased();
private:
string item_name;
double item_price;
int qty_on_hand;
int qty_purchased;
};
GroceryItem::GroceryItem()
{
item_name = " ";
item_price = 0;
qty_on_hand = 0;
qty_purchased = 0;
}
void GroceryItem::set_item_name(string name)
{
item_name = name;
}
void GroceryItem::set_item_price(double price)
{
if( price > 0)
item_price = price;
if( price <= 0)
{
item_price = 0;
cout << "Price cannot be negative. Price has been
set to zero" << endl;
}
}
void GroceryItem::set_qty_on_hand(int onhand)
{
if( onhand > 0)
qty_on_hand = onhand;
if( onhand <=0 )
{
>
cout << "Quantity on hand cannot be negative.
Quantity on hand has been set to zero" << endl;
}
}
void GroceryItem::set_qty_purchased(int purchased)
{
if( purchased > 0)
qty_purchased = purchased;
if( purchased <= 0 )
{
purchased = 0;
cout << "Quantity purchased cannot be negative.
Quantity purchased has been set to zero" << endl;
}
}
string GroceryItem::get_item_name()
{
return item_name;
}
double GroceryItem::get_item_price()
{
return item_price;
}
int GroceryItem::get_qty_on_hand()
{
return qty_on_hand;
}
int GroceryItem::get_qty_purchased()
{
return qty_purchased;
}
int main()
{
GroceryItem grocery_items[NUM_ITEMS];
ifstream inFile;
inFile.open("Grocery.txt");
int k=0,qty;
string tempy;
double temp,temp1;
double price=0,amt=0,orqty=0,tot=0;
while( k < NUM_ITEMS)
{
inFile >> tempy>>temp >> temp1;
grocery_items[k].set_item_name(tempy);
grocery_items[k].set_item_price(temp);
grocery_items[k].set_qty_on_hand(temp1);
k++;
}
inFile.close();
cout<<"The grocery items along with their respective
price, and qty on hand are: "<<endl;
for(k = 0; k < NUM_ITEMS; k++)
{
cout<<grocery_items[k].get_item_name()<<"
"<<grocery_items[k].get_item_price()<<"
"<<grocery_items[k].get_qty_on_hand()<<endl;
cout<<"Enter order quantity:";
cin>>qty;
grocery_items[k].set_qty_purchased(qty);
}
cout<<" Order details: ";
for(k = 0; k < NUM_ITEMS; k++)
{
price=grocery_items[k].get_item_price();
orqty=grocery_items[k].get_qty_purchased();
amt=price*orqty;
cout<<grocery_items[k].get_item_name()<<"
"<<grocery_items[k].get_item_price()<<"
"<<orqty<<" "<< amt<<endl;
tot=tot+amt;
price=0;
orqty=0;
amt=0;
}
cout<<"The total orderamt:"<<tot<<endl;
cout<<" The ordered product delivered at the end of
day.."<<endl;
system("pause");
return 0;
}
Result:
The grocery items along with their respective price, and qty on hand are:
RaisanBran 3.49 300
Enter order quantity:2
Milk 1.49 200
Enter order quantity:2
WhiteBread 2.49 50
Enter order quantity:2
Butter 2.49 100
Enter order quantity:2
GrapeJelly 1.09 50
Enter order quantity:2
PeanutButter 2.49 45
Enter order quantity:1
TomatoSoup 0.49 200
Enter order quantity:1
CherryYogurt 0.69 250
Enter order quantity:1
VanillaYogurt 0.69 200
Enter order quantity:1
RyeBread 1.49 55
Enter order quantity:2
Order details:
RaisanBran 3.49 2 6.98
Milk 1.49 2 2.98
WhiteBread 2.49 2 4.98
Butter 2.49 2 4.98
GrapeJelly 1.09 2 2.18
PeanutButter 2.49 1 2.49
TomatoSoup 0.49 1 0.49
CherryYogurt 0.69 1 0.69
VanillaYogurt 0.69 1 0.69
RyeBread 1.49 2 2.98
The total order amt: 29.44
The ordered product delivered at the end of day..
Press any key to continue . . .
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.