Ask C/C++ Expert


Home >> C/C++

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

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

#include

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;

}

C/C++, Programming

  • Category:- C/C++
  • Reference No.:- M9451599

Have any Question?


Related Questions in C/C++

Question 1find the minimum and maximum of a list of numbers

Question: 1. Find the Minimum and Maximum of a List of Numbers: 10 points File: find_min_max.cpp Write a program that reads some number of integers from the user and finds the minimum and maximum numbers in this list. Th ...

Software development fundamentals assignment 1 -details amp

Software Development Fundamentals Assignment 1 - Details & Problems - In this assignment, you are required to answer the short questions, identify error in the code, give output of the code and develop three C# Console P ...

What are the legal requirements with which websites must

What are the legal requirements with which websites must comply in order to meet the needs of persons with disabilities? Why is maximizing accessibility important to everyone?

There are several ways to calculate the pulse width of a

There are several ways to calculate the pulse width of a digital input signal. One method is to directly read the input pin and another method (more efficient) is to use a timer and pin change interrupt. Function startTi ...

Assignment word matchingwhats a six-letter word that has an

Assignment: Word Matching What's a six-letter word that has an e as its first, third, and fifth letter? Can you find an anagram of pine grave. Or how about a word that starts and ends with ant (other than ant itself, of ...

1 implement the binary search tree bst in c using the node

1. Implement the Binary Search Tree (BST) in C++, using the Node class template provided below. Please read the provided helper methods in class BST, especially for deleteValue(), make sure you get a fully understanding ...

Assign ment - genetic algorithmin this assignment you will

ASSIGN MENT - GENETIC ALGORITHM In this assignment, you will use your C programming skills to build a simple Genetic Algorithm. DESCRIPTION OF THE PROGRAM - CORE REQUIREMENTS - REQ1: Command-line arguments The user of yo ...

Project - space race part a console Project - Space Race Part A: Console Implementation

Project - Space Race Part A: Console Implementation INTRODUCTION This assignment aims to give you a real problem-solving experience, similar to what you might encounter in the workplace. You have been hired to complete a ...

Why do researcher drop the ewaste and where does it end

Why do researcher drop the ewaste and where does it end up?

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As