#include <iostream>
using namespace std;

// A class for food items put on a grocery list
class FOOD_ITEM
{
public:
      // constructor
	  FOOD_ITEM(char name[], unsigned int aPrice) 
      {
          strcpy_s(itemName, name);
          price = aPrice;
      };    

      // set the name of a food item
	  void setName(char name[]) 
      {
          strcpy_s(itemName, name);
      };   	

      // return the name of a food item
      char *getName() 
      {
          return itemName;
      };                      
	
      // set the price of a food item
	  void setPrice(unsigned int aPrice) 
      {
          price = aPrice;
      };   	

      // return the price of a food item
      unsigned int getPrice() 
      {
          return price;
      };      

      // print the name and price of a food item
      // use a tab to separate the name and the price.
      void printItemNamePrice() const 
      {
          cout << itemName << '\t' << price << endl;
      };                  

private:
      // the food item's name
	char itemName[80];  

      // the food item?s price
      unsigned int price;                                     
};


// A class for grocery lists
class GROCERY_LIST 
{
public:
   // A constructor to set numberOfItems to 0 and to create in the heap 
   // an array of 100 pointers to FOOD_ITEM objects. The pointer to the 
   // array is stored in pArray.
   GROCERY_LIST();

   // A destructor to remove all the heap memories occupied by a    
   // GROCERY_LIST object and all the FOOD_ITEM objects pointed to 
   // by a GROCERY_LIST object.
   ~GROCERY_LIST();
    
   // Add a FOOD_ITEM object to the next available slot (in the 
   // array of) a GROCERY_LIST object. The first item is stored in 
   // the first element of the array. The FOOD_ITEM object must be 
   // created first and then stored in a GROCERY_LIST object. The
   // function must first check whether there is enough space to
   // add the new food item. If not, return a simple error message:
   // ?Not enough space for this request.?
   void AddFoodItem(FOOD_ITEM * newItem);

   // Print out the names and prices of all FOOD_ITEM objects 
   // stored in a GROCERY_LIST object. Each item is printed on 
   // a new line.
   void PrintGroceryList();

   // Get the number of FOOD_ITEM objects stored in a 
   // GROCERY_LIST object.
   int GetnumberOfFoodItems() const;

private:
   // the number of FOOD_ITEM objects stored in the GROCERY_LIST 
   unsigned int numberOfFoodItems; 

   // a pointer pointing to the first element of the array created 
   // in the heap.
   FOOD_ITEM **pArray;      
}; 
     

int main()
{
    GROCERY_LIST *pGList = new GROCERY_LIST;

    pGList->AddFoodItem(new FOOD_ITEM("Rice", 10));
    pGList->AddFoodItem(new FOOD_ITEM("Beef", 20));
    pGList->AddFoodItem(new FOOD_ITEM("Cake", 30));

	cout << "There are " << pGList->GetnumberOfFoodItems() 
	   << " items on the grocery list:" << endl;
    pGList->PrintGroceryList();

    delete pGList;

    return 0;
}

	
GROCERY_LIST::GROCERY_LIST() 
{
	numberOfFoodItems = 0; 
	pArray = new FOOD_ITEM *[100];
}
   
GROCERY_LIST::~GROCERY_LIST() 
{
    for (int i=0; i<GetnumberOfFoodItems(); i++)
        delete pArray[i];
    delete [] pArray;
}
	 
void GROCERY_LIST::AddFoodItem(FOOD_ITEM *newItem) 
{
	if (numberOfFoodItems == 100)
		cout << "Error: Not enough space for this request!" << endl;
	else
	{
	    pArray[numberOfFoodItems]=newItem; 
	    numberOfFoodItems++;
	}
}
   
void GROCERY_LIST::PrintGroceryList()
{
    for (int i=0; i<GetnumberOfFoodItems(); i++)
		pArray[i]->printItemNamePrice();
}
   
int GROCERY_LIST::GetnumberOfFoodItems() const 
{
	return numberOfFoodItems;
}
   