aboutsummaryrefslogtreecommitdiffstats
path: root/src/musichub/main/Main.java
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/main/java/musichub/main/Main.java (renamed from src/musichub/main/Main.java)99
1 files changed, 94 insertions, 5 deletions
diff --git a/src/musichub/main/Main.java b/src/main/java/musichub/main/Main.java
index f917b01..07786d9 100644
--- a/src/musichub/main/Main.java
+++ b/src/main/java/musichub/main/Main.java
@@ -1,12 +1,22 @@
package musichub.main;
import musichub.business.*;
+import musichub.util.LogHandler;
+import javax.sound.sampled.LineUnavailableException;
+import javax.sound.sampled.UnsupportedAudioFileException;
+import java.io.IOException;
import java.util.Iterator;
+import java.util.List;
import java.util.Scanner;
+import static musichub.util.PathValidation.isPathValid;
+import static musichub.util.Policy.showTerm;
+
public class Main {
- public static void main(String[] args) {
+ public static void main(String[] args) throws UnsupportedAudioFileException, IOException, LineUnavailableException, NoAlbumFoundException {
+
+ showTerm();
MusicHub theHub = new MusicHub();
@@ -20,8 +30,10 @@ public class Main {
if (choice.length() == 0) System.exit(0);
- while (choice.charAt(0) != 'q') {
+ 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);
case 'h':
printAvailableCommands();
choice = scan.nextLine();
@@ -39,8 +51,10 @@ public class Main {
albumTitle = scan.nextLine();
try {
- System.out.println(theHub.getAlbumSongsSortedByGenre(albumTitle));
+ List<Song> songs = theHub.getAlbumSongsSortedByGenre(albumTitle);
+ System.out.println(songs);
} catch (NoAlbumFoundException ex) {
+ LogHandler.write("No album found with the requested title", "WARNING");
System.out.println("No album found with the requested title " + ex.getMessage());
}
printAvailableCommands();
@@ -53,8 +67,12 @@ public class Main {
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) {
+ LogHandler.write("No album found with the requested title", "WARNING");
System.out.println("No album found with the requested title " + ex.getMessage());
}
printAvailableCommands();
@@ -75,15 +93,37 @@ public class Main {
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());
+
+ int length;
+ try {
+ System.out.println("Song length in seconds: ");
+ length = Integer.parseInt(scan.nextLine());
+ } catch (NumberFormatException ex) {
+ String logMsg = "You've not provided a number for the length.";
+ LogHandler.write(logMsg, "ERROR"); //write a line in the log file
+ System.err.println(logMsg);
+ System.out.println("Type 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 cannot be found with the path you've provided or the extension is not .wav";
+ LogHandler.write(logMsg, "ERROR"); //write a line in the log file
+ System.err.println(logMsg);
+ System.out.println("Type 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());
+ LogHandler.write("Song successfully created", "INFO");
System.out.println("Song created!");
printAvailableCommands();
choice = scan.nextLine();
@@ -104,6 +144,7 @@ public class Main {
System.out.println("New list of albums: ");
Iterator<Album> ita = theHub.albums();
while (ita.hasNext()) System.out.println(ita.next().getTitle());
+ LogHandler.write("Album successfully created", "INFO");
System.out.println("Album created!");
printAvailableCommands();
choice = scan.nextLine();
@@ -131,6 +172,7 @@ public class Main {
} catch (NoAlbumFoundException | NoElementFoundException ex) {
System.out.println(ex.getMessage());
}
+ LogHandler.write("Song successfully added to the album", "INFO");
System.out.println("Song added to the album!");
printAvailableCommands();
choice = scan.nextLine();
@@ -152,6 +194,7 @@ public class Main {
String bLanguage = scan.nextLine();
AudioBook b = new AudioBook(bTitle, bArtist, bLength, bContent, bLanguage, bCategory);
theHub.addElement(b);
+ LogHandler.write("Audiobook successfully created", "INFO");
System.out.println("Audiobook created! New element list: ");
Iterator<AudioElement> itl = theHub.elements();
while (itl.hasNext()) System.out.println(itl.next().getTitle());
@@ -190,6 +233,7 @@ public class Main {
System.out.println("Type y to add a new one, n to end");
choice = scan.nextLine();
}
+ LogHandler.write("Playlist successfully created", "INFO");
System.out.println("Playlist created!");
printAvailableCommands();
choice = scan.nextLine();
@@ -208,6 +252,7 @@ public class Main {
} catch (NoPlayListFoundException ex) {
System.out.println(ex.getMessage());
}
+ LogHandler.write("Playlist successfully deleted", "INFO");
System.out.println("Playlist deleted!");
printAvailableCommands();
choice = scan.nextLine();
@@ -217,10 +262,50 @@ public class Main {
theHub.saveElements();
theHub.saveAlbums();
theHub.savePlayLists();
+ LogHandler.write("Elements, albums and playlists successfully saved", "INFO");
System.out.println("Elements, albums and playlists saved!");
printAvailableCommands();
choice = scan.nextLine();
break;
+ case 'r':
+ //search a music
+ try {
+ theHub.searchAudioElement();
+ }
+ catch (NoElementFoundException e){
+ System.err.println(e.getMessage());
+ }
+ catch (java.io.FileNotFoundException e){
+ LogHandler.write("Please create a file with the extension .wav inside the song folder at root of the app", "ERROR");
+ System.err.println(e.getMessage() + " Please create a file with the extension .wav inside the song folder at root of the app");
+ }
+ 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;
+ case 'm':
+ //songs of a playlist
+ System.out.println("Songs of a playlist will be displayed; enter the playlist name, available playlists are:");
+ System.out.println(theHub.getPlayListsTitles());
+
+ playListTitle = scan.nextLine();
+ try {
+ List<AudioElement> songs = theHub.getPlayListSongs(playListTitle);
+ System.out.println(theHub.getPlayListSongs(playListTitle));
+ String song = scan.nextLine();
+ theHub.getAudioElement(songs, song);
+ } catch (NoPlayListFoundException ex) {
+ LogHandler.write("No playlist found with the requested title", "WARNING");
+ System.out.println("No playlist found with the requested title " + ex.getMessage());
+ }
+ printAvailableCommands();
+ choice = scan.nextLine();
+ break;
default:
break;
@@ -234,13 +319,17 @@ public class Main {
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("r: search audio elements");
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("m: display songs of a playlist");
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