In this assignment you are asked to develop software that will let a user organize his or her music collection. Instead of a database you will be storing all the information in a file. Your program will be an interface to this file and provide a database-like functionality. The user of your program will be able to add songs to the file, delete songs, search for songs, or display everything on file.
The songs will be stored in a file called songs.txt. The format of each song has been delibrately kept simple. The songs are kept in sorted order in the file. Songs are sorted by artist and then title.
Artist: The Beatles Title: The Fool on the Hill
In your program you will read in the data from the file, create a Song object for each song and store these objects in an array. Your program will be menu driven. The menu will be of the following form:
Song Catalog Menu 1. Import songs from a file 2. Add a song 3. Delete a song 4. Search for a song 5. Display all songs 6. Exit program Enter selection (1 - 6):
If the user does not enter a vaild selection, i.e. a number between 1 and 6 inclusive you will reprint the menu. For valid menu selection you will handle each case separately.
1. Import Songs from a File: You will prompt the user to enter the absolute path name to the file. Check if the file exists and is readable. The format for the data must be in the same form as songs.txt. Read the contents of the file and populate the array. Once you have processed the request reprint the menu.
2. Add a new song: You will prompt the user to enter the name of the artist and the title of the song.
Enter artist: Enter title:If that song is already there, print a message to that effect. If the song is not there you will insert the song in its rightful place in the sorted array of songs. You will be using a variation of insertion sort for doing that. After you have processed this request you will reprint the main menu.
3. Delete a song: You will prompt the user to enter if the deletion will be by artist or title. For invalid entries prompt the user again. Depending on the valid response that you get, prompt the user to enter either the name of the artist or the title of the song.
Delete by artist or title (A or T): A Enter artist:If the deletion is by artist name you will delete all songs by that artist. If the deletion is by song title, then you will delete only the entry for that song. If the artist or the title does not exist, you will write a message to that effect. After you have processed this request you will reprint the main menu.
4. Search for a song: You will prompt the user to enter if the search will be by artist or title. For invalid entries prompt the user again. Depending on the valid response that you get, prompt the user to enter either the name of the artist or the title of the song.
Search by artist or title (A or T): T Enter title:If the search is by artist name, you will display all songs by that artist in the collection. If the search is by title you will display the name of the artist and the title in the collection. If the artist or the title does not exist then you will write a message to that effect. After you have processed this request you will reprint the main menu.
5. Display all songs: You will display all songs in the array. After you have processed the request you will reprint the main menu.
6. Exit program: You will over-write the file songs.txt with the modified array of songs in the original format. You will write a message thanking the user for using the software.
Here is the skeleton of the code to get you started:
import java.util.*; import java.io.*; class Song implements Comparable { public String artist; public String title; public Song (String artist, String title) { this.artist = artist; this.title = title; } public boolean equals (Object obj) { if (obj instanceof Song) { Song aSong = (Song) obj; return artist.equals(aSong.artist) && title.equals(aSong.title); } else { return false; } } public String toString () { String str = "Artist: " + artist + "\n" + "Title: " + title; return str; } public int compareTo (Object obj) { Song aSong = (Song) obj; int comp = artist.compareTo(aSong.artist); return ((comp == 0) ? title.compareTo(aSong.title) : comp); } } class SongList { private Song[] catalog; private int numSongs; public SongList () { catalog = new Song [100]; numSongs = 0; createCatalog(); } // Increase size of array if more songs are added private void incrCatalog () { Song[] newCatalog = new Song[catalog.length + 50]; for (int i = 0; i < catalog.length; i++) { newCatalog[i] = catalog[i]; } catalog = newCatalog; } // This method reads the file and populates the array private void createCatalog () { } // This method reads data from a file public void readFile (String aFile) { } // This method adds songs to the array public void addSong (String artist, String title) { } // This method deletes all songs by an artist public void deleteByArtist (String artist) { } // This method deletes a given song public void deleteByTitle (String title) { } // This method searches for all songs by an artist public void searchByArtist (String artist) { } // This method searches for a given song public void searchByTitle (String title) { } // This method displays all entries in the collection public void displayCatalog () { for (int i = 0; i < numSongs; i++) { System.out.println (catalog[i]); System.out.println (); } } // This method over-writes the file public void writeFile () { } // Other methods that you may need } public class SongCatalog { public static void main (String[] args) { SongList album = new SongList(); // Create menu } }
The file that you will be turning in will be called SongCatalog.java. The file will have a header of the following form:
/* File: SongCatalog.java Description: Student's Name: Student's UT EID: Partner's Name: Partner's UT EID: Course Name: CS 313E Unique Number (55490/55495): Date Created: Date Last Modified: */
You will follow the standard Java Coding Conventions. You can either view the HTML page or download the PDF or Postscript and print it out. There is a modification that I would like to make to the standard coding conventions. Please align the opening and closing braces vertically so that you can easily make out the blocks of code. For example:
Do this: if ( x > 5 ) { a = b + c; } Not this: if ( x > 5 ) { a = b + c; }
Use the turnin program to submit your SongCatalog.java file. The TAs should receive your work by 5 PM on Sunday, 24 February 2008. There will be substantial penalties if you do not adhere to the guidelines. The TA in charge of this assignment is Sumit Chougule (sumit.chougule@gmail.com).