Program for example 5

The Class GroceryList

 class GroceryList
{
public:
	GroceryList();
	double computeCost();
   	void addItem(const GroceryItem &newItem);
	void print(ostream &);
	void write(ostream &);
	void read(istream &);
   	void clearList();
private:
	Array< GroceryItem > _item;
};

The Read Function

 void GroceryList::read(istream &in)
{
	int size;
	in >>  size;
	for( int i = 0;i < size;i++)
	{
		GroceryItem oneItem;
		oneItem.read(in);
		addItem(oneItem);
	}
}

Iterator Examples

double GroceryList::computeCost()
{
	double total = 0.0;
	ArrayIterator < GroceryItem > next(_item);
	while (next)
	{
		total += next().computeCost();
		next++;
	}
	return total;
}

The second example is very similar to the last. To print the list you simply loop through the list and call on each item to print itself.

 void GroceryList::print(ostream & out)
{

	ArrayIterator < GroceryItem > next(_item);
	while (next)
	{
		next().print(out);
		out << endl;
		next++;
	}
}

The Class GroceryItem

class GroceryItem
{
public:
	GroceryItem();
	GroceryItem(string name,string id,double cost, int howMany);
	double computeCost();
	void print(ostream &);
	void write(ostream &);
	void read(istream &);
   	int operator ==(const GroceryItem &)const;
private:
	string _name;
	string _idNumber;
	double _unitCost;
	int _sizeOfOrder;
};

The Application Class

As indicated in the main notes for week 5 the application class has functions for adding an item, printing the list and computing the cost. These functions are found in the private section of the class definition, since they are not used except in the function run.
 class MyApplication
{
public:
	void run();
private: //These functions only need be known by MyApplicaton
	void addItem();
	void printList();
	void computeCost();
	// we will read the list at the start of run
	// and save at the end of run
	GroceryList groceries;
};

MyMenu Class

The class MyMenu is found in the application cpp file. This seems strange, but we don't want to export this class and it is used locally so there is not need to add identifies to the name space by putting its definition in a header. MyMenu is a decendent of the menu class whose sole purpose is to initialize the choices. The only function is its constructor which creates an array of choices and calls on setChoices to initialize the choices.

MyMenu::MyMenu()
{
	StringArray myChoices;
	myChoices.insertItem("Add an item");
	myChoices.insertItem("Compute Cost");
	myChoices.insertItem("Print list");
	myChoices.insertItem("Quit");
	setChoices(4,myChoices);
}

Some of the Details of MyApplication

Some of the details that you should notice in the implimentation of functions in MyApplication.

void MyApplication::addItem()
{
	cout << "Enter the item name";
	string name;
	name.read_line(cin);
	cout << "Enter the items id number";
	string id;
	id.read_line(cin);
	cout << "What is the cost per item?";
	string costString;
	costString.read_line(cin);
	double cost = atof(costString.c_str());
	cout << "How many do you need?";
	int howMany;
	cin>>howMany;
	GroceryItem newOne(name,id,cost,howMany);
	groceries.addItem(newOne);

}

void MyApplication::printList()
{   
	cout.precision(2); // two place accuracy on output
	cout.setf(ios::fixed);// fixed point notation
	groceries.print(cout);
}

void MyApplication::computeCost()
{
	cout.precision(2); // two place accuracy on output
	cout.setf(ios::fixed);// fixed point notation
	cout << "The cost of this list is $" << groceries.computeCost() << endl;
}

Back to Week 4

Back to Week 5

Click here to get a copy of the whole program.