samples.webapps.bookstore.util.Currency.java Source code

Java tutorial

Introduction

Here is the source code for samples.webapps.bookstore.util.Currency.java

Source

/*
 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
 * Use is subject to license terms.
 */

package samples.webapps.bookstore.util;

import java.text.NumberFormat;
import java.util.Locale;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class Currency {

    private Locale locale;
    private double amount;
    private Log currencyLog = LogFactory.getLog(Currency.class);

    public Currency() {
        locale = null;
        amount = 0.0;
    }

    public synchronized void setLocale(Locale l) {
        locale = l;
        currencyLog.info("local set" + this.locale);
    }

    public synchronized void setAmount(double a) {
        amount = a;
        currencyLog.info("amount set" + this.amount);
    }

    // NEWLY ADDED FOR AMOUNT DISPLAY WITHOUT CURRENCY
    public synchronized double getAmount() {
        return amount;

    }

    //UPTO HERE 
    public synchronized String getFormat() {
        currencyLog.info("locale is:" + locale);
        NumberFormat nf = NumberFormat.getCurrencyInstance(locale);
        currencyLog.info("number format" + nf);
        currencyLog.info("format is" + nf.format(amount));
        return nf.format(amount);

    }

}