I am working on a project creating a "cash register" and I need to format prices to two decimal places. But I can not figure out how to add a number format and instantiate it to the constructor. Any advice?
Java Code:
import java.util.*;
import java.text.*;
import java.util.NumberFormat;
/*******************************************************************************
* Create a simple class for a cash register.
*
* @author George Glessner
* @version January 26, 2015
*******************************************************************************/
public class cashRegister{
/** current amount due */
private double currentAmount;
/** daily sales */
private double dailySales;
/** number of customers */
private int customers;
/** store name */
private String storeName;
/** sales tax */
private final double salesTax;
/** number format */
NumberFormat fmt = NumberFormat.getCurrencyInstance();
/***************************************************************************
Constructor for objects of class Register
****************************************************************************/
public cashRegister(){
currentAmount = 0.00;
dailySales = 0.00;
customers = 0;
storeName = "George's Corner Store!";
salesTax = 0.06;
}
/**************************************************************************
Constructor for objects of class Register
Alternative constructor
@param pName name of store
***************************************************************************/
public cashRegister(String pName){
currentAmount = 0.00;
dailySales = 0.00;
customers = 0;
storeName = pName;
salesTax = 0.06;
}
/***************************************************************************
Scan an item's price
@param price total price of object
****************************************************************************/
public void price(double price){
NumberFormat fmt = NumberFormat.getCurrencyInstance();
price += currentAmount;
System.out.println("Price: $" + fmt.format(price));
}
/***************************************************************************
Get current amount due
@return price of item
****************************************************************************/
public double getPrice(){
return currentAmount;
}
}
No comments:
Post a Comment