//	Tommy Gober
//	CSC2103.01
//	CPO 1427
//	due date: 10/5/2005
//	purpose: Program reads in Ride name and information from text file.
//			 Prompts the user for which method to sort the data and for
//			 ascending or descending order. Rides are stored in Rollercoaster
//			 class. A new object is created for each ride, pointer to object is
//			 stored in array.
//			 
//////////////////////////////////////////////////////////////////////////
import hsa.*;

public class prog2
{
	public static void main (String args[]){
		String inputfile = "rides.txt";	// specify file to read from
		String outputfile = "outfile.txt";	// specify file to write to
		TextInputFile infile = new TextInputFile(inputfile);
		TextOutputFile outfile = new TextOutputFile(outputfile);
		RollerCoaster array[] = new RollerCoaster[50];
		int counter = 0;
		
		System.out.println("Reading " + inputfile + "...");
		while (!infile.eof())
		{
			counter++;
			RollerCoaster ride = new RollerCoaster();
			
			ride.setName(infile.readLine());
			ride.setLength(infile.readInt());
			ride.setRunTime(infile.readInt(), infile.readInt());
			ride.setCost(infile.readInt());
			array[counter] = ride;
		}
		//done with file
		infile.close();
		
		//User prompt...
		System.out.println("Done.\nReady to sort rides...");
		System.out.print("Sort by (N)ame, (T)ime or (L)ength:");
		switch (Stdin.readChar()){
			case 'n':
			case 'N':
				sort_by_name(array, counter);
				break;
			case 't':
			case 'T':
				sort_by_runtime(array, counter);
				break;
			case 'l':
			case 'L':
				sort_by_length(array, counter);
				break;
		}
		//Array sorted per user request...
		print_ridenames(array, counter);
		output_header(outfile);
		output_to_file(array,counter,outfile);
		System.out.println("\n" + outputfile + " written. Goodbye.");
		
	}
	
	//DUMP RIDE NAMES IN SORTED ORDER (just for looks)
	public static void print_ridenames(RollerCoaster[] array, int counter){
		for (int x=1; x < counter; x++){
			System.out.println("#" + x + ": " + array[x].getName());
		}
	}
	
	//OUTFILE HEADER
	public static void output_header(TextOutputFile out){
		out.println("                        Techo Coaster Evaluation");
		out.println("                     Tommy Gober , Senior Evaluator");
		out.println("                          Report Date 10/05/2005");
		out.println("                                                               Average");
		out.println("Ride Name                       Cost       Length      Time     Speed ");
		out.println("---------------------------  ----------  ----------  --------  --------");
	}
	
	//OUTPUT TO FILE
	public static void output_to_file(RollerCoaster[] array, int counter, TextOutputFile out){
		for (int x=1; x < counter; x++){
			out.print(array[x].getName(),27);
			out.print(array[x].getCost(),10);
			out.print(array[x].getLength(),13,2);
			out.print(array[x].getRunTime(),9,2);
			out.print(array[x].getSpeed(),10,2);
			out.print("\n");
		}
	}
	
	//METHOD TO SWAP VALUES IN ARRAY
	//		SWAPS Xth AND Yth
	public static void swap(RollerCoaster[] array, int x, int y){
		RollerCoaster temp;
		temp = array[x];
		array[x] = array[y];
		array[y] = temp;
	}
	
	//METHOD TO SORT BY RUNTIME OF RIDE
	public static void sort_by_name(RollerCoaster[] array, int counter){
		String rideA, rideB;
		for (int x=1; x <= counter; x++)
		{
			for (int y=x+1; y < counter; y++)
			{
				rideA = array[x].getName();
				rideB = array[y].getName();
				
				if ( rideA.compareTo(rideB) > 0 )
					swap(array,x,y);
			}
		}	
	}
	
	//METHOD TO SORT BY RUNTIME OF RIDE
	public static void sort_by_runtime(RollerCoaster[] array, int counter){
		for (int x=1; x <= counter; x++)
		{
			for (int y=x+1; y < counter; y++)
			{
				if ( array[x].getRunTime() > array[y].getRunTime() )
					swap(array,x,y);
			}
		}	
	}
	
	//METHOD TO SORT BY RIDE LENGTH
	public static void sort_by_length(RollerCoaster[] array, int counter){
	RollerCoaster temp;
		for (int x=1; x<=counter; x++){
			for (int y=x+1; y < counter; y++){
				if ( array[x].getLength() < array[y].getLength() )
						swap(array,x,y);
			}
		}
	}
	
}