Hullo! Please write the code for the following program using JAVA! Be sure to re
ID: 665997 • Letter: H
Question
Hullo! Please write the code for the following program using JAVA!
Be sure to read CAREFULLY before proceeding!!!
When done PLEASE compile and run to make sure the code WORKS!!!
Thanks.
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.
Bank BankAccount accountNo owner balance accountList has 0..n customerList addCustomer addAccount deposit abstract withdraw has SavingsAccount CheckingAccount withdravw withdravw 0..n Customer id savingsAcct checkingAcct 0..1 0..1 has addSavingsAccount addCheckingAccount Figure 1-25 Obiect model for a banking applicationExplanation / Answer
The required classes are created below as per the requirement.
-----------------------------------------------
Bank.java
------------------------------------------------
public class Bank
{
private List<BankAccount> accountList;
private List<Customer> customerList;
public Bank()
{
this.accountList = new ArrayList<BankAccount>();
this.customerList = new ArrayList<Customer>();
}
public void addCutomer(Customer customer)
{
this.customerList.add(customer);
}
public void addAccount(BankAccount account)
{
this.accountList.add(account);
}
public void withdraw(string acountNo, string customerId, double amount)
{
foreach(Customer customer in this.customerList)
{
if(customer.Id == customerId)
{
if(customer.getSavingsAccount().getAcountNo() == acountNo)
{
if (customer.getSavingsAccount().getBalance() >= amount)
{
customer.getSavingsAccount().withdraw(amount);
Console.out.Println("Withdrawn succeded for Customer [{0}] from Savings acount for amount [{1}]", customerId, amount);
}
else
{
Console.out.Println("Balance is less than the amount to be drawn");
}
}
else if (customer.getCheckingAccount().getAcountNo() == acountNo)
{
if (customer.getCheckingAccount().getBalance() >= amount)
{
customer.getCheckingAccount().withdraw(amount);
Console.out.Println("Withdrawn succeded for Customer [{0}] from Savings acount for amount [{1}]", customerId, amount);
}
else
{
if(customer.getSavingsAccount() != null)
{
double balancesInSaving = customer.getSavingsAccount().getBalance();
double balancesInChecking = customer.getCheckingAccount().getBalance();
if (balancesInSaving + balancesInChecking < amount)
{
Console.out.Println("Insufficient funds");
return;
}
double shortFall = amount - balancesInChecking;
customer.getSavingsAccount().withdraw(shortFall);
customer.getCheckingAccount().deposit(shortFall);
customer.getCheckingAccount().withdraw(amount);
}
Console.out.Println("Balance is less than the amount to be drawn");
}
}
}
}
}
public void deposit(string acountNo, string customerId, double amount)
{
foreach (Customer customer in this.customerList)
{
if (customer.Id == customerId)
{
if (customer.getSavingsAccount().getAcountNo() == acountNo)
{
customer.getSavingsAccount().deposit(amount);
Console.out.Println("Deposited in savings acount");
}
else if (customer.getCheckingAccount().getAcountNo() == acountNo)
{
customer.getCheckingAccount().deposit(amount);
Console.out.Println("Deposited in checking acount");
}
}
}
}
public static void main(string[] args)
{
Bank bank = new Bank();
Customer customer = new Customer("100");
customer.addSavingsAccount("ABC");
bank.deposit("ABC","100",500);
bank.withdraw("ABC","100", 100);
customer.addCheckingAccount("CDE");
bank.deposit("CDE", "100", 200);
bank.withdraw("CDE","100", 200);
//
bank.deposit("CDE", "100", 200);
bank.withdraw("CDE", "100", 300);
//
bank.deposit("CDE", "100", 200);
bank.withdraw("CDE", "100", 1000);
}
}
-----------------------------------------------------
BankAccount.java
-----------------------------------------------------
public abstract class BankAccount
{
private string accountNo;
private string owner;
private double balance;
public BankAccount(string accountNo, string id)
{
this.accountNo = accountNo;
this.owner = id;
this.balance = 0;
}
public void deposit(double amount)
{
this.balance += amount;
}
public abstract void withdraw(double amount);
public double getBalance()
{
return this.balance;
}
public string getAcountNo()
{
return this.accountNo;
}
public string getOwner()
{
return this.owner;
}
protected void setBalance(double amount)
{
this.balance = amount;
}
}
--------------------------------------------
SavingsAccount.java
----------------------------------------------
public class SavingAccount extends BankAccount
{
public SavingAccount(string accountNo, string id):super(accountNo, id){}
@override
public void withdraw(double amount)
{
double currentBalance = this.GetBalance();
if(amount <= currentBalance)
{
double balance = currentBalance - amount;
this.setBalance(balance);
}
}
}
-------------------------------------------------
CheckingAccount.java
------------------------------------------------
public class CheckingAccount extends BankAccount
{
public CheckingAccount(string accountNo, string id) : super(accountNo, id) { }
@override
public void withdraw(double amount)
{
double balance = this.GetBalance();
if (amount <= balance)
{
double balance = currentBalance - amount;
this.setBalance(balance);
}
}
}
-----------------------------------------
Customer.java
--------------------------------------------
public class Customer
{
private string id;
private BankAccount savingsAccount;
private BankAccount checkingAccount;
public Customer(string id)
{
this.id = id;
}
public string getId()
{
return this.id;
}
public BankAccount getSavingsAccount()
{
return this.savingsAccount;
}
public BankAccount getCheckingAccount()
{
return this.checkingAccount;
}
public void addSavingsAccount(string accountNo)
{
this.savingsAccount = new SavingAccount(accountNo, this.id);
}
public void addCheckingAccount(string accountNo)
{
this.checkingAccount = new CheckingAccount(accountNo, this.id);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.