/** * Storage class to serve as 'superclass' for concrete financial transactions. * It stores and allows access to fields for a 'date', 'amount', 'name', and * 'status'. *

* It defines varoius constants by using the modfier 'final' and defines fields * and methods. Fields, as a general rule, are marked as 'private', which means * that only objects of the same type as this class can access them directly. * Methods are frequently maked as 'public', which means that any class can * access them. Occasionally therer may be a need for 'private' methods, if they * are useful only from within this class. *

* Finally, this class uses 'javadoc' comments to allow for the automatic creation * of some (technical) documentation. */ public class Transaction { /** Constant to indicate an active transaction - contributes to a total balance */ public static final int ACTIVE = 1; /** Constant to indicate a deleted transaction - no contribution to balance */ public static final int DELETED = 2; /** This field stores a valid date as a String */ private String date = null; /** This field stores the amount as a double type */ private double amount = 0.0; /** This field stores the name for the transaction */ private String name = null; /** This field stores the status of this transaction */ private int status = ACTIVE; /** This field stores the type of transaction */ private String type = "Transaction"; /** * The constructor for a transaction. A 'constructor' is a special method whose * name equals the class name and which has no return type. It automatically * executes every time you make a 'new' object from this class (instantiation). */ public Transaction(String date, double amount, String name, String type) { super(); // calling the superclass constructor this.date = date; this.amount = amount; this.name = name; this.status = ACTIVE; this.type = type; } /** * Method to return the value of the 'date' field */ public String getDate() { return date; } /** * Method to return the value of the 'name' field */ public String getName() { return name; } /** * Method to return the value of the 'amount' field. If a transaction is flagged * as 'active' returns the actual amount stored, otherwise returns 0. */ public double getAmount() { if (status == ACTIVE) return amount; else return 0.0; } /** * Method to return the status of this transaction */ public int getStatus() { return status; } /** * Method to set the status of a transaction. The method guarantees that the * status field is always well-defined by 'throwing an exception' so that the * only possible values are ACTIVE or DELETED. */ public void setStatus(int status) { if (status == ACTIVE) this.status = ACTIVE; else if (status == DELETED) this.status = DELETED; else throw new IllegalArgumentException("Invliad transaction status"); } /** * Method to return the value of the 'type' field. */ public String getType() { return type; } /** * This method, which every class should have, returns a convenient String * representation of the this class. It is automatically called when using the * concatenation operation '+' to convert an object to String. */ public String toString() { String s = type + "\n" + "\tDate: " + date + "\n" + "\tName: " + name + "\n" + "\tAmount: " + amount + "\n"; if (status == ACTIVE) s += "\tACTIVE"; else if (status == DELETED) s += "\tDELETED"; else s += "\tundefined!"; return s; } }