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:
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
If you are just starting this lab from scratch, follow the instructions below.
Once you have this class written, add a main function, and test the class by creating some CDInfoNode objects and calling their method functions.
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.
$ 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
savefile = open("cdsaved.db", 'w') savefile.write("a, b, c\n") savefile.write("1, 2, 3\n") savefile.close()add a menu option to allow the user to save the updated database to a specified file. You should write your output file in the same format as the original cd.db file so you could use your saved file as input later.
from sys import argv """ argv is a list of strings containing all the words that appear after the python command """ if len(argv) < 2: #argv[0] is always the name of the file you are running print "usage: python %s <filename>" % (argv[0]) else: #other options start at argv[1] print "reading from file: %s" % (argv[1])
Once you are satisfied with your program, hand it in by typing handin21 in a terminal window.