// Computer Science Example
// Example using doubles

import java.io.*;
import java.text.*;

/* This application can be used to read in the cost of a meal and the level of service.  The service levels are Excellent, Good, Fair and Poor.  These levels are used to determine the size of the tip.  The tax rate is set at 8.25%.  With this data, the total bill is computed and displayed. */

public class Restaurant
{
    public static void main (String [] args)
    {
        Meal meal = new Meal ();
        meal.getData ();
        meal.calculateTotal ();
        meal.displayBill ();
    } // method main
} // class Restaurant

// Class that reads in the cost of a meal and the level of service and then calculates the restaurant bill.
class Meal
{
     private String service;
     private double cost, tip, tax, total;
 
     public void getData ()
     {
          try
          {
               BufferedReader stdin = new BufferedReader (new InputStreamReader (System.in));
               System.out.print ("Enter the cost of the meal: ");
               cost = Double.parseDouble (stdin.readLine ());
               System.out.print ("Enter the quality of service: ");
               service = stdin.readLine ();
           } catch (NumberFormatException ex) {System.out.println ("Numerical error.");}
           catch (IOException e) {System.out.println ("IO Exception");}
      } // method getData

     // calculateTip uses the level of service to determine the size of the tip.
    private void calculateTip ()
     {
          if (service.equals ("Fair")) tip = cost * 0.1;
          else if (service.equals ("Good")) tip = cost * 0.15;
          else if (service.equals ("Excellent")) tip = cost * .2;
          else tip = 0;
     } // calculateTip

     // calculateTotal computes the tip, tax and total bill.
     public void calculateTotal ()
     {
          calculateTip ();
          tax = cost * 0.0825;
          total = cost + tax + tip;
     } // calculateTotal
 
      // displayBill prints out an itemized bill.
      public void displayBill ()
     {
          System.out.println ("Service: " + service);
          System.out.println ("Cost: $" + decimals (cost));
          System.out.println ("Tax: $" + decimals (tax));
          System.out.println ("Tip: $" + decimals (tip));
          System.out.println ("Total: $" + decimals (total));
     } // method displayData

    // decimals formats a double as a String for output with two decimal places.
     public static String decimals (double num)
     {
          DecimalFormat decFor = new DecimalFormat ();
          decFor.setMaximumFractionDigits (2);
          decFor.setMinimumFractionDigits (2);
          return decFor.format (num);
     } // decimals
} // class Meal