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.

Reading a String With Blanks

The standard operator istream& operator >> (istream&,string&); will read a string until it reaches white space. The STL string has a non class member function Stream& getline (Stream&, basic_string&, charT); associated with it that will read until the end of the line if T = '\n', the end of line marker. We could use this here, but later we will not have this function associated with the preferred Windows string class. So we will write a generic bit of c++ code to read a string. This will work in most compilers because it reads a variable of type char* and all string classes I know of allow for construction of a string from char*.

string GroceryItem::readName(istream &in)
{
	long int eoln; // integer used to determine if the string is empty
	char inval[255]; //The input buffer
	char *ptr; // pointer used to find the end of the string
	do
	{
		in.getline(inval,255); // primitive stream function reads the current line
		ptr = strchr(inval,'\0');// find the address of the end of the string
		eoln = ptr -inval; // if difference between beginning and end 0, empty
	} while (!eoln && !in.eof());// do until not empty string
	return string(inval);
}

Adding a New Grocery Item

Here again I used getline to read an entire line from the terminal. In the case of the name and id I created a string. In the case of the cost, I used the atof function to change the cost to a double.

void MyApplication::addItem()
{
	cout << "Enter the item name";
    	char inval[255];
   	 cin.getline(inval,255);
	string name(inval);
	cout << "Enter the items id number";   
    	cin.getline(inval,255);
	string id(inval);
	cout << "What is the cost per item?"
    	cin.getline(inval,255);
	string costString(inval);
	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.