package edu.columbia.cs.cs1007.banking;
/**
* @author Julia Stoyanovich (jds2109@columbia.edu)
* COMS 1007, Summer 2009
*
* The class Transaction implements a banking transaction.
*
*/
import java.util.Calendar;
import java.util.Date;
public class Transaction {
// Class variables.
public static enum TRANSACTION_TYPE {DEPOSIT, WITHDRAWAL, INTEREST};
// Instance variables.
private TRANSACTION_TYPE _type;
private double _amount;
private Date _date;
/**
* Constructor.
* @param type
* @param amount
* @param date
*/
public Transaction(TRANSACTION_TYPE type, double amount, Date date) {
_type = type;
_amount = amount;
_date = date;
}
/**
* Contructor that defaults the date to current date.
* @param type
* @param amount
*/
public Transaction(TRANSACTION_TYPE type, double amount) {
_type = type;
_amount = amount;
_date = Calendar.getInstance().getTime();
}
/**
* Contructor that defaults the date to current date
* and the amount to 0.
* @param type
*/
public Transaction(TRANSACTION_TYPE type) {
_type = type;
_amount = 0;
_date = Calendar.getInstance().getTime();
}
/**
* Contructor that defaults the amount to 0.
* @param type
*/
public Transaction(TRANSACTION_TYPE type, Date date) {
_type = type;
_amount = 0;
_date = date;
}
/**
* Constructor that initializes "this" from another instance of Transaction.
* @param that
*/
public Transaction(Transaction that) {
_type = that._type;
_amount = that._amount;
_date = that._date;
}
/**
* Get transaction type.
* @return _type
*/
public TRANSACTION_TYPE getType() {
return _type;
}
/**
* Get transaction amount.
* @return _amount
*/
public double getAmount() {
return _amount;
}
/**
* Get transaction date.
* @return _date
*/
public Date getDate() {
return _date;
}
/**
* Set transaction amount.
* @param amount
*/
public void setAmount(double amount) {
_amount = amount;
}
/**
* Generate a string representation of the transaction.
* @return stringRepresentation
*/
public String toString() {
String stringRepresentation = _date.toString() + " : ";
switch (_type) {
case DEPOSIT :
stringRepresentation += "Deposit in the amount of " + _amount + " applied to account.";
break;
case WITHDRAWAL :
stringRepresentation += "Withdrawal in the amount of " + _amount + " applied to account.";
break;
case INTEREST :
stringRepresentation += "Interest of " + String.format("%.7f", _amount) + " percent applied to account.";
break;
default :
stringRepresentation += "Unsupported transaction type";
break;
}
return stringRepresentation;
}
public static void main(String[] args) {
Transaction deposit = new Transaction(TRANSACTION_TYPE.DEPOSIT, 100, new Date());
Transaction withdrawal = new Transaction(TRANSACTION_TYPE.WITHDRAWAL, 123.45, new Date());
Transaction interest = new Transaction(TRANSACTION_TYPE.INTEREST, 0.0001, new Date());
System.out.println(deposit.toString());
System.out.println(withdrawal.toString());
System.out.println(interest.toString());
}
}
|