From 61768eade51a08d0036156a05bd83311dc1b6299 Mon Sep 17 00:00:00 2001 From: Clyhtsuriva Date: Mon, 28 Dec 2020 13:33:43 +0100 Subject: Added method to display albums sorted by genre --- Album.java | 6 +++++- jMusicHub.java | 32 ++++++++++++++++++++++++++++---- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/Album.java b/Album.java index e2de48f..e457e72 100644 --- a/Album.java +++ b/Album.java @@ -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 albums = (ArrayList)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");} + } + /**

extract

* 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; -- cgit v1.2.3