FileDocCategorySizeDatePackage
Account.javaAPI DocExample2349Thu Jun 27 19:20:22 BST 2002com.oreilly.mock

Account.java

package com.oreilly.mock;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.PrintWriter;
import java.text.NumberFormat;

public class Account {
    public static final int CHECKING = 0;
    public static final int SAVINGS = 1;

    private int acctType;
    private String acctNumber;
    private double balance;

    private PropertyChangeSupport changes = new PropertyChangeSupport(this);

    static NumberFormat numFormat = NumberFormat.getNumberInstance();

    static {
        numFormat.setGroupingUsed(false); // turn off commas
        numFormat.setMinimumFractionDigits(2);
        numFormat.setMaximumFractionDigits(2);
    }

    public Account(int acctType, String acctNumber, double balance) {
        this.acctType = acctType;
        this.acctNumber = acctNumber;
        this.balance = balance;
    }

    public int getAccountType() {
        return this.acctType;
    }

    public String getAccountNumber() {
        return this.acctNumber;
    }

    public double getBalance() {
        return this.balance;
    }

    public void setBalance(double balance) {
        Double oldBalance = new Double(this.balance);
        this.balance = balance;
        this.changes.firePropertyChange("balance",
                oldBalance, new Double(balance));
    }

    public void addPropertyChangeListener(PropertyChangeListener l) {
        this.changes.addPropertyChangeListener(l);
    }

    public void removePropertyChangeListener(PropertyChangeListener l) {
        this.changes.removePropertyChangeListener(l);
    }

    public void writeCsv(PrintWriter pw) {
        pw.print(typeToString(this.acctType));
        pw.print(",");
        pw.print(this.acctNumber);
        pw.print(",");
        pw.print(balanceToString(this.balance));
    }

    static String balanceToString(double balance) {
        return numFormat.format(balance);
    }

    static String typeToString(int acctType) {
        switch (acctType) {
            case CHECKING:
                return "CHECKING";
            case SAVINGS:
                return "SAVINGS";
            default:
                throw new IllegalArgumentException(
                        "Unknown account type: " + acctType);
        }
    }
}