CPS 100E, Fall 1996 Lab 5

Control, Classes, and CDs

(You may find it easier to read this lab using Netscape or another browser, the URL is http://www.cs.duke.edu/~ola/courses/cps100e/lab/lab5.html)

The goals of this lab include doing the specific tasks outlined below and understanding general concepts behind the tasks.


In this lab, we'll introduce you to some techniques for maintaining a database. You'll enhance an existing database program for CD's by adding search and delete capabilities to a database program.

Lab 5 table of contents

[ CD Database | The Program | Modifications To Program | Submit ]

For this lab you'll first write member functions to Search the database using different keys. The program already can read a database file from disk and print the database. From here you will write member functions to search for different keys and delete a CD from the database.

The structure of the program is similar to the usepix.cc program from a previous lab. The file usedb.cc stores the client program that manipulates CD databases. The class CDlist implements a CD database (by storing a list of CDs in a vector). Information about one CD is stored in a struct CDinfo. The relationship between these parts is diagrammed below with member functions and some data shown.

*

Databases

Creating and Maintaining Databases are important applications in the real world. For example, keeping records for all the students at Duke University is an arduous task if it all had to be done on paper. Databases give people the ability to look up individual records, add new records, or to categorize records so we know what students have in common (i.e. a database of students could list all the sophomores who were originally born in New Jersey.) In this lab, we will be adding functionality to a database that maintains a list of CDs for a CD store (by the way, CD superstore in Brightleaf was started by Duke graduates.)

CD Database

There are two files you will use to test your program. One stores information for about 20 CDs the other stores information for about 3,000 CDs. It's probably a good idea to test your program with the smaller file.

Information is stored in the file as shown below in the general format

   ID number : price : group name : CD title

For example:

  100122 : 14.98 : Happy Mondays : Yes, Please
  100126 : 14.98 : 10,000 Maniacs : Our Time In Eden
  100127 : 11.98 : Skid Row : B-Side Ourselves
  100160 : 14.98 : R.E.M. : Murmur
  100164 : 14.98 : R.E.M. : Reckoning
  102767 : 14.98 : Nine Inch Nails : Downward Spiral, The
  106149 : 14.98 : Snoop Doggy Dogg : Murder Was The Case
  115586 : 14.98 : Smashing Pumpkins : Siamese Dream

back to lab contents


Compiling/Running usedb

In this section of the lab you'll copy files and then compile and run the CD database program.

First change into your cps100e subdirectory (type pwd to verify where you are). Create a lab5 subdirectory by typing mkdir lab5 and change into this subdirectory (be sure to check that you're in the lab5 subdirectory.) Now copy the files for the lab (don't forget the . when copying).

cp ~ola/cps100e/lab5/* .

You should see the files listed below (these are links to the files in case you use Netscape, and for users outside of Duke).

Be sure you're in the lab5 subdirectory, and check to see that all files are there (type ls). Next type

         ln -s ~ola/cps100e/data data
To link (not copy) a data directory. There's no reason to copy the files (unless you want to), so you can access them by linking to the data directory. The data directory stores lots of data files (including Shakespeare's plays and CD database files.) Type ls -l data. You should see something that looks like similar to the following: lrwxr-xr-x 1 mloeb grad 26 Oct 2 12:06 data -> /afs/acpub/users8/ola/data

This shows that you've linked to ola's data directory.

Then, from an xterm window (at the prompt [1] ola@teer8% or similar) compile the first version of the program by typing: make usedb.

Note that you type make usedb since the function main is in the usedb.cc file.

This should compile several files and link them together with the library libtapestry.a. Now run the program by typing: usedb at the prompt. When you run the program, you can "read" a CD list file and print out the database. When you load a file, you will want to use data/smallcd.dat first so that you can test your program on a small database first. After you get the program working, then you can try it out on the larger file data/cd.dat Test out the Print option by printing the database you read in.

back to lab contents


Modifications of the Program:

In this part of the lab you'll implement changes to the CDlist class (in cdlist.cc) and to the usedb.cc program to take advantages of these changes.

You'll need to add functionality as shown in the table below.

Additional Functionality Change in usedb.cc Change in cdlist.h/cdlist.cc
search for title [no change] implement TitleSearch()
search for artistDoArtist()implement ArtistSearch()
delete a CDDoDelete()implement Delete()
delete a group's CDsDoDeleteGroup()???
add a new CD to database DoAdd() call Add() [no change]

  • Run emacs, either by clicking on the emacs icon in the menubar (if there is one) or typing emacs & at a prompt.

  • Load (use the file menu or C-x C-f) the file cdlist.cc.

  • Load the file usedb.cc

  • Change between the two buffers/files you've loaded by using the emacs menu or by typing C-x b and using the default entry in the mini-buffer or typing a different name (e.g., if you had more than two files loaded.)

Title Search

You will need to modify the CDlist::TitleSearch member function in cdlist.cc to implement the search. You should do this in two steps

  • Loop over all the CDs stored in the private vector myList (see, for example, CDlist::Print.)

  • To check if a CD has a title match you should use the string Contains member function to determine if the string the user has input matches the current entry. You could write, for example:
       if (myList[k].title.Contains(name)
    
    to check that name is contained in the k-th entry of myList.

    If the title is a match, you should add the CD to the parameter match which stores a list of all the matching CDs. This list is printed in usedb.cc.

  • (low priority) Once you get the above working, you should try to make the code case insensitive so that if you enters "love" a CD with the title "Ain't Love grand" will match. Note that the title Beloved Angel will match too since it contains love. For case-insensitivity you can use the string member functions Downcase() and Upcase(). For example: name.Downcase() returns a lowercase version (all caps are lower) of name. Note that this doesn't change name but returns a new string that is the same as name except it is all lowercase.

Artist Search

Implement the ArtistSearch() member function. This function is similar to the search for title that you implemented above but looks for artist matches. You'll need to implement the DoGroup function in usedb.cc too.

Delete

You will now implement the DoDelete function in usedb.cc and the Delete() member function in the CDList class.

To Delete a record from the Database, you will need to

  • Prompt the user for the ID number that is to be deleted (Remember to use getline and convert to an integer using atoi.)

  • In the CDlist::Delete member function you'll need to find where the CD to be deleted is (the index) and then shift elements after it one position to the left (this will keep the CDs in order if they are in order.)

  • To shift elements left you'll need to start at the index to be deleted and loop with a statement something like myList[k] = myList[k+1].

  • Be sure to change the count of how many CDs are stored after a deletion.

Delete Group

Finally we are going to delete every CD made by a particular group that you (the user enter). Here are some hints:

  • You will want to use the ArtistSearch member function in the CDList class -- this will give you a list of all the CDs by a given group.

  • To access all the CDs in the list returned by ArtistSearch you'll need to use the First, Next, IsDone, and Current functions from the CDList class. This means you'll write something like. for(match.First(); ! match.IsDone(); match.Next()) { //delete (from the proper CD list) }

Add Group

Implementing the DoAdd function in usedb.cc is NOT required. You can earn 2 extra lab points by implementing DoAdd. Note that this function should be relatively easy to implement compared to other extra credit parts of labs.
  • Prompt the user for the data to Add. You MUST use getline to read information since getline is used throughout the program. Since getline reads strings, you'll need to use atoi to convert a string to an int and atof to convert a string to a float. See the code in the function DoRead for some hints.

  • You will want to Check to see if the ID Number the user has entered is already in the Database. To do this you'll call the ContainsID member function. This function returns true if the id is present in which case it also returns the CD containing the id. You must pass a CDinfo variable for the second parameter to hold the returned information even if your code only uses the bool value returned by the function.

  • You will then want to use the Add member function to actually add the CD to the Database.
back to lab contents

Submitting The Lab

To submit assignments you'll run the command below, but substitute your section number (1, 2, or 3) for N.
    submit100e lab5.N README usedb.cc cdlist.cc cdlist.h

You can enter the files in any order.

Remember that every assignment must have a README file submitted with it (please use all capital letters). Include your name, the date, and an estimate of how long you worked on the assignment in the README file. You must also include a list of names of all those people with whom you collaborated on the assignment.

You also must turn in the inlab questions either by turning in the sheet during lab or by submitting the answers with your README.