import java.util.*;
import java.io.*;
import java.text.*;
/* TO DO :
* Creating a playlist with musics and audiobooks (p)
* Deleting playlists (-)
* Printing the songs of a chosen album
* Printing album sorted by date of release
* Printing album sorted by its songs genre
* Printing plyalist sorted by name
* Printing audiobooks sorted by author
* Generating the javadoc
* Doing the UML Diagramm
* Doing the report
* Sending in the whole thing
* */
/**
jMusicHub
*
* The jMusicHub class is basically the app.
* It is used to launch the whole process.
*
* @author Aimeric ADJUTOR
* @version 1.0
* @since 2020-11-13
* */
public class jMusicHub {
/** addSong
* addSong is used to add songs thanks to the "c" option
* @param scan Scanner Object
*/
public static Song addSong(Scanner scan){
System.out.println("Adding a song...");
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("Artist : ");
String artist = scan.nextLine();
System.out.printf("Genre (JAZZ, CLASSICAL, HIPHOP, ROCK, POP, RAP) : ");
String choosedGenre = scan.nextLine();
choosedGenre = choosedGenre.toUpperCase();
Genre genre = Genre.valueOf(choosedGenre);
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("Artist : " + artist);
System.out.println("Genre : " + genre);
System.out.println("[Y/n]");
String confirm = scan.nextLine();
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;
} else {
System.out.println("Aborting...");
System.out.println("");
return null;
}
}
/** addAudioBook
* addAudioBook is used to add songs thanks to the "l" 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 audiobook ?");
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;
}
}
/** addAlbum
* addAlbum is used to add albums thanks to the "a" option
* @param scan Scanner Object
*/
public static Album addAlbum(Scanner scan){
System.out.println("\n[DISCLAIMER : You have to register the songs after creating (a) and saving (s) the albums]\n");
System.out.println("Adding an album...");
System.out.printf("Title : ");
String title = scan.nextLine();
System.out.printf("Artist : ");
String artist = scan.nextLine();
System.out.printf("Date (yyyy/MM/dd) : ");
String dateString = scan.nextLine();
DateFormat df = new SimpleDateFormat("yyyy/MM/dd");
try {
Date date = df.parse(dateString);
System.out.println("");
System.out.println("Do you confirm the addition of the following album ?");
System.out.println("Title : " + title);
System.out.println("Artist : " + artist);
System.out.println("Date of release : " + date);
System.out.println("[Y/n]");
String confirm = scan.nextLine();
if (confirm.equalsIgnoreCase("Y")){ //if the user is ok with what he typed, create an album obj
ArrayList albumSongs = new ArrayList();
Album newAlbum = new Album(title, 0, artist, date, albumSongs);
return newAlbum;
} else {
System.out.println("Aborting...");
System.out.println("");
return null;
}
} catch (ParseException e) {
System.out.println("Aborting...");
System.out.println("");
return null;
}
}
/** addPlaylist
* addPlaylist is used to add playlist thanks to the "p" option
* @param scan Scanner Object
*/
public static Playlist addPlaylist(Scanner scan, ArrayList songs, ArrayList audiobooks){
String userInput;
System.out.println("Adding a playlist...");
System.out.printf("Name : ");
String name = scan.nextLine();
ArrayList playlistSongs = new ArrayList();
ArrayList playlistAudioBooks = new ArrayList();
System.out.println("\n[You'll have to add each song or audiobook element by element]\n");
do {
System.out.println("What do you want to add to this playlist ?\n[(s)ong/(a)udiobook/(q)uit]\n");
userInput = scan.nextLine();
switch(userInput) {
case "s" :
System.out.printf("Song ID : ");
int songId = scan.nextInt();
String trash = scan.nextLine();
for (Song s : songs) { //looking for the right song
if ( songId == s.getId() ) {
playlistSongs.add(s);
}
}
break;
case "a" :
System.out.printf("Album ID : ");
int audiobookId = scan.nextInt();
trash = scan.nextLine();
for (AudioBook a : audiobooks) { //looking for the right song
if ( audiobookId == a.getId() ) {
playlistAudioBooks.add(a);
}
}
break;
default :
System.out.println("Unknown command.");
}
} while(!userInput.equals("q"));
System.out.println("Do you confirm the addition of the following playlist ?");
System.out.println("Name : " + name);
System.out.println("[Y/n]");
String confirm = scan.nextLine();
if (confirm.equalsIgnoreCase("Y")){ //if the user is ok with what he typed, create a playlist obj
Playlist newPlaylist = new Playlist(name, playlistSongs, playlistAudioBooks);
return newPlaylist;
} else {
System.out.println("Aborting...");
System.out.println("");
return null;
}
}
/**save
* save is used by the command "s"
* It is used to serialize (save) arrays of a list into the type's file.i
* Exemple : save("nameOfTheFile", elements)
* It will save the arrayList named elements into nameOfTheFile in the working directory.
* @param filename String
* @param elements ArrayList
* */
@SuppressWarnings ("rawtypes")
public static void save(String filename, ArrayList elements){
// 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(elements);
out.close();
file.close();
System.out.println("Your " + filename + " have been saved.");
}
catch (IOException ex) {
System.out.println("IOException is caught");
}
}
/**list
* list is called when using the "AB", "S", "A" and "P" commands in order to list the elements in respective files.
* @param filename String
* */
public static void list(String filename){
// Deserialization
try {
// Reading the object from a file
FileInputStream file = new FileInputStream(filename);
ObjectInputStream in = new ObjectInputStream(file);
switch(filename) {
case "songs":
@SuppressWarnings("unchecked")
ArrayList songs = (ArrayList)in.readObject();
in.close();
file.close();
System.out.println("\nSaved "+filename+" :\n");
for (Song s : songs ){
System.out.println(s);
}
break;
case "audiobooks" :
@SuppressWarnings("unchecked")
ArrayList audiobooks = (ArrayList)in.readObject();
in.close();
file.close();
System.out.println("\nSaved "+filename+" :\n");
for (AudioBook ab : audiobooks ){
System.out.println(ab);
}
break;
case "albums" :
@SuppressWarnings("unchecked")
ArrayList albums = (ArrayList)in.readObject();
in.close();
file.close();
System.out.println("\nSaved "+filename+" :\n");
for (Album a : albums ){
System.out.println(a);
}
break;
case "playlists" :
@SuppressWarnings("unchecked")
ArrayList playlists = (ArrayList)in.readObject();
in.close();
file.close();
System.out.println("\nSaved "+filename+" :\n");
for (Playlist p : playlists ){
System.out.println(p);
}
break;
}
}
catch (IOException ex) {System.out.println(filename+ " file missing.");}
catch (ClassNotFoundException ex) {System.out.println("ClassNotFoundException is caught");}
}
/**extract
* Extract is used to print the content of the files and put them in the ArrayList used to add elements.
* @param String filename
* */
@SuppressWarnings("rawtypes")
public static ArrayList extract(String filename){
// Deserialization
try {
FileInputStream file = new FileInputStream(filename);
ObjectInputStream in = new ObjectInputStream(file);
switch (filename) {
case "songs" :
@SuppressWarnings("unchecked")
ArrayList songs = (ArrayList)in.readObject();
in.close();
file.close();
list("songs");
return new ArrayList(songs);
case "audiobooks" :
@SuppressWarnings("unchecked")
ArrayList audiobooks = (ArrayList)in.readObject();
in.close();
file.close();
list("audiobooks");
return new ArrayList(audiobooks);
case "albums" :
@SuppressWarnings("unchecked")
ArrayList albums = (ArrayList)in.readObject();
in.close();
file.close();
list("albums");
return new ArrayList(albums);
case "playlists" :
@SuppressWarnings("unchecked")
ArrayList playlists = (ArrayList)in.readObject();
in.close();
file.close();
list("playlists");
return new ArrayList(playlists);
default :
return new ArrayList();
}
}
catch (IOException ex) { //mainly occur bc file is missing, leading to an empty ListArray
System.out.println("\n"+filename+" file missing.\n");
return new ArrayList() ;}
catch (ClassNotFoundException ex) {System.out.println("ClassNotFoundException is caught");return null;
}
}
public static void addSongToAlbum(Scanner scan, ArrayList albums, ArrayList songs){
System.out.println("Adding a existing song to an existing album...");
System.out.printf("Song's ID : ");
int songId = scan.nextInt();
System.out.printf("Album's ID : ");
String trash = scan.nextLine(); //Using this because the content scan is skip after a nexInt
int albumId = scan.nextInt();
trash = scan.nextLine();
for (Album a : albums) { //looking for the right album
if ( albumId == a.getId() ) {
for ( Song s : songs ){ //as we've found the right album, we're now looking for the right song.
if ( songId == s.getId() ){
System.out.println("\nAdding "+s.getTitle()+" from "+s.getArtist()+" to "+a.getTitle()+"\n");
a.addSong(s);
}
}
}
}
}
public jMusicHub() {
System.out.println("Welcome to the jMusicHub !\n");
Scanner scan = new Scanner(System.in);
String userInput; //Used to get the user's inputs.
System.out.println("Extraction beginning...");
@SuppressWarnings("unchecked")
ArrayList songs = extract("songs");
@SuppressWarnings("unchecked")
ArrayList audiobooks = extract("audiobooks");
@SuppressWarnings("unchecked")
ArrayList albums = extract("albums");
@SuppressWarnings("unchecked")
ArrayList playlists = extract("playlists");
System.out.println("\nExtraction done.");
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 saved songs\nAB : List all your saved audiobooks\nA : List all your saved albums\nP : List all your saved playlists\nh: print this help screen\nq: quit the program\n");
break;
case "q" : //quit
System.out.println("Goodbye !");
break;
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("\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);
}
System.out.println("");
}
} catch (InputMismatchException | IllegalArgumentException e) {
System.out.println("You typed something wrong... I'm aborting..");
System.out.println("");
}
break;
case "a": //add a new album
try { //If something goes wrong, abort
Album newAlbum = addAlbum(scan);
if (newAlbum != null){
int albumsSize = albums.size();
newAlbum.setId(albumsSize);
albums.add(newAlbum);
System.out.println("\nActual content of your albums list (you must save it (s) to do anything else with them, like adding songs) :");
for (Album iAlbum : albums){
System.out.println(iAlbum);
}
System.out.println("");
}
} catch (InputMismatchException | IllegalArgumentException e) {
System.out.println("You typed something wrong... I'm aborting..");
System.out.println("");
}
break;
case "+": //add an existing song to an album
addSongToAlbum(scan, albums, songs);
break;
case "l": //add a new audiobook
try { //If something goes wrong, abort
AudioBook newAudioBook=addAudioBook(scan);
if (newAudioBook != null){
int audiobooksSize = audiobooks.size();
newAudioBook.setId(audiobooksSize);
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": //save songs, audiobooks, playlist and albums
save("songs", songs);
save("audiobooks", audiobooks);
save("albums", albums);
save("playlists", playlists);
break;
case "S": //print songs contained in songs
list("songs");
break;
case "AB": //print audiobooks containd in audiobooks
list("audiobooks");
break;
case "A": //print albums
list("albums");
break;
case "P": //print albums
list("playlists");
break;
default :
System.out.println("Unknown command. Type h for help.");
}
} while(!userInput.equals("q"));
}
public static void main(String[] args) {
new jMusicHub();
}
}