Account.java
Account.java
import java.util.ArrayList;
import java.util.Collections;
/**
* Account class denotes the account details of an individual.
*/
class Account implements Transactionable, Comparable {
private int accountNumber;
private String name;
private float accountBalance;
private boolean isActive;
/**
* Default Constructor to initialize data members.
*
* @param acNum
* @param name
* @param bal
* @param isActive
*/
public Account(int acNum, String name, float bal, boolean isActive) {
this.accountNumber = acNum;
this.name = name;
this.accountBalance = bal;
this.isActive = isActive;
}
/**
* Display the account details
*/
@Override
public String toString() {
return "\nName : " + name + "\nAccount Number :" + accountNumber + " \nAccount Balance : $" + accountBalance
+ " \nStatus : " + (isActive ? "Active" : "Not Active");
}
/**
* Used by sort to compare two accounts
*/
@Override
public int compareTo(Account o) {
return (int) (accountBalance - o.accountBalance);
}
@Override
public float calculateInterestAmount(float timePeriod) {
if (this.accountBalance > 0 && isActive) {
float interestAmt = (interestRate * accountBalance * timePeriod) / 100;
return interestAmt;
}
return 0;
}
@Override
public boolean isAmountWithdrawable(int amount) {
if (accountBalance >= amount && isActive) {
return true;
}
return false;
}
@Override
public void withdrawAmount(int amount) {
if (isAmountWithdrawable(amount)) {
this.accountBalance -= amount;
}
}
public static void main(String[] args) {
// Populating the arraylist with 5 account details.
ArrayList
accounts = new ArrayList();
accounts.add(new Account(10001, "Raj", 100.0f, true));
accounts.add(new Account(10002, "Howard", 1500.0f, true));
accounts.add(new Account(10003, "Sheldon", 96800.0f, false));
...