//Computer Science Example
//Reading prices from the keyboard and storing them in an array.

import java.io.*;
import java.util.*;
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.readPricesFromKeyboard ();  // or storeClerk.readPricesFromString ();
          storeClerk.displayTotal ();
     } // method main
} // class Store

// Class that defines the work of a store clerk.
class Clerk
{
     private int numberItems = 0;
     private double [] prices = new double [10];

     /* Method that reads the prices of a list of items from the keyboard.
     It uses a StringTokenizer to get the individual prices and add them up.*/
     public void readPricesFromString ()
     {
          double price;
          String priceList;
          int count = 0;
          try
          {
               BufferedReader stdin = new BufferedReader (new InputStreamReader (System.in));
               System.out.print ("Enter prices, separated by spaces. ");
               priceList = stdin.readLine ();
               StringTokenizer priceString = new StringTokenizer (priceList);
               while (priceString.hasMoreTokens ())
               {
                    price = Double.parseDouble (priceString.nextToken ());
                    prices [count] = price;
                    count ++;
               }
               numberItems = count;
          } // try block
          catch (NumberFormatException ex) { System.out.println ("Number format error.");}
          catch (IOException e) {System.out.println ("IO Exception");}
     } // method readPrices
 

 /* Method that reads the prices of a list of items from the keyboard.
 It prompts for and then reads each price from the keyboard, stopping when a zero is read.*/
     public void readPricesFromKeyboard ()
     {
          double price;
          int count = 0;
          try
          {
               BufferedReader stdin = new BufferedReader (new InputStreamReader (System.in));
               System.out.print ("Enter first price: ");
               price = Double.parseDouble (stdin.readLine ());
               while (price > 0)
               {
                    prices [count] = price;
                    count ++;
                    System.out.print ("Enter next price: ");
                    price = Double.parseDouble (stdin.readLine ());
               }
               numberItems = count;
          } // try block
          catch (NumberFormatException ex) { System.out.println ("Number format error.");}
          catch (IOException e) {System.out.println ("IO Exception");}
     } // method readPrices

     // Method that calculates the total and returns the value.
     private double calculateTotal ()
     {
          double sum = 0;
          for (int count = 0; count < numberItems; count ++)
           sum = sum + prices [count];
          return sum;
     } // method calculateTax
 
     // Method that calculates and then displays the total bill.
     public void displayTotal ()
     {
          double total = calculateTotal ();
          double tax = total * 0.0825;
          double totalBill = total + tax;
          System.out.println ("The number of items bought is " + numberItems);
          System.out.println ("The total is $" + decimals (total));
          System.out.println ("The tax is $" + decimals (tax));
          System.out.println ("The total bill is $" + decimals (totalBill));
     } // method displayTotal
 
     // Formats a double for string 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 Clerk