import java.io.*;
import java.util.Arrays;
import java.util.StringTokenizer;

public class musicDatabase
{
static int x=0;
static int y=0; // generic loopers

static String sname, aname, cdname;
static int time, track;

static int numberOfSongs;

static boolean debugPrint = true;

public static void main(String [] args)
{

// set debug print: was program started with "java musicDatabase 1" ?
if (args.length > 0)
   {
   if (args[0].equals("1"))
      {
      debugPrint = true;
      }
   else 
      {
      debugPrint = false;
      }
   } // end if


File songList = new File("songs.txt");
try {
   BufferedReader songInput = new BufferedReader(new FileReader( songList ) );
   x = 0;
   while (songInput.ready())
      {
	   songInput.readLine();
      x++;	  
	   } 
	numberOfSongs = x/5;
	songInput.close();
	  	  
   dPrint("\nLines in file: " + x + "  Number of Songs: " + numberOfSongs + "\n");
   
   } // end try
catch (IOException e)
   {
   System.out.println("Bad File");
   System.exit(0);
   } // end catch

musicRecord musicDatabase[] = new musicRecord[ numberOfSongs ];
String sortedSongs[] = new String[ numberOfSongs ];
String sortedArtists[] = new String[ numberOfSongs ]; 

try
   {
   BufferedReader arrayInput = new BufferedReader( new FileReader( songList ) );
   for (x=0; x<numberOfSongs; x++)
      {
      sname = arrayInput.readLine();
	   aname = arrayInput.readLine();
	   cdname = arrayInput.readLine();
	   StringTokenizer token = new StringTokenizer( arrayInput.readLine() );
      time = Integer.parseInt( token.nextToken() );
	   track = Integer.parseInt( token.nextToken() );
	   arrayInput.readLine();
	  
      musicDatabase[x] = new musicRecord( sname, aname, cdname, time, track );

      sortedSongs[x] = sname;
	   sortedArtists[x] = aname;
      
	   } // end for 
      
   arrayInput.close();
   } // end try	  
catch (IOException e)
   {
   System.out.println("Bad File");
   System.exit(0);
   }	  

// sort and print artist list
Arrays.sort(sortedArtists);
dPrint("Artist list:");
for (x=0; x<numberOfSongs; x++)
   {
   dPrint( "  " + sortedArtists[x] );
   }// end for
dPrint(" ");

// sort and print song list
Arrays.sort(sortedSongs);
dPrint("Song list:");
for (x=0; x<numberOfSongs; x++)
   {
   dPrint( "  " + sortedSongs[x] );
   }// end for
dPrint(" ");


// step thru every song in the Song list
// find a match in musicDatabase
// set "placeInSongList" in the musicRecord equal to its place in the song list
for(x=0; x<numberOfSongs; x++)
   {
   dPrint("\nLooking for: " + sortedSongs[x] );
   for (y=0; y<numberOfSongs; y++)
	   {
      dPrint("               database record has: " + musicDatabase[y].getSong() );
      if ( sortedSongs[x].equals( musicDatabase[y].getSong() ) )
		   {
         dPrint("                  -> match! placeInSongList for this song set to " + x);
			musicDatabase[y].setSongPlace( x );
	      }
	   } // end for y = every database record
   } // end for x = every song in sortedSongs
dPrint(" ");

// step thru every artist in the Artist list
// find a match in musicDatabase
// set "placeInArtistList" in the musicRecord equal to its place in the artist list
for(x=0; x<numberOfSongs; x++)
   {
   for (y=0; y<numberOfSongs; y++)
	   {
		if ( sortedArtists[x].equals( musicDatabase[y].getArtist() ) )
		   {
			musicDatabase[y].setArtistPlace( x );
		   } 
	   } // end for y = every database record
   } // end for x = every song in sortedSongs


// print the finished database
dPrint(" ");
for(x=0; x<numberOfSongs; x++)
   {
   musicDatabase[x].printSongRecord();
   }

} // end main method


// prints only if debugPrint boolean set to true
public static void dPrint( String printThis )
   {
   
   if (debugPrint == true)
      {
      System.out.println( printThis );
      } // end if
   } // end dPrint method


} // end class

