From 67ed18fe86ddfabe5f6c6c270273597799722a72 Mon Sep 17 00:00:00 2001 From: Clyhtsuriva Date: Sat, 26 Jun 2021 22:07:53 +0200 Subject: Tests are ok fully covered. --- target/site/jacoco/musichub.main/Main.java.html | 410 ++++++++++++------------ 1 file changed, 210 insertions(+), 200 deletions(-) (limited to 'target/site/jacoco/musichub.main/Main.java.html') diff --git a/target/site/jacoco/musichub.main/Main.java.html b/target/site/jacoco/musichub.main/Main.java.html index df39af0..7ec6a41 100644 --- a/target/site/jacoco/musichub.main/Main.java.html +++ b/target/site/jacoco/musichub.main/Main.java.html @@ -1,6 +1,7 @@ Main.java

Main.java

package musichub.main;
 
 import musichub.business.*;
+import musichub.util.LogHandler;
 
 import javax.sound.sampled.LineUnavailableException;
 import javax.sound.sampled.UnsupportedAudioFileException;
@@ -9,258 +10,267 @@ import java.util.Iterator;
 import java.util.List;
 import java.util.Scanner;
 
-import static musichub.business.PathValidation.isPathValid;
+import static musichub.util.PathValidation.isPathValid;
 
