import java.util.*;
import java.text.*;
// Main class that declares a class that describes a store clerk and
calls the methods.
public class StoreTokenizer
{
public static void main (String [] args)
{
Clerk storeClerk
= new Clerk ();
storeClerk.readPrices
();
storeClerk.displayTotal
();
} // method main
} // class Store
// Class that defines the work of a store clerk.
class Clerk
{
private int numberItems = 0;
private double total = 0;
// Method that reads the price of a product
from the keyboard using a StringTokenizer.
public void readPrices ()
{
double price;
String priceList;
try
{
System.out.print ("Enter prices, separated by spaces. ");
priceList = Reader.readString ();
StringTokenizer priceString = new StringTokenizer (priceList);
while (priceString.hasMoreTokens ())
{
price = Double.parseDouble (priceString.nextToken ());
total = total + price;
numberItems ++;
}
} // try block
catch (NumberFormatException
ex) { System.out.println ("Number format error.");}
} // method getPrice
// Method that calculates the tax.
private double calculateTax (double sum)
{
final double
taxRate = 0.0825;
double tax =
sum * taxRate;
return tax;
} // method calculateTax
// Method that calculates and then displays
the total bill.
public void displayTotal ()
{
double tax =
calculateTax (total);
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);
} // method decimals
} // class Clerk