/* Filename:    Song.java
 * Author:      Your name
 * Course:      CSCI 161 (Section 00)
 * Date:        Today's date
 * Assignment:  Lab 11
 * Description: A simple Song class used for a Music Library
 */

import java.util.Scanner;

public class MusicLibrary {
	
	// HINT: one "array" member of type "Song"
	Song[] songs;
	
	/**
	 * Constructs a new music library from a file
	 * @param fileScanner the Scanner to read library information from
	 */
	public MusicLibrary(Scanner fileScanner) {
		// HINT: grab song count from fileScanner and advance to next line
		int songCount = fileScanner.nextInt();
		fileScanner.nextLine();
		songs = new Song[songCount];
		// HINT: using a loop, process line-by-line -- adding a song per line
		for (int song = 0; song < songCount; ++song) {
			String[] data = fileScanner.nextLine().split(":");
			songs[song] = new Song(data[1], data[2], Integer.parseInt(data[0]));;
		}
	}
	
	/**
	 * prints the library as a table
	 */
	public void print() {
		// HINT: print out the header first, then have a for-loop which
		//       will tell each song to print itself
		System.out.printf("%-24s%-24s%4s\n", "TITLE","ARTIST","TIME");
		System.out.printf("%-24s%-24s%4s\n", "-----","------","----");
		for (Song s : songs) {
			s.print();
		}
	}
	
	/**
	 * Accumulates the sum of all of the songs in the library
	 * @return the total time (in seconds)
	 */
	public int getTotalTime() {
		// HINT: similar to last lab
		int sum = 0;
		for (Song s : songs) {
			sum += s.length;
		}
		return sum;
	}
	
	/**
	 * Finds the longest song in the library
	 * @return the song with the longest track length
	 */
	public Song getLongestSong() {
		// HINT: similar to last lab, but return the song instead of the index
		Song largest = songs[0];
		for (Song s : songs) {
			if (s.length > largest.length) {
				largest = s;
			}
		}
		return largest;
	}
	
	/**
	 * Finds the shortest song in the library
	 * @return the song with the shortest track length
	 */
	public Song getShortestSong() {
		// HINT: similar to last lab, but return the song instead of the index
		Song smallest = songs[0];
		for (Song s : songs) {
			if (s.length < smallest.length) {
				smallest = s;
			}
		}
		return smallest;
	}
}