-public class Main {
+public class Main {
     public static void main(String[] args) throws UnsupportedAudioFileException, IOException, LineUnavailableException {
 
-        MusicHub theHub = new MusicHub();
+        MusicHub theHub = new MusicHub();
 
-        System.out.println("Type h for available commands");
+        System.out.println("Type h for available commands");
 
 
-        Scanner scan = new Scanner(System.in);
-        String choice = scan.nextLine();
+        Scanner scan = new Scanner(System.in);
+        String choice = scan.nextLine();
 
         String albumTitle;
 
-        if (choice.length() == 0) System.exit(0);
+        if (choice.length() == 0) System.exit(0);
 
-        while (!choice.equals("")) { //if the user puts nothing, quit the loop/system
-            switch (choice.charAt(0)) {
+        while (!choice.equals("")) { //if the user puts nothing, quit the loop/system
+            switch (choice.charAt(0)) {
                 case 'q': //added the option directly in the switch instead of the loop
-                    System.exit(0);
+                    System.exit(0);
                 case 'h':
-                    printAvailableCommands();
-                    choice = scan.nextLine();
-                    break;
+                    printAvailableCommands();
+                    choice = scan.nextLine();
+                    break;
                 case 't':
                     //album titles, ordered by date
-                    System.out.println(theHub.getAlbumsTitlesSortedByDate());
-                    printAvailableCommands();
-                    choice = scan.nextLine();
-                    break;
+                    System.out.println(theHub.getAlbumsTitlesSortedByDate());
+                    printAvailableCommands();
+                    choice = scan.nextLine();
+                    break;
                 case 'g':
                     //songs of an album, sorted by genre
-                    System.out.println("Songs of an album sorted by genre will be displayed; enter the album name, available albums are:");
-                    System.out.println(theHub.getAlbumsTitlesSortedByDate());
+                    System.out.println("Songs of an album sorted by genre will be displayed; enter the album name, available albums are:");
+                    System.out.println(theHub.getAlbumsTitlesSortedByDate());
 
-                    albumTitle = scan.nextLine();
+                    albumTitle = scan.nextLine();
                     try {
-                    	List<Song> songs = theHub.getAlbumSongsSortedByGenre(albumTitle);
-                        System.out.println(songs);
-                    } catch (NoAlbumFoundException ex) {
-                        System.out.println("No album found with the requested title " + ex.getMessage());
-                    }
-                    printAvailableCommands();
-                    choice = scan.nextLine();
-                    break;
+                    	List<Song> songs = theHub.getAlbumSongsSortedByGenre(albumTitle);
+                        System.out.println(songs);
+                    } catch (NoAlbumFoundException ex) {
+                        System.out.println("No album found with the requested title " + ex.getMessage());
+                    }
+                    printAvailableCommands();
+                    choice = scan.nextLine();
+                    break;
                 case 'd':
                     //songs of an album
-                    System.out.println("Songs of an album will be displayed; enter the album name, available albums are:");
-                    System.out.println(theHub.getAlbumsTitlesSortedByDate());
+                    System.out.println("Songs of an album will be displayed; enter the album name, available albums are:");
+                    System.out.println(theHub.getAlbumsTitlesSortedByDate());
 
-                    albumTitle = scan.nextLine();
+                    albumTitle = scan.nextLine();
                     try {
-                    	List<AudioElement> songs = theHub.getAlbumSongs(albumTitle);
-                        System.out.println(theHub.getAlbumSongs(albumTitle));
-                        String song = scan.nextLine();
-                        theHub.getAudioElement(songs, song);
-                    } catch (NoAlbumFoundException ex) {
-                        System.out.println("No album found with the requested title " + ex.getMessage());
-                    }
-                    printAvailableCommands();
-                    choice = scan.nextLine();
-                    break;
+                    	List<AudioElement> songs = theHub.getAlbumSongs(albumTitle);
+                        System.out.println(theHub.getAlbumSongs(albumTitle));
+                        String song = scan.nextLine();
+                        theHub.getAudioElement(songs, song);
+                    } catch (NoAlbumFoundException ex) {
+                        System.out.println("No album found with the requested title " + ex.getMessage());
+                    }
+                    printAvailableCommands();
+                    choice = scan.nextLine();
+                    break;
                 case 'u':
                     //audiobooks ordered by author
-                    System.out.println(theHub.getAudiobooksTitlesSortedByAuthor());
-                    printAvailableCommands();
-                    choice = scan.nextLine();
-                    break;
+                    System.out.println(theHub.getAudiobooksTitlesSortedByAuthor());
+                    printAvailableCommands();
+                    choice = scan.nextLine();
+                    break;
                 case 'c':
                     // add a new song
-                    System.out.println("Enter a new song: ");
-                    System.out.println("Song title: ");
-                    String title = scan.nextLine();
-                    System.out.println("Song genre (jazz, classic, hiphop, rock, pop, rap):");
-                    String genre = scan.nextLine();
-                    System.out.println("Song artist: ");
-                    String artist = scan.nextLine();
-                    System.out.println("Song length in seconds: ");
-                    int length = Integer.parseInt(scan.nextLine());
+                    System.out.println("Enter a new song: ");
+                    System.out.println("Song title: ");
+                    String title = scan.nextLine();
+                    System.out.println("Song genre (jazz, classic, hiphop, rock, pop, rap):");
+                    String genre = scan.nextLine();
+                    System.out.println("Song artist: ");
+                    String artist = scan.nextLine();
+                    System.out.println("Song length in seconds: ");
+                    int length = Integer.parseInt(scan.nextLine());
 
-                    System.out.println("Song content: ");
-                    String content = scan.nextLine();
-                    if (!isPathValid(content)) {
-                        System.out.println("The music file was not found with the path you've provided.\nType h for available commands");
-                        choice = scan.nextLine();
-                        break;
+                    System.out.println("Song content: ");
+                    String content = scan.nextLine();
+                    if (!isPathValid(content)) {
+                        String logMsg = "The music file was not found with the path you've provided.";
+                        LogHandler.write(logMsg, "WARNING"); //write a line in the log file
+                        System.out.println(logMsg + "\nType h for available commands");
+                        choice = scan.nextLine();
+                        break;
                     }
 
-                    Song s = new Song(title, artist, length, content, genre);
-                    theHub.addElement(s);
-                    System.out.println("New element list: ");
-                    Iterator<AudioElement> it = theHub.elements();
-                    while (it.hasNext()) System.out.println(it.next().getTitle());
-                    System.out.println("Song created!");
-                    printAvailableCommands();
-                    choice = scan.nextLine();
-                    break;
+                    Song s = new Song(title, artist, length, content, genre);
+                    theHub.addElement(s);
+                    System.out.println("New element list: ");
+                    Iterator<AudioElement> it = theHub.elements();
+                    while (it.hasNext()) System.out.println(it.next().getTitle());
+                    System.out.println("Song created!");
+                    printAvailableCommands();
+                    choice = scan.nextLine();
+                    break;
                 case 'a':
                     // add a new album
-                    System.out.println("Enter a new album: ");
-                    System.out.println("Album title: ");
-                    String aTitle = scan.nextLine();
-                    System.out.println("Album artist: ");
-                    String aArtist = scan.nextLine();
-                    System.out.println("Album length in seconds: ");
-                    int aLength = Integer.parseInt(scan.nextLine());
-                    System.out.println("Album date as YYYY-DD-MM: ");
-                    String aDate = scan.nextLine();
-                    Album a = new Album(aTitle, aArtist, aLength, aDate);
-                    theHub.addAlbum(a);
-                    System.out.println("New list of albums: ");
-                    Iterator<Album> ita = theHub.albums();
-                    while (ita.hasNext()) System.out.println(ita.next().getTitle());
-                    System.out.println("Album created!");
-                    printAvailableCommands();
-                    choice = scan.nextLine();
-                    break;
+                    System.out.println("Enter a new album: ");
+                    System.out.println("Album title: ");
+                    String aTitle = scan.nextLine();
+                    System.out.println("Album artist: ");
+                    String aArtist = scan.nextLine();
+                    System.out.println("Album length in seconds: ");
+                    int aLength = Integer.parseInt(scan.nextLine());
+                    System.out.println("Album date as YYYY-DD-MM: ");
+                    String aDate = scan.nextLine();
+                    Album a = new Album(aTitle, aArtist, aLength, aDate);
+                    theHub.addAlbum(a);
+                    System.out.println("New list of albums: ");
+                    Iterator<Album> ita = theHub.albums();
+                    while (ita.hasNext()) System.out.println(ita.next().getTitle());
+                    System.out.println("Album created!");
+                    printAvailableCommands();
+                    choice = scan.nextLine();
+                    break;
                 case '+':
                     //add a song to an album:
-                    System.out.println("Add an existing song to an existing album");
-                    System.out.println("Type the name of the song you wish to add. Available songs: ");
-                    Iterator<AudioElement> itae = theHub.elements();
-                    while (itae.hasNext()) {
-                        AudioElement ae = itae.next();
-                        if (ae instanceof Song) System.out.println(ae.getTitle());
-                    }
-                    String songTitle = scan.nextLine();
+                    System.out.println("Add an existing song to an existing album");
+                    System.out.println("Type the name of the song you wish to add. Available songs: ");
+                    Iterator<AudioElement> itae = theHub.elements();
+                    while (itae.hasNext()) {
+                        AudioElement ae = itae.next();
+                        if (ae instanceof Song) System.out.println(ae.getTitle());
+                    }
+                    String songTitle = scan.nextLine();
 
-                    System.out.println("Type the name of the album you wish to enrich. Available albums: ");
-                    Iterator<Album> ait = theHub.albums();
-                    while (ait.hasNext()) {
-                        Album al = ait.next();
-                        System.out.println(al.getTitle());
-                    }
-                    String titleAlbum = scan.nextLine();
+                    System.out.println("Type the name of the album you wish to enrich. Available albums: ");
+                    Iterator<Album> ait = theHub.albums();
+                    while (ait.hasNext()) {
+                        Album al = ait.next();
+                        System.out.println(al.getTitle());
+                    }
+                    String titleAlbum = scan.nextLine();
                     try {
-                        theHub.addElementToAlbum(songTitle, titleAlbum);
-                    } catch (NoAlbumFoundException | NoElementFoundException ex) {
-                        System.out.println(ex.getMessage());
-                    }
-                    System.out.println("Song added to the album!");
-                    printAvailableCommands();
-                    choice = scan.nextLine();
-                    break;
+                        theHub.addElementToAlbum(songTitle, titleAlbum);
+                    } catch (NoAlbumFoundException | NoElementFoundException ex) {
+                        System.out.println(ex.getMessage());
+                    }
+                    System.out.println("Song added to the album!");
+                    printAvailableCommands();
+                    choice = scan.nextLine();
+                    break;
                 case 'l':
                     // add a new audiobook
-                    System.out.println("Enter a new audiobook: ");
-                    System.out.println("AudioBook title: ");
-                    String bTitle = scan.nextLine();
-                    System.out.println("AudioBook category (youth, novel, theater, documentary, speech)");
-                    String bCategory = scan.nextLine();
-                    System.out.println("AudioBook artist: ");
-                    String bArtist = scan.nextLine();
-                    System.out.println("AudioBook length in seconds: ");
-                    int bLength = Integer.parseInt(scan.nextLine());
-                    System.out.println("AudioBook content: ");
-                    String bContent = scan.nextLine();
-                    System.out.println("AudioBook language (french, english, italian, spanish, german)");
-                    String bLanguage = scan.nextLine();
-                    AudioBook b = new AudioBook(bTitle, bArtist, bLength, bContent, bLanguage, bCategory);
-                    theHub.addElement(b);
-                    System.out.println("Audiobook created! New element list: ");
-                    Iterator<AudioElement> itl = theHub.elements();
-                    while (itl.hasNext()) System.out.println(itl.next().getTitle());
-                    printAvailableCommands();
-                    choice = scan.nextLine();
-                    break;
+                    System.out.println("Enter a new audiobook: ");
+                    System.out.println("AudioBook title: ");
+                    String bTitle = scan.nextLine();
+                    System.out.println("AudioBook category (youth, novel, theater, documentary, speech)");
+                    String bCategory = scan.nextLine();
+                    System.out.println("AudioBook artist: ");
+                    String bArtist = scan.nextLine();
+                    System.out.println("AudioBook length in seconds: ");
+                    int bLength = Integer.parseInt(scan.nextLine());
+                    System.out.println("AudioBook content: ");
+                    String bContent = scan.nextLine();
+                    System.out.println("AudioBook language (french, english, italian, spanish, german)");
+                    String bLanguage = scan.nextLine();
+                    AudioBook b = new AudioBook(bTitle, bArtist, bLength, bContent, bLanguage, bCategory);
+                    theHub.addElement(b);
+                    System.out.println("Audiobook created! New element list: ");
+                    Iterator<AudioElement> itl = theHub.elements();
+                    while (itl.hasNext()) System.out.println(itl.next().getTitle());
+                    printAvailableCommands();
+                    choice = scan.nextLine();
+                    break;
                 case 'p':
                     //create a new playlist from existing elements
-                    System.out.println("Add an existing song or audiobook to a new playlist");
-                    System.out.println("Existing playlists:");
-                    Iterator<PlayList> itpl = theHub.playlists();
-                    while (itpl.hasNext()) {
-                        PlayList pl = itpl.next();
-                        System.out.println(pl.getTitle());
-                    }
-                    System.out.println("Type the name of the playlist you wish to create:");
-                    String playListTitle = scan.nextLine();
-                    PlayList pl = new PlayList(playListTitle);
-                    theHub.addPlaylist(pl);
-                    System.out.println("Available elements: ");
+                    System.out.println("Add an existing song or audiobook to a new playlist");
+                    System.out.println("Existing playlists:");
+                    Iterator<PlayList> itpl = theHub.playlists();
+                    while (itpl.hasNext()) {
+                        PlayList pl = itpl.next();
+                        System.out.println(pl.getTitle());
+                    }
+                    System.out.println("Type the name of the playlist you wish to create:");
+                    String playListTitle = scan.nextLine();
+                    PlayList pl = new PlayList(playListTitle);
+                    theHub.addPlaylist(pl);
+                    System.out.println("Available elements: ");
 
-                    Iterator<AudioElement> itael = theHub.elements();
-                    while (itael.hasNext()) {
-                        AudioElement ae = itael.next();
-                        System.out.println(ae.getTitle());
-                    }
-                    while (choice.charAt(0) != 'n') {
-                        System.out.println("Type the name of the audio element you wish to add or 'n' to exit:");
-                        String elementTitle = scan.nextLine();
+                    Iterator<AudioElement> itael = theHub.elements();
+                    while (itael.hasNext()) {
+                        AudioElement ae = itael.next();
+                        System.out.println(ae.getTitle());
+                    }
+                    while (choice.charAt(0) != 'n') {
+                        System.out.println("Type the name of the audio element you wish to add or 'n' to exit:");
+                        String elementTitle = scan.nextLine();
                         try {
-                            theHub.addElementToPlayList(elementTitle, playListTitle);
-                        } catch (NoPlayListFoundException | NoElementFoundException ex) {
-                            System.out.println(ex.getMessage());
-                        }
+                            theHub.addElementToPlayList(elementTitle, playListTitle);
+                        } catch (NoPlayListFoundException | NoElementFoundException ex) {
+                            System.out.println(ex.getMessage());
+                        }
 
-                        System.out.println("Type y to add a new one, n to end");
-                        choice = scan.nextLine();
-                    }
-                    System.out.println("Playlist created!");
-                    printAvailableCommands();
-                    choice = scan.nextLine();
-                    break;
+                        System.out.println("Type y to add a new one, n to end");
+                        choice = scan.nextLine();
+                    }
+                    System.out.println("Playlist created!");
+                    printAvailableCommands();
+                    choice = scan.nextLine();
+                    break;
                 case '-':
                     //delete a playlist
-                    System.out.println("Delete an existing playlist. Available playlists:");
-                    Iterator<PlayList> itp = theHub.playlists();
-                    while (itp.hasNext()) {
-                        PlayList p = itp.next();
-                        System.out.println(p.getTitle());
-                    }
-                    String plTitle = scan.nextLine();
+                    System.out.println("Delete an existing playlist. Available playlists:");
+                    Iterator<PlayList> itp = theHub.playlists();
+                    while (itp.hasNext()) {
+                        PlayList p = itp.next();
+                        System.out.println(p.getTitle());
+                    }
+                    String plTitle = scan.nextLine();
                     try {
-                        theHub.deletePlayList(plTitle);
-                    } catch (NoPlayListFoundException ex) {
-                        System.out.println(ex.getMessage());
-                    }
-                    System.out.println("Playlist deleted!");
-                    printAvailableCommands();
-                    choice = scan.nextLine();
-                    break;
+                        theHub.deletePlayList(plTitle);
+                    } catch (NoPlayListFoundException ex) {
+                        System.out.println(ex.getMessage());
+                    }
+                    System.out.println("Playlist deleted!");
+                    printAvailableCommands();
+                    choice = scan.nextLine();
+                    break;
                 case 's':
                     //save elements, albums, playlists
-                    theHub.saveElements();
-                    theHub.saveAlbums();
-                    theHub.savePlayLists();
-                    System.out.println("Elements, albums and playlists saved!");
-                    printAvailableCommands();
-                    choice = scan.nextLine();
-                    break;
+                    theHub.saveElements();
+                    theHub.saveAlbums();
+                    theHub.savePlayLists();
+                    System.out.println("Elements, albums and playlists saved!");
+                    printAvailableCommands();
+                    choice = scan.nextLine();
+                    break;
+                case 'o':
+                    //consult the app logs
+                    LogHandler.read();
+                    System.out.println("Type h for available commands");
+                    choice = scan.nextLine();
+                    break;
                 default:
 
-                    break;
+                    break;
             }
         }
-        scan.close();
-    }
+        scan.close();
+    }
 
     private static void printAvailableCommands() {
-        System.out.println("t: display the album titles, ordered by date");
-        System.out.println("g: display songs of an album, ordered by genre");
-        System.out.println("d: display songs of an album");
-        System.out.println("u: display audiobooks ordered by author");
-        System.out.println("c: add a new song");
-        System.out.println("a: add a new album");
-        System.out.println("+: add a song to an album");
-        System.out.println("l: add a new audiobook");
-        System.out.println("p: create a new playlist from existing songs and audio books");
-        System.out.println("-: delete an existing playlist");
-        System.out.println("s: save elements, albums, playlists");
-        System.out.println("q: quit program");
-    }
+        System.out.println("t: display the album titles, ordered by date");
+        System.out.println("g: display songs of an album, ordered by genre");
+        System.out.println("d: display songs of an album");
+        System.out.println("u: display audiobooks ordered by author");
+        System.out.println("c: add a new song");
+        System.out.println("a: add a new album");
+        System.out.println("+: add a song to an album");
+        System.out.println("l: add a new audiobook");
+        System.out.println("p: create a new playlist from existing songs and audio books");
+        System.out.println("-: delete an existing playlist");
+        System.out.println("s: save elements, albums, playlists");
+        System.out.println("o: consult the app logs");
+        System.out.println("q: quit program");
+    }
 }
 
\ No newline at end of file -- cgit v1.2.3