//      Tommy Gober
//      CSC2103.01
//      CPO 1427
//      due date: 10/17/2005
//      purpose: Program will read in daily statistics for various rides.
//                       Will compile report for each day, and give a sum for all rides'
//                       revenue for each day. Program will output report to filename
//                       specified in String inputfile.
//
//                       
//////////////////////////////////////////////////////////////////////////

import hsa.*;

public class prog3
{
	public static void main (String args[]){
		String inputfile = "coasters.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 current;
		MyDate date;
		double revenue = 0.00;
		
		System.out.println("Reading data from " + inputfile + "...");
		
		print_header(outfile);
		
		//prime the while loop...               
		current = RollerCoaster.readCoasterRec(infile);
		date = current.getRevenueDate();
		group_header(outfile, date);
		
		while (!infile.eof()){
			//check to see if date is different than previous entry...
			if (date.compareTo(current.getRevenueDate()) != 0){
				group_footer(outfile, revenue);
				date = current.getRevenueDate();
				revenue = 0.00;
				group_header(outfile, date);
			}
			revenue += calcRevenue(current);
			print_detail(outfile, current);
						
			//next line...
			current = RollerCoaster.readCoasterRec(infile);
		}
		group_footer(outfile, revenue);
		outfile.close();
		System.out.println(outputfile + " created.\nGoodbye.");
	}
	
	//PRINT FILE OUTPUT HEADER
	public static void print_header(TextOutputFile out){
		out.print("",20);
		out.println("Roller Coaster Ticket Sales Report");
		out.print("",18);
		out.println("Tommy Gober , Techo Park Sales Manager");
		out.print("",25);
		out.println("Report Date: 10/17/2005\n");
	}
	
	//PRINT GROUP HEADER FOR START OF NEW DAY
	public static void group_header(TextOutputFile out, MyDate date){
		out.print("Revenue Date : ");
		out.print(date.toString(),20);
		out.print("Rider",9);
		out.print("Ticket",11);
		out.print("Number of",13);
		out.println("Coaster",16);
		
		out.print("Coaster Name",35);
		out.print("Count",10);
		out.print("Price",10);
		out.print("Discounts",13);
		out.println("Revenue",16);
		
		out.print("--------------------------------",34);
		out.print("--------",10);
		out.print("--------",10);
		out.print("-----------",13);
		out.println("----------");
	}
	
	//PRINT GROUP FOOTER TO CONCLUDE EACH DAY
	public static void group_footer(TextOutputFile out, double revenue){
		out.print("",67);
		out.println("==========");
		out.print("",46);
		out.print("Total Daily Revenue",21);
		out.print(revenue,9,2);
		out.println("\n");
	}
	
	//CALC REVENUE FOR GIVEN ENTRY
	public static double calcRevenue(RollerCoaster ride){
		double revenue = ( ride.getRiders() - ride.getDiscounts() ) * ride.getPrice() + ride.getDiscounts() * 0.9 * ride.getPrice();
		int foo = (int)((100*revenue)+.5);
		return foo/100.;
	}
	
	//PRINT RIDE DETAIL FOR ROLLERCOASTER
	public static void print_detail(TextOutputFile out, RollerCoaster ride){
		out.print(ride.getName(),26);
		out.print(ride.getRiders(),14);
		out.print(ride.getPrice(),10,2);
		out.print(ride.getDiscounts(),11);
		out.print(calcRevenue(ride),15,2);
		out.println("");
	}
}
