Class Account
java.lang.Object
bank.Account
- All Implemented Interfaces:
Serializable, Comparable<Account>
This class represents the relationship between a bank and its customers
(account holders). An account has one or more holders and has one current
account and 0 (zero) or more savings accounts.
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidAdd an account holder.final voidaddSavingsAccount(double amount, double rate, int duration) final booleanVerifies whether an account can be removed.intCompares account numbers.voidcreateSavingsAccount(double amount, double rate, int time) doublevoiddeposit(double amount) Makes a deposit in the current account.booleanTwo accounts are considered equal when they have the same account number.final intholders()Returns the holders of the account.voidremoveSavingsAccount(SavingsAccount account) Remove a given savings account.savingsAccount(int number) Returns the savings accounts associated with the bank account.Returns the savings accounts associated with the bank account.toString()String representation of the account: presents the account number and the total balance.doubleReturns the total balance for the account.voidwithdraw(double amount) Performs a withdrawal.
-
Constructor Details
-
Account
public Account(int number, double amount) - Parameters:
number- account numberamount- initial amount for the current account.
-
-
Method Details
-
getNumber
public final int getNumber()- Returns:
- the account number.
-
compareTo
Compares account numbers.- Specified by:
compareToin interfaceComparable<Account>
-
equals
-
deposit
Makes a deposit in the current account.- Parameters:
amount- the amount to be deposited.- Throws:
InvalidDepositException- if it is not possible to make the deposit.NegativeAmountException- if the amount is not positive.
-
withdraw
public void withdraw(double amount) throws InvalidWithdrawalException, NegativeAmountException, BalanceTooLowException Performs a withdrawal.- Parameters:
amount- the amount to be withdrawn.- Throws:
InvalidWithdrawalException- if it is not possible to withdraw the intended amount.NegativeAmountException- if the amount is not positive.BalanceTooLowException
-
currentAccountBalance
public double currentAccountBalance()- Returns:
- the amount in the current account.
-
totalBalance
public double totalBalance()Returns the total balance for the account. This balance includes the current account balance and the balances of all the savings accounts. The following implementation uses an implicit iterator, but the same actions could be performed using an explicit one.- Returns:
- the account balance.
-
holders
-
addSavingsAccount
public final void addSavingsAccount(double amount, double rate, int duration) throws InvalidWithdrawalException, NegativeAmountException, BalanceTooLowException - Parameters:
amount-rate-duration- duration in days- Throws:
BalanceTooLowExceptionNegativeAmountExceptionInvalidWithdrawalException
-
createSavingsAccount
public void createSavingsAccount(double amount, double rate, int time) throws InvalidWithdrawalException, NegativeAmountException, BalanceTooLowException - Parameters:
amount-rate-time-- Throws:
BalanceTooLowExceptionNegativeAmountExceptionInvalidWithdrawalException
-
savingsAccount
Returns the savings accounts associated with the bank account.- Parameters:
number- the account number- Returns:
- the savings account
- Throws:
NoSuchSavingsAccountException
-
savingsAccounts
Returns the savings accounts associated with the bank account.- Returns:
- a list of savings deposits or null (if there are no savings deposits.
-
removeSavingsAccount
Remove a given savings account.- Parameters:
account- the savings account to be removed.
-
addHolder
Add an account holder.- Parameters:
holder- the holder to be added.
-
canBeRemoved
public final boolean canBeRemoved()Verifies whether an account can be removed. An account can be removed only when its total balance is 0 (zero).- Returns:
- true, if the account can be removed; false, otherwise.
-
toString
-