aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Playlist.java4
-rw-r--r--albumsbin0 -> 673 bytes
-rw-r--r--audiobooksbin470 -> 297 bytes
-rw-r--r--jMusicHub.java164
-rw-r--r--playlistsbin0 -> 968 bytes
-rw-r--r--songsbin460 -> 379 bytes
6 files changed, 128 insertions, 40 deletions
diff --git a/Playlist.java b/Playlist.java
index 03f412f..0bf55fd 100644
--- a/Playlist.java
+++ b/Playlist.java
@@ -16,11 +16,11 @@ public class Playlist implements Serializable {
}
public int getId(){return id;}
- public String getName(){return name;}
+ public String getName(){return name.toUpperCase();}
public ArrayList<Song> getSongs(){return songs;}
public ArrayList<AudioBook> getAudioBooks(){return audiobooks;}
- public void setID(int id){this.id=id;}
+ public void setId(int id){this.id=id;}
public void setName(){this.name=name;}
public String toString() {
diff --git a/albums b/albums
new file mode 100644
index 0000000..e72f50b
--- /dev/null
+++ b/albums
Binary files differ
diff --git a/audiobooks b/audiobooks
index 4a6b105..da41cf0 100644
--- a/audiobooks
+++ b/audiobooks
Binary files differ
diff --git a/jMusicHub.java b/jMusicHub.java
index 90e5e44..715f85f 100644
--- a/jMusicHub.java
+++ b/jMusicHub.java
@@ -2,12 +2,9 @@ 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
@@ -168,29 +165,37 @@ public class jMusicHub {
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");
+ System.out.println("What do you want to add to this playlist ?\n[(s)ong/audio(b)ook/(q)uit]\n");
userInput = scan.nextLine();
switch(userInput) {
case "s" :
- System.out.printf("Song ID : ");
+ listById("songs");
+ System.out.printf("\nSong 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);
+ System.out.println("\nAdding the song "+s.getTitle()+" from "+s.getArtist()+" to the playlist "+name+"\n");
}
}
break;
- case "a" :
- System.out.printf("Album ID : ");
+ case "b" :
+ listById("audiobooks");
+ System.out.printf("\nAudioBook ID : ");
int audiobookId = scan.nextInt();
trash = scan.nextLine();
- for (AudioBook a : audiobooks) { //looking for the right song
- if ( audiobookId == a.getId() ) {
- playlistAudioBooks.add(a);
+ for (AudioBook b : audiobooks) { //looking for the right song
+ if ( audiobookId == b.getId() ) {
+ playlistAudioBooks.add(b);
+ System.out.println("\nAdding the audiobook "+b.getTitle()+" from "+b.getAuthor()+" to the playlist "+name+"\n");
+
}
}
break;
+ case "q" :
+ System.out.println("Quitting the addition process...\n");
+ break;
default :
System.out.println("Unknown command.");
}
@@ -243,12 +248,12 @@ public class jMusicHub {
}
}
-/**<h2>list</h2>
+/**<h2>listById</h2>
* 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){
+ public static void listById(String filename){
// Deserialization
try {
@@ -314,6 +319,30 @@ public class jMusicHub {
catch (ClassNotFoundException ex) {System.out.println("ClassNotFoundException is caught");}
}
+ public static void listPlaylistsByName(){
+ String filename = "playlists";
+ try {
+ // Reading the object from a file
+ FileInputStream file = new FileInputStream(filename);
+ ObjectInputStream in = new ObjectInputStream(file);
+
+ @SuppressWarnings("unchecked")
+ ArrayList<Playlist> playlists = (ArrayList<Playlist>)in.readObject();
+ in.close();
+ file.close();
+
+ Collections.sort(playlists, Comparator.comparing(Playlist::getName));
+
+ System.out.println("\nSaved "+filename+", sorted by name :\n");
+ for (Playlist p : playlists ){
+ System.out.println(p);
+ }
+ }
+ catch (IOException ex) {System.out.println(filename+ " file missing.");}
+ catch (ClassNotFoundException ex) {System.out.println("ClassNotFoundException is caught");}
+ }
+
+
/**<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
@@ -334,7 +363,7 @@ public class jMusicHub {
in.close();
file.close();
- list("songs");
+ listById("songs");
return new ArrayList<Song>(songs);
case "audiobooks" :
@@ -344,7 +373,7 @@ public class jMusicHub {
in.close();
file.close();
- list("audiobooks");
+ listById("audiobooks");
return new ArrayList<AudioBook>(audiobooks);
case "albums" :
@@ -354,7 +383,7 @@ public class jMusicHub {
in.close();
file.close();
- list("albums");
+ listById("albums");
return new ArrayList<Album>(albums);
case "playlists" :
@@ -364,7 +393,7 @@ public class jMusicHub {
in.close();
file.close();
- list("playlists");
+ listById("playlists");
return new ArrayList<Playlist>(playlists);
default :
@@ -380,9 +409,11 @@ public class jMusicHub {
public static void addSongToAlbum(Scanner scan, ArrayList<Album> albums, ArrayList<Song> songs){
System.out.println("Adding a existing song to an existing album...");
- System.out.printf("Song's ID : ");
+ listById("songs");
+ System.out.printf("\nSong's ID : ");
int songId = scan.nextInt();
- System.out.printf("Album's ID : ");
+ listById("albums");
+ System.out.printf("\nAlbum's ID : ");
String trash = scan.nextLine(); //Using this because the content scan is skip after a nexInt
int albumId = scan.nextInt();
trash = scan.nextLine();
@@ -399,6 +430,33 @@ public class jMusicHub {
}
}
+ public static ArrayList<Playlist> delPlaylist(ArrayList<Playlist> playlists){
+ Scanner scan = new Scanner(System.in);
+ listById("playlists");
+ System.out.println("\nWhich one do you want to delete ?");
+ System.out.printf("Playlist's ID : ");
+ int playlistId = scan.nextInt();
+ String trash = scan.nextLine();
+ for ( Playlist p : playlists ){
+ if ( playlistId == p.getId() ){
+ int index = playlists.indexOf(p);
+ System.out.println("Do you confirm the removal of the following playlist ?");
+ System.out.println("Id : "+p.getId()+"\nName : " + p.getName());
+ System.out.println("[Y/n]");
+ String confirm = scan.nextLine();
+ if (confirm.equalsIgnoreCase("Y")){
+ System.out.println("Removing...");
+ playlists.remove(index);
+ } else {
+ System.out.println("Aborting...");
+ System.out.println("");
+ }
+ break;
+ }
+ }
+ return playlists;
+ }
+
public jMusicHub() {
System.out.println("Welcome to the jMusicHub !\n");
@@ -422,9 +480,11 @@ public class jMusicHub {
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");
+ 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\n\np: create a new playlist from existing songs and audiobooks\n-: delete a playlist\n\ns: save playlists, albums, songs and audiobooks into the concerned files\n\nS: List all your saved songs\nB : List all your saved audiobooks\nA : List all your saved albums\nP : List all your saved playlists\n\nPS : Print playlists sorted by their name\n\nh: print this help screen\nq: quit the program\n");
break;
+
case "q" : //quit
System.out.println("Goodbye !");
break;
@@ -436,12 +496,11 @@ public class jMusicHub {
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) :");
+ System.out.println("\nActual content of your list (you must save it (s) to do anything else with them) :");
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..");
@@ -456,12 +515,11 @@ public class jMusicHub {
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) :");
+ System.out.println("\nActual content of your 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..");
@@ -469,9 +527,11 @@ public class jMusicHub {
}
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);
@@ -479,21 +539,46 @@ public class jMusicHub {
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) :");
+ System.out.println("\nActual content of your list (you must save it (s) to do anything else with them) :");
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":
+
+ case "p": //adding a playlist with songs and audiobooks
+ try { //If something goes wrong, abort
+ Playlist newPlaylist=addPlaylist(scan, songs, audiobooks);
+ if (newPlaylist != null){
+ int playlistsSize = playlists.size();
+ Playlist lastObject=playlists.get(playlistsSize-1);
+ int idOfLastObject = lastObject.getId();
+ newPlaylist.setId(idOfLastObject+1);
+ playlists.add(newPlaylist);
+ System.out.println("\nActual content of your list (you must save it (s) to do anything else with them) :");
+ for (Playlist iPlaylist : playlists){
+ System.out.println(iPlaylist);
+ }
+ System.out.println("");
+ }
+ } catch (InputMismatchException | IllegalArgumentException e) {
+ System.out.println("You typed something wrong... I'm aborting..");
+ System.out.println("");
+ }
break;
+
case "-":
+ try {
+ playlists=delPlaylist(playlists);
+ save("playlists", playlists);
+ } catch (InputMismatchException | IllegalArgumentException e) {
+ System.out.println("You typed something wrong... I'm aborting..");
+ }
break;
case "s": //save songs, audiobooks, playlist and albums
save("songs", songs);
@@ -501,27 +586,30 @@ public class jMusicHub {
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");
+
+ case "S": //print songs contained in songs sorted by id
+ listById("songs");
break;
- case "A": //print albums
- list("albums");
+
+ case "B": //print audiobooks containd in audiobooks sorted by id
+ listById("audiobooks");
break;
- case "P": //print albums
- list("playlists");
+
+ case "A": //print albums sorted by id
+ listById("albums");
break;
+ case "P": //print playlists sorted by id
+ listById("playlists");
+ break;
+ case "PS": //print playslists sorted by their name
+ listPlaylistsByName();
+ break;
default :
System.out.println("Unknown command. Type h for help.");
-
}
} while(!userInput.equals("q"));
-
}
-
public static void main(String[] args) {
new jMusicHub();
}
diff --git a/playlists b/playlists
new file mode 100644
index 0000000..e201e98
--- /dev/null
+++ b/playlists
Binary files differ
diff --git a/songs b/songs
index df468f2..2208b77 100644
--- a/songs
+++ b/songs
Binary files differ