A Donation to the Music Archive

Lab Assignment 5

This lab gives you practice with simple nested data structures involving ArrayList and LinkedList, formatted console output, sequential input from both the local disk and a Web site, sequential output, and the Scanner class; interfaces, inheritance and polymorphism also play a large role.


Pair Programming

You may pair program for this assignment, or work alone. Make this decision before you begin work on the assignment. Once you make the decision, it stands for the duration of the assignment. If you choose to pair program, you must work with the same partner for the assignment's duration.


Program Requirements

A serious collector of recorded music has left his collection to a music archive. You've been hired to create a title index and some "media counts" for the collection, using the information from a file that lists the donated items. The archive needs the title index to easily find items; it needs the counts to determine how many storage shelves are required for each kind of media.

The archive has begun making a catalog of the donated items. A catalog entry consists of an accession number (a unqiue identifier), the work's title, the kind of media the music is on, such as paper (a book or pages of sheet music) or vinyl record(s) and, depending on the kind of media, a bit more about the item; details below.

An accession number is no more than 10 characters, and can be made up of digits and/or letters (and no other symbols). The title is no longer than 50 characters and always starts with a capital letter; it never contains a semicolon (for reasons that will become clear shortly). The media categories are each designated by a single uppercase letter, as shown in the chart below, For each media type, the chart also shows the additional data about the item that is to be included in the report:

Letter   Category                    Additional Information
C        Compact media               number of tracks (integer), year of release (string)
P        Paper (sheet music, books)  number of pages (integer)
V        Shellac & vinyl records     record label's imprint (string), speed of play - 33, 45, 78 (integer)
W        Wax cylinder                maker (string)

Technical Details

You've worked out with your archive contact that the input file given to you will be a text file, called music.txt, that can be found in one or two places: on the local disk, or on the web site http://www.ics.uci.edu/~jacobson/ics45J/LabManual/LabFiles/Lab5Files -- so the Web path to the file is
http://www.ics.uci.edu/~jacobson/ics45J/LabManual/LabFiles/Lab5Files/music.txt

When the program is run, its first action (the user sees) is a question asking whether to use the version of the file on the disk or the Web; the program uses the chosen version as the input file, if it exsits. If it does not, or some other problem prevents the file from being found, the program prints out a message to the console window describing the problem and exits.

Each donated item's information is on one line (and there is only donated item per line). Each line will have the form

acccession number; title; media category code; additional information

that is, a line is formed by an accession number, a title, and a media category code, and one or two additional items of information, depending upon the media code, with each of these fields separated by a semicolon (;) and a space. (There is no ";" or space at the end of the line.) Each line will end with the standard PC end-of-line mark—that is, a carriage return character followed by a line feed character; in Java terms, the characters \r\n.

The provided music file will have been run though a testing program to ensure its format is correct and that its fields follow the specifications given for them. You can be confident that the music file will be in the correct format to be fed into your program.

The indexed items are to be placed into a text file called index.txt. It will have the information about each musical item on one line, nicely formatted, and be in alphabetical order by title. Be sure the program tells the user the name of the file, and where to find it.

The counts are to be displayed in the console window in a neat, easy-to-read arrangement, after the index file is constructed (and stored). Each count is labeled so the user knows the media to which the count refers.

The basic flow of the program goes like this:

	Ask the user if the music file is on disk or on the Web
	If it exists, open it (as a Scanner); if not (or other problem), exit with a message to the user
	Construct an (empty) MusicList
	For each line of the music file,
		use it to build a MusicItem object
		find the Bucket in the MusicList at which the MusicItem is to be inserted
		place it into that Bucket, in alphabetical order by title
		increment the media count for the media type of this MusicItem
	Close the music file
	Open the index file for sequential output; if any problems, exit with a message to the user
	For each Bucket in the MusicList
		For each MusicItem in the Bucket
			write the MusicItem out (nicely formatted) to the index file
	Close the index file.
    Print a message telling the user the index' file's name and location
	Print out the media counts, labeled so the user knows what's what
	Compute and print the count of all the items, also appropriately labeled 

When you create index.txt for the first time (or re-create it when it has been deleted) you will likely need to select your project folder and issue the Refresh command (it’s in the File menu, among other places) to see it in the Package Explorer.

After you run the program, if you open index.txt in the editing window, you may very well be told the “resource is out of sync with the file system... .” This occurs because the editor has the old version of the file in its memory, but knows the version on disk has changed. Just hit F5 to display the current contents of the index file. And if you run the program with index.txt file open in the editor window, when you go back to look at its contents (after the program completes) you’ll likely see a dialog box warning you that "The file...has been changed on the file system" and asking if you want to "replace the editor contents with these changes?" Click on Yes; you will then see the current contents of index.txt in the window.

A hint: You will probably find the String method compareTo() quite useful. s.compareTo(t), with s and t being Strings, returns 0 if s and t have the same value, a number less than 0 if s comes before t in alphanumeric order, and a number greater than 0 if s comes after t. This method comes in handy when figuring out where to place a music item in the music list so that ordering by title is maintained.

Another hint: You will also probably find the String method split(delimiter) quite useful. split returns an array (of type String) with each cell containing the substring that is terminated by the given delimiter, or by the end of the string; they are placed in order in the array. So, if you have, say, a string "This is a sentence." stored in aSentence, String[] words = aSentence.split(" ") will return words[0] = "This", word[2] = "is", word[3] = "a" and word[4] = "sentence." -- a handy method for breaking up the music item line into its component parts.

An admonition: There are two methods in the ArrayList and LinkedList class called add. add(item) adds an item to the end of the list. add(i, item) adds an item at position i of an list, moving all items from i to the end of the list "over by one." You will likely need to use both of these in your program; do not confuse them!

As usual, we provide an Eclipse Project, Lab5.zip. In it, you will find interfaces for the classes you are to write to create the program and an test input file. (Unlike previous assignments, you are writing this program from scratch.) The actual list of music will be provided to you at a later date, as the cataloging of the collection is ongoing. So, you will have to test your program using a “fake” list. We've provided one for you, called music.txt. You should also test your program on other test lists you put together, ones designed to try and "break" your program. (And if they do not break your code, then you have reasonably certainty that the indexing part of your program works correctly.) Especially, try music files that are empty; have one, two or three items; place one item in every bucket; add a new item before all items in a bucket or after all items in a bucket...any and all cases that might uncover a potential mistake.


Deliverables

Zip up your project folder into the file MusicArchive.zip, and turn it in via Checkmate.



Written by Norman Jacobson for ICS 21 Fall 2004, July 2004.
Minor revisions, by Norman Jacobson for ICS21 Winter 2005, December 2004.
Rvised to reflect encapsulation of file handling routines, by Norman Jacobson for ICS21 Fall 2006, November 2006.
Minor edits for clarity, by Norman Jacobson, December 2006 and September 2008
Minor editing to reflect use of Eclipse and for improved clarity, by Norman Jacobson, September 2009.
  Includes some text adapted from Alex Thornton's Lab 4 for ICS 21 Summer 2009.
Minor typos fixed, by Norman Jacobson, September 2010.
Revised to remove requirement of status messages (processing is fast enough that users
  will not wonder if program is hung up), and to make explicit the requirement to display
  the name of the index file, by Norman Jacobson, December 2010.
Revised from ICS21 Lab Assignments 4 and 5, for ICS 45J, by Norman Jacobson, September 2012.
Deliverables section added, by Norman Jacobson, October 2012.