//Computer Science 121
//Example using doubles.

// Application for a clerk in a store. It reads the price, calculates the tax and displays the total bill.
import java.io.*;
import java.text.*;

// Main class that declares a class that describes a store clerk and calls the methods.
public class Store
{
    public static void main (String [] args)
    {
        Clerk storeClerk = new Clerk ();
        storeClerk.readPrices ();
        storeClerk.calculateTax ();
        storeClerk.displayTotal ();
    } // method main
} // class Store

// Class that defines the work of a store clerk.
class Clerk
{
    private double price, sum = 0, tax, total;
    private int numberPrices = 0;

    // Method that reads the price of a product from the keyboard.
    public void readPrices ()
    {
        try
        {
            BufferedReader stdin = new BufferedReader (new InputStreamReader (System.in));
            System.out.println ("Enter prices ending with a zero price.");
            System.out.print ("Enter the price: ");
            price = Double.valueOf (stdin.readLine ()).doubleValue ();
            while (price > 0)
            {
                System.out.print ("Enter the price: ");
                sum = sum + price;
                numberPrices ++;
                price = Double.valueOf (stdin.readLine ()).doubleValue ();
            }
        } // try block
        catch (IOException e) { System.out.println ("I/O error");}
        catch (NumberFormatException ex) { System.out.println ("Number format error.");}
    } // method getPrice

    // Method that calculates the tax.
    public void calculateTax ()
    {
        final double taxRate = 0.0825;
        tax = sum * taxRate;
    } // method calculateTax

    // Method that calculates and then displays the total bill.
    public void displayTotal ()
    {
        total = tax + sum;
        System.out.println ("The number of items bought is " + numberPrices);
        System.out.println ("The sum is $" + decimals (sum));
        System.out.println ("The tax is $" + decimals (tax));
        System.out.println ("The total bill is $" + decimals (total));
    } // method displayTotal

    // Formats a double for string output with two decimal places.
    public String decimals (double num)
    {
        DecimalFormat decFor = new DecimalFormat ();
        decFor.setMaximumFractionDigits (2);
        decFor.setMinimumFractionDigits (2);
        return decFor.format (num);
    } // method decimals
} // class Clerk