CS21 Lab11: Starting Point

For this program you may work with one partner (you may not work in groups larger than two).

This is just get you started on some things you will need to do for lab 11. You do not need to submit anything for this part. However, we strongly suggest that you do this part prior to starting the regular lab 11 assignment that will be assigned early next week.

Run update21 to create the cs21/labs/11 directory. Then cd into your cs21/labs/11 directory and create the python programs for lab 11 in this directory. After running update21, you will have a file named cd.db that contains an example database of CD information.

For lab 11 you will write a program that reads in information from a CD data base into a list and then enters a loop with the following menu options for performing different operations on the list:

  1. Print all CDs by a given artist (and print out the number of matches)
  2. Print all CDs with a given title (and print out the number of matches)
  3. Print all CDs released in a given year (and print out the number of matches)
  4. List all the CDs in the database
  5. Add a new CD to the database
  6. Quit
In lab 11, you will use a linked list to store the CD information, but as a starting point, you will use a regular Python list and only implement three parts of the functionality: The cd.db file is formated such that assocaiated with each CD are three pieces of information: artist name; album title; and publication year. Each of these three is stored on a single line and separated by a comma. For example, the file for two CDs (one by James Brown and one by Sleater-Kinney) might look like this:
James Brown,Get on the Good Foot,1972
Sleater-Kinney,Dig Me Out,1997


Getting Started
Open a file, cdinfo.py, and start by writing a class named CDInfo. CDInfo should store data about an individual CD. CDInfo should have __int__ and __str__ methods and have accessor and mutator methods for each data field.

Once you have this class written, add a main function, and test the class by creating some CDInfo objects and calling their method functions.

Next, implement some functions that you will use as starting points for your lab 11 assignment:

  1. write a function to create a list of CDInfo objects from data that is read in from a file (pass the filename to this function in addition to any other parameters that you think this function needs).
  2. write a function that takes the list of CDInfo objects and prints out the cd's in the list
  3. write a function that prints out the menu of options.
  4. write a function that gets and returns a valid menu option entered by the user
In your main function, call the function to create the cd list, and then in a loop call your function(s) to print the menu and get the user's choice. If the user chooses print the list, then call your function that prints out the CD list. Otherwise, just print out the option that the user entered. The loop should continue until the user enters the Quit option.

Once this is working, apply some top-down-design to add other functions to your program. Add them just as function shells for now, but think about what values they need to be passed and what they return, and you could try adding calls to the function shells from main. We do not recommend that you implement full functionality for these because you are not going to be using a list of CDInfo objects for lab 11, but a linked-list of CDInfoNode objects instead.

Sample output
$ python cdinfo.py 

##############    CD DB Menu  ########################

   1. Print all CDs by a given artist and the total number
   2. Print all CDs with a given title and the total number
   3. Print all CDs released on a give year and the total number
   4. Print all CDs in the DB and the total number
   5. Add a new CD to the DB
   6. Quit
######################################################

Enter a value between 1 and 6 : hello
Hey, hello isn't a number...try again
Enter a value between 1 and 6 : 12
Hey, 12 isn't between 1 and 6 ...try again
Enter a value between 1 and 6 : 1

Option 1 not yet supported


##############    CD DB Menu  ########################

   1. Print all CDs by a given artist and the total number
   2. Print all CDs with a given title and the total number
   3. Print all CDs released on a give year and the total number
   4. Print all CDs in the DB and the total number
   5. Add a new CD to the DB
   6. Quit
######################################################

Enter a value between 1 and 6 : 4

The CD DB:
---------
Public Enemy, It Takes a Nation of Millions to Hold Us Back, 1988
James Brown, Get on the Good Foot, 1972
Sly & the Family Stone, Fresh, 1973
Bob Marley, Survival, 1979
De La Soul, 3 Feet High and Rising, 1989
Patti Smith, Horses, 1975
Sleater-Kinney, Dig Me Out, 1997
X, Under the Big Black Sun, 1982
Charles Mingus, Mingus Ah Um, 1959
X, More Fun in the New World, 1983
Sleater-Kinney, One Beat, 2002
Killdozer, For Ladies Only, 1989
MC5, Kick Out the Jams, 1969
Spot 1019, Spot 1019, 1986
De La Soul, De La Soul Is Dead, 1991
X, Los Angeles, 1980
The Fall, The Infotainment Scan, 1993
Al Green, Greatest Hits, 2005
Ramones, Greatest Hits, 1990
Sly & the Family Stone, Greatest Hits, 1970
Sly & the Family  Stone, There's a Riot Goin' On, 1971

There are a total of 21 CDs in the library


##############    CD DB Menu  ########################

   1. Print all CDs by a given artist and the total number
   2. Print all CDs with a given title and the total number
   3. Print all CDs released on a give year and the total number
   4. Print all CDs in the DB and the total number
   5. Add a new CD to the DB
   6. Quit
######################################################

Enter a value between 1 and 6 : 6

bye bye


The next step
If you want to work ahead even more, then once you get the above done and tested you could try the following:
  1. Copy your cdinfo.py file to cdlinked.py. Change the CDInfo class to be the CDInfoNode class, and add a next field and accessor and mutator methods for the next field.
  2. Create a new class, CDLinkedList, that has a head, tail, and size field.
  3. Add an __init__ method to this class
  4. Add an insertAtHead method that takes an artist, title, and date value, creates a new CDInfoNode and adds it to the front of the CDLinkedList.
  5. Add some calls from main to create a new CDLinkedList object and to add a couple elements to it