diff options
-rw-r--r-- | AudioBook.java | 10 | ||||
-rw-r--r-- | MusicalElement.java | 3 | ||||
-rw-r--r-- | Pair.java | 11 | ||||
-rw-r--r-- | Song.java | 27 | ||||
-rw-r--r-- | audiobooks | bin | 0 -> 470 bytes | |||
-rw-r--r-- | jMusicHub.java | 222 | ||||
-rw-r--r-- | songs | bin | 0 -> 460 bytes |
7 files changed, 225 insertions, 48 deletions
diff --git a/AudioBook.java b/AudioBook.java index 03c6b0e..9f6ba56 100644 --- a/AudioBook.java +++ b/AudioBook.java @@ -1,5 +1,9 @@ public class AudioBook extends MusicalElement { + + private static final long serialVersionUID = -7145972304319088676L; + private String author, language, category; + public AudioBook(String title, int duration, String content, String author, Language language, Category category) { super(title, duration, content); this.author=author; @@ -15,4 +19,10 @@ public class AudioBook extends MusicalElement { public void setGenre(Language language){this.language=language.name();} public void setCategory(Category category){this.category=category.name();} + + +public String toString() { + return "Id : "+getId()+"\nTitle : "+getTitle()+"\nDuration : "+getDuration()+"\nContent : "+getContent()+"\nAuthor : "+getAuthor()+"\nLanguage : "+getLanguage()+"\nCategory : "+getCategory(); +} + } diff --git a/MusicalElement.java b/MusicalElement.java index 9dbb044..e748281 100644 --- a/MusicalElement.java +++ b/MusicalElement.java @@ -1,6 +1,6 @@ import java.io.Serializable; -/** The MusicalElement contains the base of songs and audibooks. +/** The MusicalElement contains the base of songs and audiobooks. * It is the abstract class they will take. * We use the classical get and set here. * Get is used to return the said value. @@ -15,7 +15,6 @@ public abstract class MusicalElement implements Serializable { public MusicalElement(String title, int duration, String content) { - this.setId(idCount++); this.title=title; this.duration=duration; this.content=content; diff --git a/Pair.java b/Pair.java new file mode 100644 index 0000000..d7648d0 --- /dev/null +++ b/Pair.java @@ -0,0 +1,11 @@ +public class Pair<S, T> { + public final S x; + public final T y; + + public Pair(S x, T y) { + this.x = x; + this.y = y; + } + public S getX(){return x;} + public T getY(){return y;} +} @@ -16,32 +16,5 @@ public class Song extends MusicalElement{ public String toString() { return "Id : "+getId()+"\nTitle : "+getTitle()+"\nDuration : "+getDuration()+"\nContent : "+getContent()+"\nArtist : "+getArtist()+"\nGenre : "+getGenre(); -// return getTitle()+";"+getDuration()+";"+getContent()+";"+getArtist()+";"+getGenre(); } - -/* public static void save(){ - String filename="songs.csv"; - try - { - //Saving of object in a file - FileOutputStream file = new FileOutputStream(filename); - ObjectOutputStream out = new ObjectOutputStream(file); - - // Method for serialization of object - out.writeObject(this); - - out.close(); - file.close(); - - System.out.println("Object has been serialized"); - - } - - catch(IOException ex) - { - System.out.println("IOException is caught"); - } - }*/ - - } diff --git a/audiobooks b/audiobooks Binary files differnew file mode 100644 index 0000000..4a6b105 --- /dev/null +++ b/audiobooks 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; } } + +/** <h2>addAudioBook</h2> + * 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; + } + } + /**<h2>saveSongs</h2> * saveSongs is used by the command "s" * @param songs ArrayList<Song> 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"); + } + } + +/**<h2>saveAudioBooks</h2> + * saveAudioBooks is used by the command "l" + * @param audiobooks ArrayList<AudioBook> Object + * */ + 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"); @@ -96,19 +170,108 @@ public class jMusicHub { @SuppressWarnings("unchecked") // Method for deserialization of object - ArrayList<Song> songs = (ArrayList<Song>)in.readObject(); + ArrayList<Song> songsInFile = (ArrayList<Song>)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");} } +/**<h2>listAudioBooks</h2> + * 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<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 (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. + * */ + + public static ArrayList<Song> extractSongs(){ + + // Deserialization + try { + + 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 + + inSong.close(); + songFile.close(); + + return new ArrayList<Song>(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<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 + + + } + 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() { @@ -117,30 +280,30 @@ public class jMusicHub { Scanner scan = new Scanner(System.in); String userInput; //Used to get the user's inputs. - ArrayList<Song> songs = new ArrayList<Song>(); - //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<Song> songs = extractSongs(); + ArrayList<AudioBook> 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."); |