diff options
author | Clyhtsuriva <aimeric@adjutor.xyz> | 2020-12-28 13:33:43 +0100 |
---|---|---|
committer | Clyhtsuriva <aimeric@adjutor.xyz> | 2020-12-28 13:33:43 +0100 |
commit | 61768eade51a08d0036156a05bd83311dc1b6299 (patch) | |
tree | 05bcf5a8b325751c659dbdf7c1079ca663f20dd7 | |
parent | 3b58bb1f300f0b8a951ccdcf1143d2d99612cc92 (diff) |
Added method to display albums sorted by genre
-rw-r--r-- | Album.java | 6 | ||||
-rw-r--r-- | jMusicHub.java | 32 |
2 files changed, 33 insertions, 5 deletions
@@ -33,7 +33,11 @@ public class Album implements Serializable { } } - + public String getGenre(){ //used for AG + Song firstSong=this.songs.get(0); + String firstSongGenre = firstSong.getGenre(); + return firstSongGenre; + }; public void setId(int id){this.id=id;} public void setTitle(){this.title=title;} diff --git a/jMusicHub.java b/jMusicHub.java index cd3d3eb..8feab62 100644 --- a/jMusicHub.java +++ b/jMusicHub.java @@ -2,8 +2,6 @@ import java.util.*; import java.io.*; import java.text.*; /* TO DO : - * Printing album sorted by date of release - * Printing album sorted by its songs genre * Printing audiobooks sorted by author * Generating the javadoc * Doing the UML Diagramm @@ -365,6 +363,29 @@ public class jMusicHub { catch (ClassNotFoundException ex) {System.out.println("ClassNotFoundException is caught");} } +public static void listAlbumsByGenre(){ + String filename = "albums"; + try { + // Reading the object from a file + FileInputStream file = new FileInputStream(filename); + ObjectInputStream in = new ObjectInputStream(file); + + @SuppressWarnings("unchecked") + ArrayList<Album> albums = (ArrayList<Album>)in.readObject(); + in.close(); + file.close(); + + Collections.sort(albums, Comparator.comparing(Album::getGenre)); + + System.out.println("\nSaved "+filename+", sorted by name :\n"); + for (Album a : albums ){ + System.out.println(a); + } + } + 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 @@ -557,7 +578,7 @@ public class jMusicHub { 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\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\nPC: Print the content of a playlist\nAD: Print albums sorted by their date of release\nAC: Print the content of an album\n\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\nPC: Print the content of a playlist\nAD: Print albums sorted by their date of release\nAG: Print albums sorted by their genre\nAC: Print the content of an album\n\nh: print this help screen\nq: quit the program\n"); break; case "q" : //quit @@ -687,9 +708,12 @@ public class jMusicHub { case "PC": //display content of a chosen playlist contentOfPlaylist(playlists); break; - case "AD" : + case "AD" : // albums sorted by date listAlbumsByDate(); break; + case "AG" : //albums sorted by genre + listAlbumsByGenre(); + break; case "AC": //display content of a chosen album contentOfAlbum(albums); break; |