Program: GroceryStore

In this program you will implement two classes:

1)Item – to represent a single item that the store may have for sale

2)GroceryStore – to represent the store itself, including a record of its inventory

Item Class

Data Members:

  • An item for sale at the store consists of the following private data members: name, size (in whole number of ounces), price, and category (which is represented by a letter – P=Produce, D=Dairy, M=Meat, etc)

Methods:

  • The Item class should have an accessor method for each of its data members.
  • The item class should have one mutator method that allows a new price (passed as a parameter) to be set for the item
  • The item class should have a non-default constructor that takes values for each of the data members as parameters and sets the data members’ values accordingly

GroceryStore Class

Data Members:

  • The grocery store’s only data member is a vector of items (i.e. Item class – NOT just a string with the item name)

Methods:

  • AddItem – takes as parameters all information about a new item, creates that item, and adds it to the vector of items for the store
  • Print – draws a chart displaying a report of all items in the store including their names, price, size, and category
  • FindName – takes as parameter the name of an item and returns its position in the store inventory vector (or -1 if not found) – this function should then be called as a helper function from other functions listed here (and its code should not be reimplemented to search!)
  • SetPrice – takes as parameter the name of an item and its price and changes the price of that item in inventory to the new price
  • GetPrice – takes as parameter the name of an item and returns the price of that item (or -1 if it is not found)
  • GetSize – takes as parameter the name of an item and returns the size in ounces of that item (or -1 if not found)
  • GetItems – takes as parameter a category and returns a vector containing the names of all items of that category from the store inventory

Driver

Free Functions (i.e. NOT inside the classes, but do use those classes)

  • OpenFile – takes as parameter an ifstream object and reprompts the user until they enter a valid name – leaves the filestream open (you should steal this directly from your censoring program)
  • LoadInventory – takes as parameter a GroceryStore – reads from a file of store inventory and loads all items into the store inventory before closing out the file
  • ChangePrices – takes as parameter a GroceryStore - reads item names and prices from input and changes the prices of the corresponding items in store to the new prices.

For example, assume store contains the following items.

Name / Price / Size (in ounces) / Category
Avocado / 1.68 / 8 / P
Milk / 1.92 / 64 / D
Chicken / 4.48 / 64 / M
Broccoli / 1.92 / 16 / P
Yogurt / 0.96 / 16 / D
Spinach / 1.76 / 16 / P
Cornedbeef / 6.72 / 48 / M
Porkchops / 2.24 / 32 / M

Assume that the stream input contains the following data.

cornedbeef 7.99

yogurt .75

milk 1.25

broccoli .98

The call ChangePrices(store, input) will change the prices of cornedbeef, yogurt, milk, and broccoli to the corresponding new prices.

  • BargainItem - takes as parameter a GroceryStore and a particular category – Determines the biggest bargain item for that category. The unit price of an item is the price per ounce.

The unit price of avocado is 1.68 divided by 8, which equals 0.21, and the unit price of spinach is 1.76 divided by 16, which equals 0.11.

BargainItem returns the name of an item whose unit price is the lowest in the specified category. If there is more than one item with the lowest unit price, any one of these items may be returned. If there are no items in the category, BargainItem returns “none.”

For example, consider the items and prices list in the table in part (a). Using this table, the results of three calls to BargainItem are shown below.

Function call / Returned Value
BargainItem(store, ‘P’) / spinach
BargainItem(store, ‘M’) / chicken or porkchops
BargainItem(store, ‘B’) / none
  • Main – you may take the code directly from here – a result of the run is shown below the code:

int main()

{

GroceryStore g;

loadInventory(g);

g.print();

cout<endl<endl;

changePrices(g);

g.print();

cout<endl<endl;

cout<"Bargain Produce:"<BargainItem(g, 'P')<endl;

cout<"Bargain Meat:"<BargainItem(g, 'M')<endl;

cout<"Bargain Dariy:"<BargainItem(g, 'D')<endl;

cout<"Bargain Bakery:"<BargainItem(g, 'B')<endl;

return 0;

}