CD/DVD Collection This program will allow the user to keep track of both a CD an
ID: 3570153 • Letter: C
Question
CD/DVD Collection
This program will allow the user to keep track of both a CD and a DVD collection. The program needs to work for both CDs and DVDs. There should be a base class that maintains the common information between a CD and a DVD. Each CD/DVD in the collection will be represented as a class, so you will have one class that will be a CD and another that will be a DVD. The program will then has a base class called Media. Both the CD and DVD classes will inherit from the same base class.
The base class will have one data member that will hold the CD Name or Movie Title and another data member for the length of the total CD or the length of the movie.
The CD class will use a linked list to keep track of the titles of the songs on the CD; this will allow each CD to have a different number of songs. It should also maintain the length of each song, thus the class will use a structure which will have the song title and its length. Each song will be an instance of this structure and will be stored in the linked list. The class will also have a data member for the artist name.
The DVD class will have data members for the year the movie was released. The class will have a linked list for storage of the name of the actors and actresses in the movie. The class will also maintain the character names that the actors/actresses played in the movie. The actors/ actresses and characters information will be stored in a structure which will be stored in the linked list.
The program will then maintain two lists, one for the CDs and another for DVDs. The program will use linked lists to maintain the two different lists. The program must provide methods (functions) to add a CD/DVD, remove a CD/DVD and update a CD/DVD. There should also be a function that displays the entire list of CDs/DVDs. The output must be a table format, with heading.
NOTE: There must be only one linked list class (template) that will be used for each of the lists needed in this program. Derived classes of the linked list can be added to expend the functionality of the linked list to be specific for each class.
Movie Title Length of Movie Year Released Actors/Actresses Characters
NOTE: The movie title, length of movie and year released should only appear once while the actors/actresses and characters will have several lines. So the other columns must be displayed with blanks.
Artist CD Name Length of CD Song Title Song Length
NOTE: The artist name, CD name and length of CD should only appear once while the song title and length will have several lines. So the other columns must be displayed with blanks.
Explanation / Answer
public class Project1
{
static String userIn; //Determines what the user wants to do in the application
static String[] info = new String[] {}; //Stores album info
static LinkedList <String> list = new LinkedList<String> (); //Array that stores the info to file
static Scanner scan = new Scanner(System.in); //Takes input from keyboard
static String album; //Takes in the album name
static String artist; //Takes in the artist name
static String date; //Takes in the release date of the album
static String length; //Length
static FileWriter fstream; //Used to write to file
static BufferedWriter out; //Used to write to file
static String file_name ; //The name of the file is stored
static String search ; //Takes input
public void welcome() //Main menu of the program where user makes selection
{//Method will be used as a main menu asking the user what he/she would like to do
System.out.println("*****MAIN MENU*****");
System.out.println("");
System.out.println("To add your album to the system please enter the word add");
System.out.println("");
System.out.println("To edit a album in your collection please insert the word edit");
System.out.println("");
System.out.println("If you would like to delete a album please insert the word delete");
System.out.println("");
System.out.println("To search for a particular album by the CD or DVD name please insert the word cd");
System.out.println("");
System.out.println("To search for your album by artist name insert the word artist");
System.out.println("");
System.out.println("To search for your album length");
userIn = scan.next(); //Takes user input to decide what action to take
}//Close of welcome method (Serving as main menu)
public void adding() //Used to add info to the linked list
{//Start of adding method
if(userIn.equals("add")) //Starts the add method
{//Start of if statement
System.out.println("*****ADD MENU*****");
System.out.println("Please enter the name of the artist");
artist = scan.next(); //Stores the artist name
System.out.println("Please enter the album name");
album = scan.next(); //Stores the album name
System.out.println("Please enter the release date");
date = scan.next(); //Stores the release date
System.out.println("Please enter the length");
length = scan.next(); //Stores the length
list.add(artist); //Adds the artist variable to file
list.add(album); //Adds album variable to file
list.add(date); //Adds the date variable to file
list.add(length); //Adds the length
System.out.println("Album name,Artist Name,Date of release,length" + list); //Displays the linked list
System.out.println(); //Prints Blank Line
file_name = "output.txt"; //File where info is stored to
try
{//Start of try and catch method to catch fileNotFound Exception
fstream = new FileWriter(file_name); //Creates an instance of the fileWriter class
out = new BufferedWriter(fstream); //Creates instance of the BufferedWriter class
out.write(artist); //Adds artist variable to file
out.write(album); //Adds album variable to file
out.write(date); //Adds date variable to file
out.write(length); //Adds length variable to file
out.close(); //Closes the input stream
}
catch (IOException e) //Catches FileNotFoundException
{
e.printStackTrace();
}//End of try/catch
}//End of if statement
}//End of adding method
public void edit()
{//Starts the edit method
if(userIn.equals("edit"))
{//Start of if statement which searches for the album through a matching word
System.out.println("*****EDIT MENU*****");
System.out.println("Please enter the name you would like to edit");
search = scan.next(); //Takes input to match files that need to be edited
if(search.equals(album)) //Matches the name the user wants edited to the name in the list
{//Start the if statement that
System.out.println(list); //Displays the list containing the info
list.removeAll(list); //Removes all the input to allow user to edit
System.out.println("Please enter new name of the artist ");
artist = scan.next(); //Allows user to add a new artist
System.out.println("Please enter new album name ");
album = scan.next(); //Allows user to add a new album
System.out.println("Please enter new release date");
date = scan.next(); //Allows user to add a new release date
System.out.println("Please enter length");
length = scan.next(); //Allows user to add a length
list.add("," + artist); //Adds the artist variable to the linked list
list.add("," + album); //Adds the album variable to the linked list
list.add( date); //Adds the date to the variable to the linked list
list.add( length); //Adds the length to the variable to the linked list
System.out.println("Album name,Artist Name,Release Date,length" + list); //Displays the Linked List
try
{//Start of try and catch method to catch fileNotFound Exception
fstream = new FileWriter(file_name); //Creates an instance of the fileWriter class
out = new BufferedWriter(fstream); //Creates instance of the BufferedWriter class
out.write(artist); //Adds artist variable to file
out.write(album); //Adds album variable to file
out.write(date); //Adds date variable to file
out.write(length); //Adds length variable to file
out.close(); //Closes the input stream
}
catch (IOException e) //Catches FileNotFoundException
{
e.printStackTrace();
try
{//Start
out.close(); //Closes the stream
}//End
catch (IOException z)
{//Start of catch statement
z.printStackTrace();
}//End of try catch
}
}//End of if statement
else
{//Start of
System.out.println("Album not found"); //Displays if the user input could not be linked to info in the linked list
}//End of if else statement
}//End of if statement
}//End of method
public void delete() //Used to delete info from the linked list
{//Start of delete method
if(userIn.equals("delete"))
{//Matches user input to verify if correct method
System.out.println("*****DELETE MENU*****");
System.out.println("Please enter the name to delete");
search = scan.next(); //Takes in the name to match with linked list
System.out.println("To continue insert 1");
int uSure = scan.nextInt(); //Takes input to verify if deletion is desired
if(uSure==1)
{//Starts
System.out.println("Please enter the name of the album you would like to delete"); //Displays the file contents to the user
list.removeAll(list); //Deletes the contents of the list
}//End of second if statement
if(search.equals(album))
{//Searches for info in linked list according to user input.
System.out.println(list);
list.remove(); //Removes info from the linked lists
System.out.println("Album deleted");
try
{//Start of try and catch method to catch fileNotFound Exception
fstream = new FileWriter(file_name); //Creates an instance of the fileWriter class
out = new BufferedWriter(fstream); //Creates instance of the BufferedWriter class
out.write(artist); //Adds artist variable to file
out.write(album); //Adds album variable to file
out.write(date); //Adds date variable to file
out.write(length); //Adds length variable to file
out.close(); //Closes the input stream
} //end of try
catch (IOException e) //Catches FileNotFoundException
{
e.printStackTrace();
}//End of try/catch
}
else
{
System.out.println("Album not found");
}//End of if statement
}//End of first if statement
}//End of method
public void search()
{//Start of search method
if(userIn.equals("search"))
{//Matches user input to verify if correct method
System.out.println("*****SEARCH MENU*****");
System.out.println(); //Prints out a blank line
System.out.println("Please insert the name of the album");
search = scan.next(); //Takes user input
if(album.equals(search)) //Matches user input to info in linked list
{
System.out.println(list); //Matches user search to info in linked list
}
else
{//Ends the if statement
System.out.println("Album name could not be found"); //Displayed if user input could not be matched with info in the linked list
}//End of second if statement
}//End of first if statement
}//End of search method
public static void main(String [] args) throws IOException
{//Starts the main method which runs the application
Project1 cd = new Project1(); //Making an instance of the class
cd.welcome(); //Runs the welcome method
cd.adding(); //Runs the adding method
cd.edit(); //Runs the edit method
cd.delete(); //Runs the edit method
cd.search(); //Runs the search method
}//End Of Main method
}//End Of Class