CS21 Lab12: A CD Database

  Due: 11:59pm THURSDAY April 29

You may work with one partner on this lab.

Run update21 to create the cs21/labs/12 directory. Then cd into your cs21/labs/12 directory and create the python programs for lab 12 here. you will have a file named cd.db that contains an example database of CD information, and a file called cdlinked.py where you will write your solution.

Read through the entire lab before you begin coding. Then go to the section on Getting Started and follow the instructions there to begin.

Introduction

For this lab you will write a program that reads in information from a CD data base into a linked 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 (in chronological order)
  5. Quit
You will use a modified version of a linked list to store the CD information. You should adapt the code we've been creating in class to solve this problem.

The cd.db file is formated such that associated with each CD are three pieces of information: artist name, album title, and release year. The information for each CD is stored on a single line and is separated by commas. For example, the the first three lines of the file are:

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

Getting Started

Edit the file called cdlinked.py. In this file, you will create two classes: CDInfoNode and CDLinkedList.

1. CDInfoNode

CDInfoNode should store data about an individual CD: album, artist, and year. CDInfoNode should have __int__ and __str__ methods and have accessor and mutator methods 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.

2. 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 class started:


Creating a CD database

Once you have tested your CDLinkedList, use it to create a CD Database by implementing the following:

  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 function that prints out the menu of options.
  3. Write a function that gets and returns a valid menu option entered by the user.
  4. Add methods to the CDLinkedList class to implement the searches listed in the menu (by artist, year, album, and all). Note that you only need to print the result and number of matches. These methods do not need to return anything. Be sure to print an informative message if no matches are found.

Creating a sorted CD database

Once all of the functionality described above is working correctly add an insertSortedByYear method that takes an artist, title, and year as parameters. It creates a new CDInfoNode, and adds it to the CDLinkedList so that the list is sorted in chronological order by year.

Here is some pseudocode to help you get started on this method:

if list is empty or year <= head's year
   insert new data at head
elif year >= tail's year
   insert new data at tail
else
   set previous to None
   set current to head
   while current's year < year
      set previous to current
      set current to current's next
   insert the new node between previous and current

in every case need to increment the size field   

Modify your function that reads in the data from a file so that it uses this new method insertSortedByYear rather than the original method insertAtHead. Once you make this change, and view all of the data, you should see that it is now in sorted order.

Sample output
$ 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. Quit
######################################################

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

Enter an artist name : De La Soul

Matching Albums
---------------
3 Feet High and Rising - De La Soul (1989)
De La Soul Is Dead - De La Soul (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. Quit
######################################################

Enter a value between 1 and 5 : 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. Quit
######################################################

Enter a value between 1 and 5 : 4

The CD DB:
---------
Mingus Ah Um - Charles Mingus (1959)
Avalon Blues - Mississippi John Hurt (1962)
Time Out - Dave Brubeck Quartet (1964)
... 
SOME RESULTS OMITTED HERE
...
One Beat - Sleater-Kinney (2002)
Greatest Hits - Al Green (2005)
White Blood Cells - The White Stripes (2005)

There are a total of 38 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. Quit
######################################################

Enter a value between 1 and 5 : 5

bye bye

Optional extras

Do not attempt to solve these problems until the required portion of the lab 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.