Notes for week 5

For this week's links click here.

The Application

Last week we finished creating the classes for GroceryList and GroceryItem, now we have to create an application. Once again our application will have a run function that is called from the main function. This application is more complex because it requires that the program ask the user to choose an operation, then the program will actually carry out the operation. This will require creating a menu.

Next the functions of the application should be listed. There should be one for each operation the user can do. In this case the functions are:

In this problem, Read and Save will be automatically called at the beginning and end of run respectively. These functions would actually be calls to the corresponding GroceryList functions so are not created as separate functions. The run function does take on a new look:
void MyApplication::run()
{
	MyMenu menu;
	ifstream in("grocery.dat");
	if (!in)
		throw "File of groceries not found";
	groceries.read(in);
	in.close();
	int choice;
	do {
		  choice = menu.makeChoice();
		  switch (choice) {
			case 0:  addItem(); break;
			case 1:	computeCost(); break;
			case 2:  printList(); break;
			}
	}while (choice != 3);
	
	ofstream out("grocery.dat");
	if(!out)
		throw "File of groceries not found";
	groceries.write(out);
	out.close();
}
Most of the code is fairly clear except for the switch statement - see text for explanation. Notice that the choice 3 is not a case of the switch, it is quit. When the user chooses quite the loop terminates. For more details on this code click here.

ContainersWith More Than One Type of Object

If you have a container with more than one type of object such as the various types of Employee in last weeks problem, you must use an array of counted pointers to Employee.

	typedef RcPointer < Employee > EmployeePtr;
	Array < EmployeePtr > EmployeeArray;
When reading a file use the techniques we discussed at that time to read objects. This requires that you save the type of each object as an enumerated type when you write to the file, and retrieve this when you read. I recommend again that you save the number in the array before the array.

Introduction to Windows Applications

Our console version of the grocery list program took a traditional terminal-oriented approach to implementing a menu. Today most programs have a user-friendly type of windows interface. We are going to rebuild our program with a simple windows interface. This will be the first step in creating a Windows application, so it will not have all of the bells and whistles of a typical Windows program.

Designing a Windows Program

Process of creating a Windows program is much the same as that of creating a Console program except there are more opertunities for the user to be involved at an early stage in the program development. Once you have talked to the user about the requirements of the program, you should be able to list the processes that the program will perform. Once you have this list of operations group related operations so you may construct a menu bar with related operations in the same menu. Next decide on the general appearance of the final program (this is just a first design). Now you are ready to create a shell for the program.

Create a Single Document Interface Program

Most windows programs are created as Single Document Interface (SDI) programs. These are programs that allow you to inspect all aspects of a single document, but do not allow more than one document to be open at a time. Builder is an example of an SDI program since it only allows one project (document) to be open at a given time. This does not mean there cannot be more than one file open as long as they are all part of the same document.

Menus

After you have created your project edit the menu bar to include the menus you have decided on. You should at this point either draw a menu bar for the user to see or create a shell with menu bar. Once you have created the menu bar in a form you believe will work, you can either show it to the user to see if it meets their needs. This should happen in a short time so the user can be involved in the early design of the program. At this point the menus will not have handlers, so it should just be used to see if you are meeting the needs of the user.

Menu Handlers

Once you have shown the user these menus, you should design the menu handlers. Each menu entry except those built by your programming system should have its handler sketched in a scenerio . At this point you will discover any dialog boxes you need to construct. Create the dialog boxes . Next create the handlers for the menus . At this point you can again show your user what you propose to do. Create as much of the interface action as you can without actually doing anything. At this point you may have to show the interface to an administrator or board to get approval for funding.

Program

Once the user is happy with the interface, you can begin to design the actual program in much the way you did for Console programs. The largest challenge will be to coordinate changes in data in various windows involved in a project. In this project we wanted to have a list of grocery items. Our menu items are all designed to operation on this list or display properties of the list.. To see the details of the grocery list problem click here .