import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;

class MusicCatalog {

	public static void main(String[] args) {
		
		Scanner kb = new Scanner(System.in);
		System.out.println("Please enter a file name");
		String filename = kb.nextLine();

		Scanner input = null;
		try {
			input = new Scanner(new File(filename));
			input.useDelimiter(",|\n");
		}
		catch (FileNotFoundException e) {
			System.out.println("file not found");
			return;
		}	

		Song[] arr = new Song[10];
		int i = 0;

		while(input.hasNext()) {
			String artist = input.next();
			String title = input.next();
			int duration = input.nextInt();
			String genre = input.next();
	
			Song s = new Song(title, artist, duration, genre);
			arr[i++] = s;
			System.out.println(s);
		}	
		input.close();

		while(true) {
			printMenu();
			System.out.print("Enter choice: ");
			int choice = kb.nextInt();
			kb.nextLine();

			switch(choice) {
				case 1: 
					displayAll(arr);
					break;
				case 2:
					displayTitledSongs(arr, kb);
					break;
				case 3:	
					displayArtistSongs(arr, kb);
					break;
				case 4:
					kb.close();		
					return;
				default:
					System.out.println("Invalid option");
			}	

		}					

	}

	static void displayAll(Song[] arr) {
		for(Song s : arr) {
			if (s != null) {
				System.out.println(s);
			}
		}
	}

	static void displayTitledSongs(Song[] arr, Scanner kb) {
		System.out.println("Please enter a title");
		String title = kb.nextLine();
		
		for(Song s : arr) {
			if (s == null) {
				return;
			}

			if (s.getTitle().equals(title)) {
				System.out.println(s);
			}
		}	
	}

	static void displayArtistSongs(Song[] arr, Scanner kb) {
		System.out.println("Please enter an artist");
		String artist = kb.nextLine();
		
		for(Song s : arr) {
			if (s == null) {
				return;
			}

			if (s.getArtist().equals(artist)) {
				System.out.println(s);
			}
		}	
	}

	static void printMenu() {

		System.out.println("1) Display all songs");		
		System.out.println("2) Display all songs with a given title");		
		System.out.println("3) Display all songs for a given artist");		
		System.out.println("4) Exit");		
	}

}
