From 6c06acde8fb51125e925a34b8adf428ccfb37055 Mon Sep 17 00:00:00 2001 From: Clyhtsuriva Date: Tue, 22 Dec 2020 18:38:34 +0100 Subject: Simplifying I'm creating methods in jMusicHub to simplify the work I'l will be doing to add Albums and Playlists commands such as list, extract and save. --- Song.java | 4 ++ jMusicHub.java | 206 +++++++++++++++++++++------------------------------------ 2 files changed, 81 insertions(+), 129 deletions(-) diff --git a/Song.java b/Song.java index 2dd07d5..84e1e6d 100644 --- a/Song.java +++ b/Song.java @@ -1,3 +1,7 @@ +/**

Song

+ * Song is using instanciating MusicalElement and is Serializable. + * */ + public class Song extends MusicalElement{ private static final long serialVersionUID = 2112880640160601826L; diff --git a/jMusicHub.java b/jMusicHub.java index 799c715..56cf38c 100644 --- a/jMusicHub.java +++ b/jMusicHub.java @@ -54,7 +54,7 @@ public class jMusicHub { } /**

addAudioBook

- * add Song is used to add songs thanks to the "c" option + * add Song is used to add songs thanks to the "l" option * @param scan Scanner Object */ public static AudioBook addAudioBook(Scanner scan){ @@ -98,13 +98,13 @@ public class jMusicHub { } } -/**

saveSongs

- * saveSongs is used by the command "s" - * @param songs ArrayList Object +/**

save

+ * save is used by the command "s" + * @param filename String + * @param elements ArrayList * */ - public static void saveSongs(ArrayList songs){ - - String filename = "songs"; + @SuppressWarnings ("rawtypes") + public static void save(String filename, ArrayList elements){ // Serialization try { @@ -114,52 +114,24 @@ public class jMusicHub { ObjectOutputStream out = new ObjectOutputStream(file); // Method for serialization of object - out.writeObject(songs); + out.writeObject(elements); out.close(); file.close(); - System.out.println("Your songs have been saved.\n"); + System.out.println("Your " + filename + " have been saved."); } catch (IOException ex) { System.out.println("IOException is caught"); } } -/**

saveAudioBooks

- * saveAudioBooks is used by the command "l" - * @param audiobooks ArrayList Object +/**

list

+ * list is called when using the "AB" and "S" commands in order to list the elements in respective files. + * @param filename String * */ - public static void saveAudioBooks(ArrayList audiobooks){ - - String filename = "audiobooks"; - - // Serialization - try { - - // Saving of object in a file - FileOutputStream file = new FileOutputStream(filename); - ObjectOutputStream out = new ObjectOutputStream(file); - - // Method for serialization of object - out.writeObject(audiobooks); - out.close(); - file.close(); - - System.out.println("Your audiobooks have been saved.\n"); - } - catch (IOException ex) { - System.out.println("IOException is caught"); - } - } - -/**

listSongs

- * listSongs is called when using the "S" command in order to list the songs inside the "songs" file. - * */ - - public static void listSongs(){ - String filename="songs"; + public static void list(String filename){ // Deserialization try { @@ -167,111 +139,82 @@ public class jMusicHub { FileInputStream file = new FileInputStream(filename); ObjectInputStream in = new ObjectInputStream(file); - @SuppressWarnings("unchecked") // Method for deserialization of object - ArrayList songsInFile = (ArrayList)in.readObject(); + switch(filename) { + case "songs": + @SuppressWarnings("unchecked") + ArrayList songs = (ArrayList)in.readObject(); + in.close(); + file.close(); - in.close(); - file.close(); - System.out.println("\nSaved songs :\n"); - for (Song s : songsInFile ){ - System.out.println(s); - } - } - catch (IOException ex) {System.out.println("listSongs : Audio file missing.");} - catch (ClassNotFoundException ex) {System.out.println("ClassNotFoundException is caught");} - } + System.out.println("\nSaved "+filename+" :\n"); -/**

listAudioBooks

- * listAudioBooks is called when using the "AB" command in order to list the songs inside the "audiobooks" file. - * */ + for (Song s : songs ){ + System.out.println(s); + } + break; + case "audiobooks" : + @SuppressWarnings("unchecked") + ArrayList audiobooks = (ArrayList)in.readObject(); + in.close(); + file.close(); - public static void listAudioBooks(){ - String filename="audiobooks"; + System.out.println("\nSaved "+filename+" :\n"); - // Deserialization - try { - // Reading the object from a file - FileInputStream file = new FileInputStream(filename); - ObjectInputStream in = new ObjectInputStream(file); + for (AudioBook ab : audiobooks ){ + System.out.println(ab); + } + + break; + } - @SuppressWarnings("unchecked") - // Method for deserialization of object - ArrayList audiobooks = (ArrayList)in.readObject(); - in.close(); - file.close(); - System.out.println("\nSaved audiobooks :\n"); - for (AudioBook s : audiobooks ){ - System.out.println(s); - } } - catch (IOException ex) {System.out.println("listAudioBooks : AudioBook file missing.");} + catch (IOException ex) {System.out.println(filename+ " file missing.");} catch (ClassNotFoundException ex) {System.out.println("ClassNotFoundException is caught");} } -/**

extractSongs

- * Extraction is used to print the content of the files and put them in the ArrayList used to add elements. +/**

extract

+ * Extract is used to print the content of the files and put them in the ArrayList used to add elements. + * @param String filename * */ - - public static ArrayList extractSongs(){ + @SuppressWarnings("rawtypes") + public static ArrayList extract(String filename){ // Deserialization try { + FileInputStream file = new FileInputStream(filename); + ObjectInputStream in = new ObjectInputStream(file); - String songFileName="songs"; - FileInputStream songFile = new FileInputStream(songFileName); - ObjectInputStream inSong = new ObjectInputStream(songFile); - - @SuppressWarnings("unchecked") - ArrayList songs = (ArrayList)inSong.readObject(); - //used to temporarly save the songs before any "s" command + switch (filename) { + case "songs" : + @SuppressWarnings("unchecked") + ArrayList songs = (ArrayList)in.readObject(); - inSong.close(); - songFile.close(); + in.close(); + file.close(); - return new ArrayList(songs); - //Here will be the number of playlist, albums, songs and audiobook extracted + return new ArrayList(songs); + case "audiobooks" : + @SuppressWarnings("unchecked") + ArrayList audiobooks = (ArrayList)in.readObject(); + in.close(); + file.close(); + return new ArrayList(audiobooks); + default : + return new ArrayList(); + } } catch (IOException ex) { //mainly occur bc file is missing, leading to an empty ListArray - System.out.println("\nextractSongs : Songs file missing.\n"); - return new ArrayList() ;} - catch (ClassNotFoundException ex) {System.out.println("ClassNotFoundException is caught");return null;} -} - -/**

extractAudioBooks

- * Extraction is used to print the content of the files and put them in the ArrayList used to add elements. - * */ - - public static ArrayList extractAudioBooks(){ - - // Deserialization - try { - - String audiobookFileName="audiobooks"; - FileInputStream audiobookFile = new FileInputStream(audiobookFileName); - ObjectInputStream inAudioBook = new ObjectInputStream(audiobookFile); - - @SuppressWarnings("unchecked") - ArrayList audiobooks = (ArrayList)inAudioBook.readObject(); - - inAudioBook.close(); - audiobookFile.close(); - - return new ArrayList(audiobooks); - //Here will be the number of playlist, albums, songs and audiobook extracted - - + System.out.println("\n"+filename+" file missing.\n"); + return new ArrayList() ;} + catch (ClassNotFoundException ex) {System.out.println("ClassNotFoundException is caught");return null; } - catch (IOException ex) { //mainly occur bc file is missing, leading to an empty ListArray - System.out.println("\nextractAudioBooks : AudioBooks file missing.\n"); - return new ArrayList() ;} - catch (ClassNotFoundException ex) {System.out.println("ClassNotFoundException is caught");return null;} -} + } public jMusicHub() { @@ -280,10 +223,15 @@ public class jMusicHub { Scanner scan = new Scanner(System.in); String userInput; //Used to get the user's inputs. - ArrayList songs = extractSongs(); - ArrayList audiobooks = extractAudioBooks(); - listSongs(); - listAudioBooks(); + System.out.println("Extraction beginning..."); + @SuppressWarnings("unchecked") + ArrayList songs = extract("songs"); + @SuppressWarnings("unchecked") + ArrayList audiobooks = extract("audiobooks"); + + list("songs"); + list("audiobooks"); + System.out.println("\nExtraction done."); do { System.out.println("What do you want to do? [h for help]"); @@ -344,14 +292,14 @@ public class jMusicHub { case "-": break; case "s": //save songs, audiobooks, playlist and albums - saveSongs(songs); - saveAudioBooks(audiobooks); + save("songs", songs); + save("audiobooks", audiobooks); break; case "S": //print songs contained in songs - listSongs(); + list("songs"); break; case "AB": //print audiobooks containd in audiobooks - listAudioBooks(); + list("audiobooks"); break; default : System.out.println("Unknown command. Type h for help."); -- cgit v1.2.3