273-422 Object-Oriented Design Assignment # 3 ---------------------------------------------- Write a C++ program that maintains a list of names, addresses, and phone numbers. This list is to be stored in a file and this file is read when the program starts up and an in-memory version of the list is created. The name of the file should be "rolo.txt" by default but the user can supply an alternative filename as a command line argument to the program. After the program has read the list of names, etc. from the file, it should present the user with a "menu" of possible commands: a Add a new entry d Delete the current entry s Search for an entry by name q Quit The "current entry" in the in-memory list is the entry that was most recently referenced (added or found by a search). At program start-up, the current entry is undefined. If the user chooses 'a', the program should prompt for a name, address, and telephone number. Upon receipt of this information, a new entry is added to the in-memory list. If the user chooses 'd', the program should delete the current entry from the in-memory list. (If the current entry is undefined, the program should issue an error message.) After an entry is deleted, the current entry is undefined. If the user chooses 's', the program should prompt for a name to be searched for. It then should search through the in-memory list for an entry with a matching name and print the name, address and telephone number to the screen. (If no matching entry is found, a message to that effect should be printed to the screen.) If the user chooses 'q', the program should write the in-memory list to the file (to replace the previous contents of the file), and then exit. ----------------------------------------------------------------------- Implementation Notes: 1) You should use the external functions ReadRoloEntry() and WriteRoloEntry() for reading and writing the entries in the file. These functions follow the declarations: int ReadRoloEntry( FILE *fp, char **ptr_name, char **ptr_addr, char **ptr_phone ); int WriteRoloEntry( FILE *fp, char *name, char *addr, char *phone ); I will supply you with these functions in source code form and you should keep this source in a separate file from the one you are writing. Part of this assignment is to get practice in compiling and linking C++ programs that span several files. Do NOT use #include to add this file into your file - instead you should investigate your compiler's "Project" facilities. Since you are using the above functions to access the file, you don't really need to know the file format but it is as follows: The file contains an arbitrary number of entries, each entry being of the form: NAME: Cameron Hayne ADDR: 1801 McGill College Av, Suite 800 PHONE: 840-1234 where the name, address, and telephone number fields are indicated by the keywords "NAME", "ADDR", and "PHONE", and extend to the end of the line. 2) The in-memory list should be a linked list where each entry in the list is an object of class Entry: class Entry { char *name; char *addr; char *phone; Entry *next; }; Note that the fields in the class structure are pointers - not fixed length arrays. This allows arbitrary length data without a waste of memory space. You may modify the definition of the Entry class as long as you keep the 'name', 'addr' & 'phone' data members as pointers. In fact, the above class definition provides nothing more than would be provided by a C struct. I would expect you to add function members to the class to provide methods for manipulating the data. I would also expect you to decide on appropriate access restrictions for the data members and modify the class accordingly. You may change the implementation of the linked list mechanism as you wish - e.g. it is not necessary to have the pointer called 'next'. You may use some higher level linked list handling if you wish (e.g. a separate class which handles the linked list) as long as you implement it yourself - you are not allowed to use library routines written by others (e.g. the STL) for the linked list handling. Adding an entry to the in-memory list would involve the following steps: - allocate space for a new Entry object - for each of the three char * pointers in the structure, allocate sufficient memory for the given string and copy the string into this memory - insert the new object into the linked list Deleting an entry from the in-memory list would involve the following steps: - remove the object from the linked list - free the memory for each of the three char * pointers - free the memory for the object itself Searching for an entry by name in the in-memory list can be done by traversing the linked list, doing a string comparison (strcmp()) on the 'name' field for each entry. 3) You can assume that the number of names will be small enough that all records will fit in RAM. The overview of what you program should do is as follows: - read all of the records into RAM - manipulate the records according to the user's commands - write out all of the records to the data file (overwriting what was previously in the file) The file handling is therefore very simple: - open the file for reading, read everything, close the file - open the file for writing, write out everything, close the file. To get started, you can simply create an empty file - your program should handle this case properly.