JAVA Implement the object model shown in Figure 1-25 to implement a small bankin
ID: 3847539 • Letter: J
Question
JAVA
Implement the object model shown in Figure 1-25 to implement a small banking application. Create five classes: Bank, BankAccount, SavingsAccount, CheckingAccount, and Customer. Bank Should be a singleton class. For simplicity, the bank stores bank accounts in one array and customers in another. All bank accounts are savings accounts or checking accounts, and any customer may have zero or one account of each type.
The difference between the two accounts affects only the withdraw method. The deposit method simply increments the balance. The withdraw method decrements the balance, but cannot always do so. A customer can withdraw from a SavingsAccount only an amount less than or equal to the current balance. For a CheckingAccount the logic is:
§ Withdraw the amount if less than or equal to the balance.
§ If the customer has a SavingsAccount, and the SavingsAccount has enough money to cover the shortfall in the CheckingAccount, transfer enough money from saving to checking to cover the withdrawal. Then withdraw the money.
§ If the customer has no savings account, or the total of both accounts is less than the withdrawal amount, issue an InsufficientFundsException exception.
Add a main method to the Bank class to test. Create one customer and give that customer a savings account. Try a deposit and withdrawal. Give the customer a checking account and try another deposit. Try withdrawals that succeed without a transfer, that transfer founds from savings, and for which there are insufficient funds.
BankAccount Bank account No account List has 0..n Owner customer List balance addCustomer deposit abstract withdraw add Account has Savings Account CheckingAccount withdraw withdraw 0..n Customer 0..1 0..1 savings Acct has checking Acct has add Savings Account addCheckingAccount Figure 1-25 Object model for a banking applicationExplanation / Answer
Hi please find the solution for your question.I have given my maximum effort to clear your doubt.
Bank.java:
import java.util.ArrayList;
import java.util.List;
public class Bank {
private final Bank bank = new Bank();
private Bank() {
}
private Bank getInstance() {
return bank;
}
private List<BankAccount> bankAccounts = new ArrayList<>();
private List<Customer> customers = new ArrayList<>();
public void addCustomer(Customer customer) {
customers.add(customer);
}
public void addAccount(BankAccount bankAccount) {
bankAccounts.add(bankAccount);
}
public static void main(String[] args) throws InsufficientFundsException {
//creating object of savings account
SavingsAccount savingsAccount = new SavingsAccount();
savingsAccount.accountNo = 456L;
savingsAccount.balance = 1000.0;
savingsAccount.owner = "Sateesh";
//creating object of customer
Customer customer = new Customer();
customer.id = 123L;
customer.savingsAccount = savingsAccount;
customer.savingsAccount.deposit(1000.0);
System.out.println("Withdrawing amount: " + 200);
customer.savingsAccount.withdraw(200.0);
//creating object of checking account
CheckingAccount checkingAccount = new CheckingAccount();
checkingAccount.accountNo = 785L;
checkingAccount.balance = 1000.0;
checkingAccount.owner = "Sateesh";
customer.checkingAccount = checkingAccount;
customer.checkingAccount.deposit(100.0);
double withdraw = 1000.0;
boolean status = customer.checkingAccount.withdraw(withdraw);
status = customer.checkingAccount.withdraw(withdraw);
if (!status) {
//checks account status
if(customer.savingsAccount!=null){
transfer(customer, withdraw);
} else {
//throws exception if there is insufficient balance
throw new InsufficientFundsException("Insufficient Funds");
}
}
}
private static void transfer(Customer customer, double withdraw) {
System.out.println("Transferring Amount from Savings to Checking Account");
if (customer.savingsAccount.balance >= withdraw) {
Double savingAmount = customer.savingsAccount.balance;
Double remainingAmount = savingAmount - withdraw;
customer.checkingAccount.balance = withdraw;
customer.savingsAccount.balance = remainingAmount;
customer.checkingAccount.withdraw(withdraw);
}
}
}
BankAccount.java:
public abstract class BankAccount {
// data members of the class
Long accountNo;
String owner;
Double balance;
// deposits amount to the particular account
public void deposit(Double amount) {
this.balance = balance + amount;
System.out.println("Current Balance After deposit:" + balance);
}
// withdraws amount from particular account- to be implemented by the sub
// classes
public abstract boolean withdraw(Double amount);
}
SavingsAccount.java:
public class SavingsAccount extends BankAccount {
@Override
public boolean withdraw(Double withdraw) {
//checking the balance for withdrawal
if (withdraw <= balance) {
balance = balance - withdraw;
System.out.println("Remaining balance Savings Account:" + balance);
return true;
}
return false;
}
}
CheckingAccount.java:
public class CheckingAccount extends BankAccount{
@Override
public boolean withdraw(Double withdraw) {
//Checks for the balance to be withdrawn
if (withdraw <= balance) {
balance = balance - withdraw;
System.out.println("remaining balance CheckingAccount:" + balance);
return true;
}
return false;
}
}
Customer.java:
public class Customer {
Long id;
SavingsAccount savingsAccount;
CheckingAccount checkingAccount;
public void addSavingsAccount(SavingsAccount savingsAccount){
this.savingsAccount = savingsAccount;
}
public void addCheckingAccount(CheckingAccount checkingAccount){
this.checkingAccount = checkingAccount;
}
}
InsufficientFundException.java:
public class InsufficientFundsException extends Exception {
private static final long serialVersionUID = 1L;
public InsufficientFundsException(String errorMessage) {
super(errorMessage);
}
}
As you have not mentioned the limit for checkingAccount I am giving solution as per my assumption of parameters.Hope you will get satisfied with the answer.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.