Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java Programming, Creating Bank account. Implement the object model shown in Fig

ID: 665292 • Letter: J

Question

Java Programming, Creating Bank account.

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(just print message if Exception has not been covered).

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.

---------------------------------------------------------------

I am stuck at the checking method. When I try to transfer saving account money to checking account, I cannot pass the saving account value to withdraw.

Thank you for helping!

Explanation / Answer

Answer:

/************* Java Program for Bank Application*******************/

import java.util.*;

import java.io.*;

import java.util.ArrayList;

//ABSTRACT CLASS BankAccount

abstract class BankAccount

{

int account_No;

String account_name;

double accnt_balance;

  

public BankAccount(int account_No1, String account_name1,double accnt_balance1)

{

this.account_No = account_No1;

this.account_name = account_name1;

this.accnt_balance=accnt_balance1;

}

public int getAccnt_id()

{

return account_No;

}

public String getAccount_Name()

{

return account_name;

}

public void deposit(int a_amt)

{

accnt_balance= accnt_balancee+a_amt;

System.out.println("Account deposited with $"+a_amt);

}

public abstract void withdraw(int a_amt);

}

/************************

CLASS SavingsAccount

*************************

*/

class SavingsAccount extends BankAccount

{

public SavingsAccount(int account_No1, String account_name1, double accnt_balance1)

{

super(account_No1, account_name1, accnt_balance1);

  

}

public void withdraw(int a_amt)

{

if(a_amt<=accnt_balance)

{

accnt_balance=accnt_balance-a_amt;

System.out.println("SUCESSFULLY WITHDRAWN!");

}

else

System.out.println("INSUFFICIENT BALANCE");

}

  

public void display()

{

System.out.println("Balance is $"+accnt_balance);

}

}

/**********************

CLASS CheckingAccount

**********************/

class CheckingAccount extends BankAccount

{

Customer a_cust;

public CheckingAccount(int account_No, String account_name,double accnt_balance)

{

super(account_No, account_name, accnt_balance);

  

}

public void withdraw(int a_amt)

{

if(a_amt<=accnt_balance)

{

accnt_balance=accnt_balance-a_amt;

System.out.println("SUCESSFULLY WITHDRAWN!!");

}

else

{

boolean flag12=a_cust.transferMoney(this,a_amt);

if(!flag12)

System.out.println("INSUFFICIENT BALANCE");

}

}

  

public void display()

{

System.out.println("ACCOUNT BALANCE is $"+accnt_balance);

}

}

/****************************

CLASS Customer

****************************/

class Customer

{

int cust_id;

String cust_name;

  

ArrayList<SavingsAccount> savingAccnt=new ArrayList<SavingsAccount>();

ArrayList<CheckingAccount> checkingAcct=new ArrayList<CheclingAccount>();

  

  

public Customer(int a_id, String a_name) {

this.cust_id = a_id;

this.cust_name = a_name;

}

//ADDS new CheckingAccount

public void addCheckingAccount(CheckingAccount chk_accnt)

{

checkingAcct.add(chk_accnt);

}

//ADDS new SavingAccount

public void adddSavingAccount(SavingAccount sav_accnt)

{

savingAccnt.add(sav_accnt);

}

//BOOLEAN METHOD WITHDRAW MONEY FROM SavingsAccount FOR THE CheckingAccount

public boolean transferMoney(CheckingAccount chk_accnt,int a_amt)

{

boolean isMoneyTransfer=false;

for(int i=0;i<a_saving;i++)

{

if(savingAccnt.get(i).getAccnt_id()==chk_accnt.getAccnt_id())

{

a.saving.get(i).withdraw(a_amt);

isMoneyTransfer=true;

}

}

return isMoneyTransfer;

}

  

}

//CLASS Bank

class Bank

{

private ArrayList<BankAccount> bankaccountList=new ArrayList<BankAccount>();

private ArrayList<Customer> cust_customerList=new ArrayList<Customer>();

//ADDS NEW Customer

public void addCustomer(Customer newcust12)

{

cust_customerList.add(newcust12);

}

//ADDS NEW BankAccount

public void addAccount(BankAccount newacc12)

{

bankaccountList.add(newacc12);

}

}

// CLASS BankImplementation

public class BankImplementation {

//main method

public static void main(String[] args)

{

int a_id=110;

String a_name="Arlen";

double a_bal=1000;

//CREATING SAVINGSACCOUNT

SavingsAccount newsaving=new SavingsAccount(a_id, a_name, a_bal);

//CREATING A CUSTOMER

Customer newCust123=new Customer(a_id, a_name);

newCust123.addSavingAccount(newsaving);

//CREATING CHECKINGACCOUNT

CheckingAccount newCheckingAccount=new CheckingAccount(a_id,a_name,1000);

newCust123.addCheckingAccount(newCheckingAccount);

newCust123.newsaving.display();

newCust123.newsaving.withdraw(1000);

newCust123.newsaving.display();

newCust123. newCheckingAccount.display();

newCust123.newCheckingAccount.withdraw(2000);

newCust123. newCheckingAccount.display();

  

}//MAIN ENDS

}//CLASS ENDS HERE

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote