import java.util.Date; import java.text.ParseException; import java.text.DecimalFormat; import java.text.DateFormat; /** Superclass for all checkbook transactions, which are 'data storage' classes. @author Bert G. Wachsmuth @version Feb. 2002 */ public class Transaction { /** The MONEY_FORMAT object is used to format a decimal number as a string representing US dollars. See the java.text.DecimalFormat class for details. */ private static final DecimalFormat MONEY_FORMAT = new DecimalFormat("$#,##0.00"); /** The DATE_FORMAT object is used to format a Date object as a date suitable to the current locale. See the java.text.DateFormat class for details. */ private static final DateFormat DATE_FORMAT = DateFormat.getDateInstance(DateFormat.SHORT); private String name; private double amount; private String date; private String type; protected static int numDeposits = 0; protected static int numChecks = 0; protected static int numAdmins = 0; /** Create a transaction with the given name, amount, date, and type. Note that the constructor attempts to parse a string representing a date, a process that may or may not succeed. */ public Transaction(String name, double amount, String date, String type) { super(); DATE_FORMAT.setLenient(false); this.name = name; this.amount = amount; try { this.date = toDate(DATE_FORMAT.parse(date)); } catch(ParseException pe) { this.date = "invalid"; } this.type = type; } public double getAmount() { return amount; } public String getName() { return name; } public String getDate() { return date; } /** Static method to format a double as a string representing US dollars. */ public static String toMoney(double d) { return MONEY_FORMAT.format(d); } /** Static method to format a date d in suitable form. */ public static String toDate(Date d) { return DATE_FORMAT.format(d); } /** Returns a string representation of a transaction. */ public String toString() { return toMoney(amount) + " - " + name + " on " + date + " (" + type +")"; } }