aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorClyhtsuriva <aimeric@adjutor.xyz>2020-12-23 18:05:00 +0100
committerClyhtsuriva <aimeric@adjutor.xyz>2020-12-23 18:05:00 +0100
commitbec74db10232efaa6d1456df4d01924f0313a0e9 (patch)
treef14f725f46f4760c381f4a29a7b041c6478bdca7
parent6c06acde8fb51125e925a34b8adf428ccfb37055 (diff)
Implementing Album
Adding list, extract and save support to albums as well as addAlbum for the albums. Starting the method to add songs to an album as well.
-rw-r--r--Album.java9
-rw-r--r--MusicalElement.java1
-rw-r--r--jMusicHub.java162
3 files changed, 155 insertions, 17 deletions
diff --git a/Album.java b/Album.java
index 34fae7a..5035398 100644
--- a/Album.java
+++ b/Album.java
@@ -2,14 +2,14 @@ import java.util.*;
import java.io.Serializable;
public class Album implements Serializable {
+ private static final long serialVersionUID = -8678385322105507976L;
private int id, duration;
private Date date;
private String title, artist;
private ArrayList<Song> songs = new ArrayList<Song>();
- public Album(int id, String title, int duration, String artist, Date date, ArrayList<Song> songs) {
- this.id=id;
+ public Album(String title, int duration, String artist, Date date, ArrayList<Song> songs) {
this.title=title;
this.duration=this.getDuration();
this.artist=artist;
@@ -30,10 +30,13 @@ public class Album implements Serializable {
public ArrayList<Song> getSongs(){return songs;}
- public void setId(){this.id=id;}
+ public void setId(int id){this.id=id;}
public void setTitle(){this.title=title;}
public void setArtist(){this.artist=artist;}
public void setDate(){this.date=date;}
+ public String toString() {
+ return "Id : "+getId()+"\nTitle : "+getTitle()+"\nDuration : "+getDuration()+"\nArtist : "+getArtist()+"\nDate of release : "+getDate()+"\nSongs : "+getSongs();
+}
}
diff --git a/MusicalElement.java b/MusicalElement.java
index e748281..9094913 100644
--- a/MusicalElement.java
+++ b/MusicalElement.java
@@ -11,7 +11,6 @@ public abstract class MusicalElement implements Serializable {
//Our vars
private int id, duration;
private String title, content;
- private static int idCount=0;
public MusicalElement(String title, int duration, String content) {
diff --git a/jMusicHub.java b/jMusicHub.java
index 56cf38c..c70c9b8 100644
--- a/jMusicHub.java
+++ b/jMusicHub.java
@@ -1,5 +1,6 @@
import java.util.*;
import java.io.*;
+import java.text.*;
/** <h1>jMusicHub</h1>
*
@@ -14,7 +15,7 @@ import java.io.*;
public class jMusicHub {
/** <h2>addSong</h2>
- * add Song is used to add songs thanks to the "c" option
+ * addSong is used to add songs thanks to the "c" option
* @param scan Scanner Object
*/
public static Song addSong(Scanner scan){
@@ -54,7 +55,7 @@ public class jMusicHub {
}
/** <h2>addAudioBook</h2>
- * add Song is used to add songs thanks to the "l" option
+ * addAudioBook is used to add songs thanks to the "l" option
* @param scan Scanner Object
*/
public static AudioBook addAudioBook(Scanner scan){
@@ -98,8 +99,52 @@ public class jMusicHub {
}
}
+/** <h2>addAlbum</h2>
+ * addAlbum is used to add songs 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 song ?");
+ 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<Song> albumSongs = new ArrayList<Song>();
+ 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;
+ }
+ }
+
/**<h2>save</h2>
* 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
* */
@@ -127,7 +172,7 @@ public class jMusicHub {
}
/**<h2>list</h2>
- * list is called when using the "AB" and "S" commands in order to list the elements in respective files.
+ * list is called when using the "AB", "S", "A" and "P" commands in order to list the elements in respective files.
* @param filename String
* */
@@ -139,8 +184,6 @@ public class jMusicHub {
FileInputStream file = new FileInputStream(filename);
ObjectInputStream in = new ObjectInputStream(file);
-
- // Method for deserialization of object
switch(filename) {
case "songs":
@SuppressWarnings("unchecked")
@@ -165,7 +208,30 @@ public class jMusicHub {
for (AudioBook ab : audiobooks ){
System.out.println(ab);
}
+ break;
+ case "albums" :
+ @SuppressWarnings("unchecked")
+ ArrayList<Album> albums = (ArrayList<Album>)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<Playlist> playlists = (ArrayList<Playlist>)in.readObject();
+ in.close();
+ file.close();
+ System.out.println("\nSaved "+filename+" :\n");
+
+ for (Playlist p : playlists ){
+ System.out.println(p);
+ }
break;
}
@@ -185,8 +251,8 @@ public class jMusicHub {
// Deserialization
try {
- FileInputStream file = new FileInputStream(filename);
- ObjectInputStream in = new ObjectInputStream(file);
+ FileInputStream file = new FileInputStream(filename);
+ ObjectInputStream in = new ObjectInputStream(file);
switch (filename) {
case "songs" :
@@ -196,7 +262,9 @@ public class jMusicHub {
in.close();
file.close();
+ list("songs");
return new ArrayList<Song>(songs);
+
case "audiobooks" :
@SuppressWarnings("unchecked")
ArrayList<AudioBook> audiobooks = (ArrayList<AudioBook>)in.readObject();
@@ -204,7 +272,29 @@ public class jMusicHub {
in.close();
file.close();
+ list("audiobooks");
return new ArrayList<AudioBook>(audiobooks);
+
+ case "albums" :
+ @SuppressWarnings("unchecked")
+ ArrayList<Album> albums = (ArrayList<Album>)in.readObject();
+
+ in.close();
+ file.close();
+
+ list("albums");
+ return new ArrayList<Album>(albums);
+
+ case "playlists" :
+ @SuppressWarnings("unchecked")
+ ArrayList<Playlist> playlists = (ArrayList<Playlist>)in.readObject();
+
+ in.close();
+ file.close();
+
+ list("playlists");
+ return new ArrayList<Playlist>(playlists);
+
default :
return new ArrayList();
}
@@ -216,6 +306,22 @@ public class jMusicHub {
}
}
+ public static void addSongs(Scanner scan, ArrayList<Album> albums){
+ 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) {
+ if ( albumId == a.getId() ) {
+ System.out.println(a);
+ }
+ }
+ }
+
public jMusicHub() {
System.out.println("Welcome to the jMusicHub !\n");
@@ -228,9 +334,11 @@ public class jMusicHub {
ArrayList<Song> songs = extract("songs");
@SuppressWarnings("unchecked")
ArrayList<AudioBook> audiobooks = extract("audiobooks");
+ @SuppressWarnings("unchecked")
+ ArrayList<Album> albums = extract("albums");
+ @SuppressWarnings("unchecked")
+ ArrayList<Playlist> playlists = extract("playlists");
- list("songs");
- list("audiobooks");
System.out.println("\nExtraction done.");
do {
@@ -238,7 +346,7 @@ public class jMusicHub {
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\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\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 !");
@@ -265,15 +373,34 @@ public class jMusicHub {
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 "+":
+ case "+": //add an existing song to an album
+ addSongs(scan, albums);
break;
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);
+ 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){
@@ -294,6 +421,8 @@ public class jMusicHub {
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");
@@ -301,6 +430,13 @@ public class jMusicHub {
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.");