From fe4f2ee0f70b7b35113062538a51bf5444131196 Mon Sep 17 00:00:00 2001 From: Clyhtsuriva Date: Sat, 19 Dec 2020 23:21:05 +0100 Subject: Adding AudioBooks Finishing AudioBook class, adding list and save for the audiobooks. Adding extract method for both audiobooks and songs. Resolving the id incremention issue. --- jMusicHub.java | 222 ++++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 203 insertions(+), 19 deletions(-) (limited to 'jMusicHub.java') diff --git a/jMusicHub.java b/jMusicHub.java index 82b364b..799c715 100644 --- a/jMusicHub.java +++ b/jMusicHub.java @@ -52,6 +52,52 @@ public class jMusicHub { return null; } } + +/**

addAudioBook

+ * add Song is used to add songs thanks to the "c" option + * @param scan Scanner Object + */ + public static AudioBook addAudioBook(Scanner scan){ + System.out.println("Adding an audiobook..."); + System.out.printf("Title : "); + String title = scan.nextLine(); + System.out.printf("Duration (in seconds) : "); + int duration = scan.nextInt(); + System.out.printf("Content path : "); + String trash = scan.nextLine(); //Using this because the content scan is skip after a nexInt + String content = scan.nextLine(); + System.out.printf("Author : "); + String author = scan.nextLine(); + System.out.printf("Language (FRENCH, ENGLISH, ITALIAN, SPANISH, GERMAN) : "); + String choosedLanguage = scan.nextLine(); + choosedLanguage = choosedLanguage.toUpperCase(); + Language language = Language.valueOf(choosedLanguage); + System.out.printf("Category (TEEN, NOVEL, THEATER, SPEECH, DOCUMENTARY) : "); + String choosedCategory = scan.nextLine(); + choosedCategory = choosedCategory.toUpperCase(); + Category category = Category.valueOf(choosedCategory); + System.out.println(""); + + System.out.println("Do you confirm the addition of the following song ?"); + System.out.println("Title : " + title); + System.out.println("Duration : " + duration); + System.out.println("Content path : " + content); + System.out.println("Author : " + author); + System.out.println("Language : " + language); + System.out.println("Category : " + category); + System.out.println("[Y/n]"); + String confirm = scan.nextLine(); + + if (confirm.equalsIgnoreCase("Y")){ //if the user is ok with what he typed, create a audiobook obj + AudioBook newAudioBook = new AudioBook(title, duration, content, author, language, category); + return newAudioBook; + } else { + System.out.println("Aborting..."); + System.out.println(""); + return null; + } + } + /**

saveSongs

* saveSongs is used by the command "s" * @param songs ArrayList Object @@ -73,7 +119,35 @@ public class jMusicHub { out.close(); file.close(); - System.out.println("Your songs have been serialized\n"); + System.out.println("Your songs have been saved.\n"); + } + catch (IOException ex) { + System.out.println("IOException is caught"); + } + } + +/**

saveAudioBooks

+ * saveAudioBooks is used by the command "l" + * @param audiobooks ArrayList Object + * */ + 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"); @@ -96,19 +170,108 @@ public class jMusicHub { @SuppressWarnings("unchecked") // Method for deserialization of object - ArrayList songs = (ArrayList)in.readObject(); + ArrayList songsInFile = (ArrayList)in.readObject(); in.close(); file.close(); - System.out.println("Object has been deserialized"); - for (Song s : songs ){ + System.out.println("\nSaved songs :\n"); + for (Song s : songsInFile ){ System.out.println(s); } } - catch (IOException ex) {System.out.println("IOException is caught");} + catch (IOException ex) {System.out.println("listSongs : Audio file missing.");} catch (ClassNotFoundException ex) {System.out.println("ClassNotFoundException is caught");} } +/**

listAudioBooks

+ * listAudioBooks is called when using the "AB" command in order to list the songs inside the "audiobooks" file. + * */ + + public static void listAudioBooks(){ + String filename="audiobooks"; + + // 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 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 (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. + * */ + + public static ArrayList extractSongs(){ + + // Deserialization + try { + + 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 + + inSong.close(); + songFile.close(); + + return new ArrayList(songs); + //Here will be the number of playlist, albums, songs and audiobook extracted + + + } + 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 + + + } + 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() { @@ -117,30 +280,30 @@ public class jMusicHub { Scanner scan = new Scanner(System.in); String userInput; //Used to get the user's inputs. - ArrayList songs = new ArrayList(); - //used to temporarly save the songs before any "s" command - - System.out.println("Starting extraction"); - System.out.println("Extraction done\n"); - //Here will be the number of playlist, albums, songs and auidobook extracted + ArrayList songs = extractSongs(); + ArrayList audiobooks = extractAudioBooks(); + listSongs(); + listAudioBooks(); do { System.out.println("What do you want to do? [h for help]"); 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\nS: List all your songs\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 saved songs\nAB : List all your saved audiobooks\nh: print this help screen\nq: quit the program\n"); break; - case "q" : + case "q" : //quit System.out.println("Goodbye !"); break; - case "c": + case "c": //add a new song try { //If something goes wrong, abort Song newSong=addSong(scan); if (newSong != null){ + int songsSize = songs.size(); + newSong.setId(songsSize); songs.add(newSong); - System.out.println("Actual content of your songs list (you must save it (s) to do anything else with your songs) :"); + System.out.println("\nActual content of your songs list (you must save it (s) to do anything else with your songs) :"); for (Song iSong : songs){ System.out.println(iSong); } @@ -153,22 +316,43 @@ public class jMusicHub { } break; - case "a": + case "a": //add a new album break; case "+": break; - case "l": + case "l": //add a new audiobook + try { //If something goes wrong, abort + AudioBook newAudioBook=addAudioBook(scan); + if (newAudioBook != null){ + int audiobookSize = audiobooks.size(); + newAudioBook.setId(audiobookSize); + audiobooks.add(newAudioBook); + System.out.println("\nActual content of your audiobook list (you must save it (s) to do anything else with your audiobooks) :"); + for (AudioBook iAudioBook : audiobooks){ + System.out.println(iAudioBook); + } + System.out.println(""); + + } + } catch (InputMismatchException | IllegalArgumentException e) { + System.out.println("You typed something wrong... I'm aborting.."); + System.out.println(""); + } break; case "p": break; case "-": break; - case "s": + case "s": //save songs, audiobooks, playlist and albums saveSongs(songs); + saveAudioBooks(audiobooks); break; - case "S": + case "S": //print songs contained in songs listSongs(); break; + case "AB": //print audiobooks containd in audiobooks + listAudioBooks(); + break; default : System.out.println("Unknown command. Type h for help."); -- cgit v1.2.3