// 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 ()
{
String morePrices;
try
{
BufferedReader stdin = new BufferedReader (new InputStreamReader (System.in));
do
{
System.out.print ("Price: ");
price = Double.valueOf (stdin.readLine ()).doubleValue ();
sum = sum + price;
numberPrices ++;
System.out.print ("Is there another item? y/n ");
morePrices = stdin.readLine ();
} while (morePrices.equals ("y"));
} // 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