From 4c37298e054dc1681875a71b181ce0478c577349 Mon Sep 17 00:00:00 2001 From: Clyhtsuriva Date: Fri, 11 Dec 2020 17:48:10 +0100 Subject: (De)Serialization of the songs Serialization and Deser now work fine for the songs. We're able to store arrays. --- Song.java | 2 +- jMusicHub.java | 103 ++++++++++++++++++++++++++++++++++----------------------- 2 files changed, 63 insertions(+), 42 deletions(-) diff --git a/Song.java b/Song.java index 2890536..6e26332 100644 --- a/Song.java +++ b/Song.java @@ -15,7 +15,7 @@ public class Song extends MusicalElement{ public void setGenre(Genre genre){this.genre=genre.name();} public String toString() { - return "Title : "+getTitle()+"\nDuration : "+getDuration()+"\nContent : "+getContent()+"\nArtist : "+getArtist()+"\nGenre : "+getGenre(); + return "Id : "+getId()+"\nTitle : "+getTitle()+"\nDuration : "+getDuration()+"\nContent : "+getContent()+"\nArtist : "+getArtist()+"\nGenre : "+getGenre(); // return getTitle()+";"+getDuration()+";"+getContent()+";"+getArtist()+";"+getGenre(); } diff --git a/jMusicHub.java b/jMusicHub.java index 26fe9b6..82b364b 100644 --- a/jMusicHub.java +++ b/jMusicHub.java @@ -46,13 +46,69 @@ public class jMusicHub { if (confirm.equalsIgnoreCase("Y")){ //if the user is ok with what he typed, create a song obj Song newSong = new Song(title, duration, content, artist,genre); return newSong; -// songs.add(newSong); } else { System.out.println("Aborting..."); System.out.println(""); return null; } } +/**

saveSongs

+ * saveSongs is used by the command "s" + * @param songs ArrayList Object + * */ + public static void saveSongs(ArrayList songs){ + + String filename = "songs"; + + // 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(songs); + + out.close(); + file.close(); + + System.out.println("Your songs have been serialized\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"; + + // Deserialization + try { + // Reading the object from a file + FileInputStream file = new FileInputStream(filename); + ObjectInputStream in = new ObjectInputStream(file); + + @SuppressWarnings("unchecked") + + // Method for deserialization of object + ArrayList songs = (ArrayList)in.readObject(); + + in.close(); + file.close(); + System.out.println("Object has been deserialized"); + for (Song s : songs ){ + System.out.println(s); + } + } + catch (IOException ex) {System.out.println("IOException is caught");} + catch (ClassNotFoundException ex) {System.out.println("ClassNotFoundException is caught");} + } + public jMusicHub() { @@ -65,18 +121,6 @@ public class jMusicHub { //used to temporarly save the songs before any "s" command System.out.println("Starting extraction"); - //Here will be the process to extract the files -/* try { - FileOutputStream fos = new FileOutputStream(file); - ObjectOutputStream oos = new ObjectOutputStream(fos); - oos.writeObject(albums); - oos.close(); - fos.close(); - } - catch (IOException ioe) { - ioe.printStackTrace(); - return; - }*/ System.out.println("Extraction done\n"); //Here will be the number of playlist, albums, songs and auidobook extracted @@ -85,7 +129,7 @@ public class jMusicHub { userInput = scan.nextLine(); switch(userInput) { case "h" : //page help - System.out.printf("c: add a new song\na: add a new album\n+: add an existing song to an album\nl: add a new audiobook\np: create a new playlist from existing songs and audiobooks\n-: delete a playlist\ns: save playlists, albums, songs and audiobooks into the concerned files\nh: print this help screen\nq: quit the program\n"); + System.out.printf("c: add a new song\na: add a new album\n+: add an existing song to an album\nl: add a new audiobook\np: create a new playlist from existing songs and audiobooks\n-: delete a playlist\ns: save playlists, albums, songs and audiobooks into the concerned files\nS: List all your songs\nh: print this help screen\nq: quit the program\n"); break; case "q" : System.out.println("Goodbye !"); @@ -120,34 +164,11 @@ public class jMusicHub { case "-": break; case "s": - for (Song s: songs){ - String filename = "songs.csv"; - - // 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(s); - - out.close(); - file.close(); - - System.out.println("Object has been serialized\n" - + "Data before Deserialization."); - System.out.println(s); - } - catch (IOException ex) { - System.out.println("IOException is caught"); - } - } + saveSongs(songs); + break; + case "S": + listSongs(); break; - default : System.out.println("Unknown command. Type h for help."); -- cgit v1.2.3