Computer Science 121
Example using doubles, a StringTokenizer, and a package.

// Application for a clerk in a store. It reads the prices, calculates the tax and displays the total bill.
// It uses a StringTokenizer to read all the prices on the same line.
// And it imports a package called simpleIO to read the data.

import java.io.*;
import java.text.*;
import java.util.*;
import simpleIO.KeyboardReader;

// 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;
    private String priceList;

    // Method that reads the price of a product from the keyboard.
    public void readPrices ()
    {
        try
        {
            System.out.print ("Enter prices, separated by spaces. ");
            priceList = KeyboardReader.readString ();
            StringTokenizer prices = new StringTokenizer (priceList);
            while (prices.hasMoreTokens ())
            {
                price = Double.valueOf (prices.nextToken ()).doubleValue ();
                sum = sum + price;
                numberPrices ++;
            }
        } // try block
        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 $" + KeyboardReader.decimals (sum));
        System.out.println ("The tax is $" + KeyboardReader.decimals (tax));
        System.out.println ("The total bill is $" + KeyboardReader.decimals (total));
    } // method displayTotal
} // class Clerk

package simpleIO;

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

public class KeyboardReader
{
    // Reads a double from the keyboard and returns it to the calling method.
    public static double readDouble ()
    {
        double number;
        BufferedReader stdin = new BufferedReader (new InputStreamReader (System.in));
        try
        {
            number = Double.valueOf (stdin.readLine ()).doubleValue ();
        } catch (IOException e)
        { number = 0;}
        return number;
    } // method readDouble

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

    // Reads a string from the keyboard and returns it to the calling method.
    public static String readString ()
    {
        String string;
        BufferedReader stdin = new BufferedReader
        (new InputStreamReader (System.in));
        try
        {
            string = stdin.readLine ();
        } catch (IOException e)
        { string = "";}
        return string;
    } // method readString
} // public class Reader