Reference no: EM13161475
You will create a GroceryItem class that contains the following data: item_name, item_price, quantity_on_hand, and qty_purchased. The GroceryItemclass will contain the following methods:
- GroceryItem(): The default constructor.
- set_item_name(): Assigns a value to the data member item_name.
- set_item_price(): Assigns a value to the data member item_price.
- set_qty_on_hand(): Assigns a value to the data member quantity_on_hand.
- set_qty_purchased(): Sets qty_purchased to zero before a customer begins shopping.
- get_item_name(): Returns the value of the data member item_name.
- get_item_price(): Returns the value of the data member item_price.
- get_qty_on_hand(): Returns the value of the data member quantity_on_hand.
- get_qty_purchased(): Returns the value of the data member qty_purchased.
Write a C++ program that creates an array of 10 GroceryItem objects. Read the data from the file Grocery.dat and assign values to the 10 GroceryItem objects. 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 a cout statement that announces the customer's total bill and tells the customer that the order will be delivered by the end of the day.
this is grocery.dat
Grocery.dat File
Raisin Bran 3.49 300 Milk 1.49 200 White Bread 2.49 50 Butter 2.49 100 Grape Jelly 1.09 50 Peanut Butter 2.49 45 Tomato Soup .49 200 Cherry Yogurt .69 250 Vanilla Yogurt .69 200 Rye Bread 1.49 55
then do this
In part II, you will create all necessary constructors for the GroceryItem class by using default function arguments, if appropriate. In addition, you should change the implementation of the item_name to use dynamically allocated memory instead of an array of characters. Make sure that you write a destructor for this class, as you are now using dynamically allocated memory. You should also overload the comparison operators (>, <, = =, !=, >=, and <=) so that you can sort the customer's order alphabetically by item_name.In addition, you will need to overload the assignment operator (=).
Read the data from the file Grocery.dat and assign values to the 10 GroceryItem objects. 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, sorted by item_name. then calculate the total bill. The program should also simulate delivering the order with a cout statement that announces the customer's total bill and tells the customer "your order is on its way."
then do this
In Part III, you will create a second class named Customer that will keep track of each customer's name, address, and total bill. Write the methods for the Customerclass, including a method named pick_one().Pass aGroceryItemobject to the pick_one() method so that you can add its price to the Customerobject's total bill. TheGroceryItem class should grant friendship to the Customerclass. Use constants, constant methods, and inline functions where appropriate.
Simulate a customer shopping by picking items from the array of Grocery objects and then calculating a total bill.
When you finish, you should have the main program (i.e. the driver) and the class files as grocery.h, grocery.cpp, customer.h and customer.cpp.
I have the first two. I only need part 3.
#include <string>
using namespace std;
class GroceryItem{
string item_name;
float item_price;
int quantity_on_hand;
int qty_purchased;
int selected;
public:
GroceryItem(void){selected = 0;}
~GroceryItem(void) {}
void set_selected() { this->selected = 1; }
void set_item_name(string name) { this->item_name = name; }
void set_item_price(float price) { this->item_price = price; }
void set_qty_on_hand(int qty) { this->quantity_on_hand = qty; }
void set_qty_purchased(int purchased) {this->qty_purchased = purchased; }
int is_selected() { return selected; }
string get_item_name() { return item_name; }
float get_item_price() { return item_price; }
int get_qty_on_hand() { return quantity_on_hand; }
int get_qty_purchased() { return qty_purchased; }
};
#include <iostream>
#include <fstream>
using namespace std;
int main(){
GroceryItem items[10];
ifstream file ("Grocery.dat");
string line;
if(!file.is_open()) {
cerr << "Unable to open the file" << endl;
return -1;
}
int i = 0;
string item_name;
float item_price;
int quantity_on_hand;
int qty_purchased;
while(!file.eof()){
file >> item_name >> item_price >> quantity_on_hand >> qty_purchased;
items[i].set_item_name(item_name);
items[i].set_item_price(item_price);
items[i].set_qty_on_hand(quantity_on_hand);
items[i].set_qty_purchased(qty_purchased);
i++;
}
file.close();
cout << "Items available : quantity" << endl;
cout << "--------------------------------" << endl;
for(int i=0; i<10; i++)
cout << i << ". " << items[i].get_item_name() << " : " << items[i].get_qty_on_hand() << endl;
char c;
float bill = 0;
while(true) {
cout << "Buy items (Y/N): ";
cin >> c;
if(c != 'Y' && c!= 'y') break;
int id, quantity;
cout << "Enter item id: "; cin >> id;
cout << "Enter item quantity: "; cin >> quantity;
items[id].set_qty_purchased(quantity);
items[id].set_qty_on_hand(items[i].get_qty_on_hand() - quantity);
items[id].set_selected();
bill += items[id].get_item_price() * (float) quantity;
}
cout << "Selected items:" << endl;
cout << "---------------------------------" << endl;
for(int i=0; i<10; i++)
if(items[i].is_selected())
cout << i << ". " << items[i].get_item_name() << " : " << items[i].get_qty_purchased() << endl;
cout << "Total bill: " << bill << endl;
cout << "The order will be deliverd in one day." << endl;
return 0;
}