aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorClyhtsuriva <aimeric@adjutor.xyz>2020-12-11 17:48:10 +0100
committerClyhtsuriva <aimeric@adjutor.xyz>2020-12-11 17:48:10 +0100
commit4c37298e054dc1681875a71b181ce0478c577349 (patch)
treed50e941206f4d9dcea6fcb527441a0f2157a6e80
parent67db3838608308576ef28c0259dc0584673d27a8 (diff)
(De)Serialization of the songs
Serialization and Deser now work fine for the songs. We're able to store arrays.
-rw-r--r--Song.java2
-rw-r--r--jMusicHub.java103
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;
}
}
+/**<h2>saveSongs</h2>
+ * saveSongs is used by the command "s"
+ * @param songs ArrayList<Song> Object
+ * */
+ public static void saveSongs(ArrayList<Song> 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");
+ }
+ }
+
+/**<h2>listSongs</h2>
+ * 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<Song> songs = (ArrayList<Song>)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.");