import java.util.Scanner;

/** ISPBilling makes bills based on ISPCharges
 *
 * @author Nancy Harris
 * @version V1 10/2013
 */
public class ISPBilling
{
	private static Scanner keyboard; 
	
   /** main creates the bill
    */
	public static void main(String[] args)
	{
      char pkg;
      double hours;
      double savings;
      ISPCharge charge;
      
		keyboard = new Scanner(System.in);
		System.out.println("Dukes ISP Billing");
		System.out.println();
		
		
      pkg = readPackage("Enter the package code (A, B, or C): ");
		hours = readHours("Enter the number of hours used: ");
		
		charge = new ISPCharge (pkg, hours);
		
		System.out.println();
		System.out.println("Customer Bill");
		System.out.println();
		System.out.printf("Package: %s\n", charge.getPackage() );
		System.out.printf("Hours Used: %.2f\n\n", charge.getBaseHours() + charge.getAddtlHours());
		if(charge.needAddtlHours())
		{	
         printAdditional(charge);
		}
		System.out.println();
		System.out.printf("Total Charge: $%.2f\n", charge.calcCost());
		System.out.printf("Tax: $%.2f\n", charge.calcTax());
		System.out.println();
		System.out.printf("Pay this Amount: $%.2f\n", charge.calcCost() + charge.calcTax());
		
		checkSavings(charge);
	}
	
   /** checkSavings will perform the output for calculating
     * the savings with other packages
     */
	public static void checkSavings(ISPCharge charge)
	{	
		double savings;
		
		if (charge.saveWithB() || charge.saveWithC())
		{
			System.out.println();
		
			if (charge.saveWithB())
			{
				savings = charge.savingsWithB();
				System.out.printf("You could have saved $%,.2f with package B." + 
						" Call 1-888-555-1234 for more information.\n", savings);
			}
			if (charge.saveWithC())
			{
				savings = charge.savingsWithC();
				System.out.printf("You could have saved $%,.2f with package C." + 
						" Call 1-888-555-1234 for more information.\n", savings);
			}
		}
	}
	/** readPackage prompts the user and reads in the 
    *  package code. If a bad code is entered
    *  it returns a default.
    *
    *  @param prompt The prompt to use
    */
	public static char readPackage(String prompt)
	{
		String pkg;
		
		System.out.print(prompt);
		pkg = keyboard.next();
            
      if(!(pkg.length() == 1 && pkg.toUpperCase().charAt(0) >= 'A' 
         && pkg.toUpperCase().charAt(0) <= 'C'))
      {
      		System.out.printf("You entered %s. Using A\n", pkg);
				pkg = "A";
		}
		
		return pkg.toUpperCase().charAt(0);
	}
	
   /** readHours reads in the hours defaulting if
    *  bad values are read in.
    *
    * @param prompt The prompt to use
    */
	public static double readHours(String prompt)
	{
		double hours;
		
		System.out.print(prompt);
		if (keyboard.hasNextDouble())
		{
			hours = keyboard.nextDouble();
			if (hours < 0)
			{
				System.out.printf("You entered %f. Using 0\n", hours);
				hours = 0;
			}
		}
		else
		{
			System.out.printf("You entered %s. Using 0\n", keyboard.nextLine());
			hours = 0;
		}
		return hours;
	}
   
   /** printHours prints the additional hours
    *
    * @param charge The charge to print
    */
    public static void printAdditional(ISPCharge charge)
    {
   		System.out.printf("Base Charge: $%.2f\n", charge.getBase());
			System.out.printf("Additional Hours: %.2f\n", charge.getAddtlHours());
    }
}
