![]() |
CSE 143 Spring 2001Homework 3Due: Electronic submission by 10 pm, Wednesday April 25. Paper receipt and written report due in quiz section on Thursday April 26. |
For this assignment, write a program that allows users to browse for CD information. The users can search for CDs by title or by performer.
As with all assignments, please read the instructions all the way through before beginning. Read them once again before you turn in your assignment, to be sure you've left nothing out.
The goal of this assignment is to gain experience with the following the following concepts:
This program maintains a database of information about CD's and allows the user to manipulate and examine that information. For each CD, the program maintains the following information:
The price is a decimal number (15.95, for example); all other data is represented by strings.
When the program starts, the database is initially empty. The program should repeatedly present the user with a menu of possible commands, then accept a command and carry it out. This continues until the user stops the program by entering a quit command.
The available commands are indicated by a letter, which can be either upper or lower case. The available commands are:
After the user enters a command letter, the command should be processed as described in the following sections. Once the command is processed, if the command was anything other than quit, the program should request a new command and continue.
When the user selects this option, the program should prompt for a file name and try to open that file. If it fails, it should report an error and return to the main menu.
Upon successfully opening the file, the program should read CD information from it and merge this information into the database (more about that below). The file is composed of blocks of 5 lines each, in the following order:
CD# Title Performer(s) Price Review
There might be any number of such blocks, including none, and there is no limit on how many blocks the file may contain. The end of the input is marked by an empty line when the next CD# would normally be, and the program should stop reading data at that point. You should assume that the input is clean. Specifically, the following fields will never be empty: CD#, Title, and Price (Performer and Review might be empty), and no field will span multiple lines. An example is given below.
All CD info from the file should be “merged” into the existing database. This means:
A CD is said to be in the database if the database already contains an entry with the same CD#. Different CDs may have the same title, same performer, same price, or same review, but the CD# is guaranteed to be unique.
-------------- START OF EXAMPLE FILE ------------------ 9 25789-2 Negotiations and Love Songs Paul Simon 12.99 This is a great CD. You really should buy it right away, and I mean now! CDP 0777 7 89282 2 5 The Best of Chick Corea Chick Corea 10.99 Good one. --------------- END OF EXAMPLE FILE -------------------
When the user selects this option, the program should ask for a word to search for in the song titles. It should then display a list of all CDs whose titles contain that word, in the following format:
The program should then ask the user for a number, which selects an item in the list (that is, “2” would indicate the Chick Corea CD). If the user enters “0” the program should return to the main menu. Otherwise, it should display the complete information for the selected CD, in whatever format you wish (but try to make it as usable as possible).
After displaying the CD information, the program should redisplay the same list found in the title search, and prompt the user for another number, continuing until the user enters 0 to return to the main menu.
This option is very similar to the previous (search by title), except that the search is done on performers: any CD whose performer contains the specified search word should be displayed. Everything else should work exactly the same as in a title search.
Exit the program. Make sure that you clean up all dynamically allocated memory before the program actually terminates.
The main technical goal of this assignment is to learn to use classes that have dynamically allocated data structures. Specifically, the database of CD information should automatically grow as needed. The database should be implemented as a class that contains a dynamic array of CD records (CD records being instances of another class). The database class should have at least the following functionality:
operator=
) that correctly copies the contents of
the assigned database.You will need to add additional operations to this class, of course, to implement the rest of the program.
Depending on how you write the client program, you might wind up not actually using the copy constructor or assignment operator in the client code. However, a well-designed C++ class should contain these member functions. You should include them in your class, and, if necessary, do some extra testing to verify that they work.
As usual, watch out for style and modularity issues. Also, continue to avoid using C strings when C++ strings will do the job, C standard I/O (e.g. printf), etc.
As is usually the case, you will find it very helpful to do some designing before you start writing detailed code. What is the high-level structure of the code? What sort of data structure operations are needed to process the various commands?
A few specific suggestions and reminders:
getline()
functions in the I/O libraries -
they may be useful for reading the input file. The getline()
member function of istream
only works on C-strings (arrays), but
there is a free-standing function getline(stream,cppstring)
that
reads a line from the specified input stream into a C++ string cppstring
.
(Substitute appropriate stream and string variable names for your code, of
course.)Think about interesting test cases for your program. A few suggestions (by no means a complete list!):
You know the drill by now: planning, implementation, testing, what you’ve learned.
Note: When describing the implementation, please don’t repeat stuff that is already documented in the code. Instead, describe what was problematic or interesting in your implementation, and how you handled these cases.
Also, as usual, there is no sample program for this assignment. In cases where the specification is not clear, you may need to make some reasonable assumptions. Feel free to discuss issues about the specifications on the course newsgroup, in sections, with your colleagues, etc.
When you've finished your program, turn in the source program (.cpp and .h files only) using this turnin form . Print out the receipt that appears, staple it and your written report together, and hand it in during quiz section.