CS21 Lab11: CD Database

Due 11:59pm Tuesday Dec 8

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

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 this lab 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
You will use a modified version of a linked list to store the CD information.

The cd.db file is formated such that associated with each CD are three pieces of information: artist name; album title; and release 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
Note: If you completed the warmup exercises for lab 11, copy your cdinfo.py file to cdlinked.py and use cdlinked.py to modify your previous work with the changes below. Many of the transition steps are described in the next step section of the warmup exercises.

If you are just starting this lab from scratch, follow the instructions below.

CDInfoNode

Open a file, cdlinked.py, and start by writing a class named CDInfoNode. CDInfoNode should store data about an individual CD. CDInfo should have __int__ and __str__ method functions and have accessor and mutator method functions for each data field. Additionally, your class should have a next field that can refer to another CDInfoNode object.

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

CDLinkedList

Your CDInfoNode class will form the basis of a CDLinkedList class that will be very similar to the LinkedList class described during class. Follow these steps to get your CDLinkedList class started:
  1. Create a CDLinkedList constructor with head, tail, and size fields.
  2. Add an __str__ method that returns a string containing all the CD info in the list. You may put each album on a separate line by concatenating each CD with newline "\n" characters between entries.
  3. Add an insertAtHead method that takes an artist, title, and date value as parameters, creates a new CDInfoNode, and adds it to the front of the CDLinkedList.
  4. Add some test code to create a new CDLinkedList object and add a few nodes to it.
Creating a CD Database
Once you have tested your CDLinkedList class, use it to create a CD Database by implementing the following functions (If you completed the lab11 warmup exercises, you may only need to slightly modify your functions to work with a CDLinkedList instead of a python list):
  1. write a function to create a CDLinkedList of CDInfoNode 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). This function should return a CDLinkedList object.
  2. write a CDLinkedList method that prints out the CDs 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
  5. write additional functions/methods as needed to implement the three types of searches listed in the menu (artist, year, title). Note that you only need to print the result and number of matches. These functions/methods do not need to return anything. Hint: you will probably want to add a method that prints all CD with a matching artist, for example. This method should take a string parameter describing the artist name. Be sure to print an informative message if no matches are found.

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, get the user's choice and process the user's choice appropriately. The loop should continue until the user enters the Quit option.

Once your code is working correctly, add a method insertSorted to your CDLinkedList that inserts a new album into the list in sorted order. You can decide the order (by artist, by album title, or by year). Then modify the code that creates the CD database to use insertSorted instead of insertAtHead.

Sample output
NOTE: This sample output is from a run before the insertSorted functionality was added. In your complete program, after implementing and testing full functionality on the unsorted linked list (like the example below), you will change your program to call insertSorted instead of insertAtHead. Thus, in the final version, when you print out CDs from the DB, the CDs will be listed in sorted order.
$ python cdlinked.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 in a given 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

Enter an artist name : De La Soul

Matching Albums
---------------
De La Soul, 3 Feet High and Rising, 1989
De La Soul, De La Soul Is Dead, 1991

There are 2 total matches

##############    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 : 1

Enter an artist name : Lady Gaga

Sorry, no matching Albums found for this artist. Perhaps you should buy some.


##############    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 : 5

Enter the Artist Name : Soul Coughing
Enter the Album Name : Ruby Vroom
Enter the Year : 1994

Added Soul Coughing, Ruby Vroom, 1994


##############    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


Extra Options
These questions are NOT required to receive full credit. Furthermore, do not attempt to solve these problems until the required portion of the assignment is complete. There are many extensions you could add to this program. Here are a few we thought might be interesting.
Submit

Once you are satisfied with your program, hand it in by typing handin21 in a terminal window.