aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorClyhtsuriva <aimeric@adjutor.xyz>2020-12-22 18:38:34 +0100
committerClyhtsuriva <aimeric@adjutor.xyz>2020-12-22 18:38:34 +0100
commit6c06acde8fb51125e925a34b8adf428ccfb37055 (patch)
tree0df92aa09f74585c5f2bc2582fe42e39c232e12c
parentfe4f2ee0f70b7b35113062538a51bf5444131196 (diff)
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.
-rw-r--r--Song.java4
-rw-r--r--jMusicHub.java206
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 @@
+/**<h1>Song<h1>
+ * 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 {
}
/** <h2>addAudioBook</h2>
- * 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 {
}
}
-/**<h2>saveSongs</h2>
- * saveSongs is used by the command "s"
- * @param songs ArrayList<Song> Object
+/**<h2>save</h2>
+ * save is used by the command "s"
+ * @param filename String
+ * @param elements ArrayList
* */
- public static void saveSongs(ArrayList<Song> 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");
}
}
-/**<h2>saveAudioBooks</h2>
- * saveAudioBooks is used by the command "l"
- * @param audiobooks ArrayList<AudioBook> Object
+/**<h2>list</h2>
+ * 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<AudioBook> 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");
- }
- }
-
-/**<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";
+ 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<Song> songsInFile = (ArrayList<Song>)in.readObject();
+ switch(filename) {
+ case "songs":
+ @SuppressWarnings("unchecked")
+ ArrayList<Song> songs = (ArrayList<Song>)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");
-/**<h2>listAudioBooks</h2>
- * 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<AudioBook> audiobooks = (ArrayList<AudioBook>)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<AudioBook> audiobooks = (ArrayList<AudioBook>)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");}
}
-/**<h2>extractSongs</h2>
- * Extraction is used to print the content of the files and put them in the ArrayList used to add elements.
+/**<h2>extract</h2>
+ * 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<Song> 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<Song> songs = (ArrayList<Song>)inSong.readObject();
- //used to temporarly save the songs before any "s" command
+ switch (filename) {
+ case "songs" :
+ @SuppressWarnings("unchecked")
+ ArrayList<Song> songs = (ArrayList<Song>)in.readObject();
- inSong.close();
- songFile.close();
+ in.close();
+ file.close();
- return new ArrayList<Song>(songs);
- //Here will be the number of playlist, albums, songs and audiobook extracted
+ return new ArrayList<Song>(songs);
+ case "audiobooks" :
+ @SuppressWarnings("unchecked")
+ ArrayList<AudioBook> audiobooks = (ArrayList<AudioBook>)in.readObject();
+ in.close();
+ file.close();
+ return new ArrayList<AudioBook>(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<Song>() ;}
- catch (ClassNotFoundException ex) {System.out.println("ClassNotFoundException is caught");return null;}
-}
-
-/**<h2>extractAudioBooks</h2>
- * Extraction is used to print the content of the files and put them in the ArrayList used to add elements.
- * */
-
- public static ArrayList<AudioBook> extractAudioBooks(){
-
- // Deserialization
- try {
-
- String audiobookFileName="audiobooks";
- FileInputStream audiobookFile = new FileInputStream(audiobookFileName);
- ObjectInputStream inAudioBook = new ObjectInputStream(audiobookFile);
-
- @SuppressWarnings("unchecked")
- ArrayList<AudioBook> audiobooks = (ArrayList<AudioBook>)inAudioBook.readObject();
-
- inAudioBook.close();
- audiobookFile.close();
-
- return new ArrayList<AudioBook>(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<AudioBook>() ;}
- 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<Song> songs = extractSongs();
- ArrayList<AudioBook> audiobooks = extractAudioBooks();
- listSongs();
- listAudioBooks();
+ System.out.println("Extraction beginning...");
+ @SuppressWarnings("unchecked")
+ ArrayList<Song> songs = extract("songs");
+ @SuppressWarnings("unchecked")
+ ArrayList<AudioBook> 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.");