22.7. Banking Example: Class Cashflow

public class Cashflow {
    private Date   date;
    private double amount;

    public Cashflow() {
    }

    public Cashflow(Date date, double amount) {
        this.date = date;
        this.amount = amount;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public double getAmount() {
        return amount;
    }

    public void setAmount(double amount) {
        this.amount = amount;
    }

    public String toString() {
        return "Cashflow[date=" + date + ",amount=" + amount + "]";
    }
}
  • Class Cashflow has two simple attributes, a date and an amount. (Using the type double for monetary units is generally bad practice because floating point numbers cannot represent most numbers accurately.)
  • There is an overloaded constructor to set the values and a method toString to print a cashflow.