Good Morning! I have a bit of a problem with my code. First, let me show you the
ID: 665613 • Letter: G
Question
Good Morning! I have a bit of a problem with my code. First, let me show you the instructions:
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.
And now, my code :
import java.util.*;
import java.io.*;
import java.util.ArrayList;
//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);
}
}
import java.util.*;
import java.io.*;
import java.util.ArrayList;
/****************************
CLASS Customer
****************************/
class Customer extends Bank
{
int cust_id;
String cust_name;
ArrayList<SavingsAccount> savingAccnt=new ArrayList<SavingsAccount>();
ArrayList<CheckingAccount> checkingAcct=new ArrayList<CheckingAccount>();
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 addSavingAccount(SavingsAccount sav_accnt)
{
savingAccnt.add(sav_accnt);
}
//BOOLEAN METHOD WITHDRAW MONEY FROM SavingsAccount FOR THE CheckingAccount
public boolean transferMoney(CheckingAccount chk_accnt,double a_amt)
{
boolean isMoneyTransfer=false;
for(SavingsAccount sa:savingAccnt)
{
if(sa.getAccnt_id()==chk_accnt.getAccnt_id())
{
sa.withdraw(a_amt);
isMoneyTransfer=true;
}
}
return isMoneyTransfer;
}
}
//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(double a_amt)
{
accnt_balance= accnt_balance+a_amt;
System.out.printf(" Account deposited with $%.2f ",a_amt);
}
public abstract void withdraw(double a_amt);
}
/**********************
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(double a_amt)
{
if(a_amt<=accnt_balance)
{
accnt_balance=accnt_balance-a_amt;
System.out.printf(" $%.2f SUCESSFULLY WITHDRAWN FROM CHECKING!! ",a_amt);
}
else
{
boolean flag12=a_cust.transferMoney(this,a_amt);
if(!flag12)
System.out.println("INSUFFICIENT BALANCE");
}
}
public void display()
{
System.out.printf(" CHECKING ACCOUNT BALANCE is $%.2f",accnt_balance);
}
}
/************************
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(double a_amt)
{
if(a_amt<=accnt_balance)
{
accnt_balance=accnt_balance-a_amt;
System.out.printf(" $%.2f SUCESSFULLY WITHDRAWN FROM SAVINGS!!",a_amt);
}
else
System.out.println("INSUFFICIENT BALANCE");
}
public void display()
{
System.out.printf(" SAVINGS ACCOUNT BALANCE is $%.2f ",accnt_balance);
}
}
// CLASS BankImplementation
public class BankImplementation {
//main method
public static void main(String[] args)
{
int a_id=110;
String a_name="Arlen";
double a_bal=5000;
//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);
newsaving.display();
newsaving.deposit(5000);
newsaving.display();
newsaving.withdraw(9000);
newsaving.display();
newCheckingAccount.display();
newCheckingAccount.deposit(2000);
newCheckingAccount.display();
newCheckingAccount.withdraw(2000);
//newCust123.transferMoney(newCheckingAccount, 1000);
newCheckingAccount.display();
}//MAIN ENDS
}//CLASS ENDS HERE
I am not able to transfer money directly from the savings account into the checking account whenever the checking account balance falls below zero. I have tried this using public boolean transferMoney and //newCust123.transferMoney(newCheckingAccount, 1000) after purposefully withdrawing more money than I had in my checking account in order to see if it worked. Instead I received a Null Pointer Exception. I know something is up but I can't seem to figure out what.
I would greatly appreciate it if you could find out where I went wrong and correct it PLEASE, using JAVA. Oh, and make sure the code compiles and runs before you send it to me!!!
CHEEEERS!
Bank BankAccount accountNo owner balance accountList has O..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 Object model for a banking applicationExplanation / Answer
import java.util.Scanner;
public class BankAccount {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int userChoice;
boolean quit = false;
float balance = 0f;
do {
System.out.println("1. Deposit money");
System.out.println("2. Withdraw money");
System.out.println("3. Check balance");
System.out.print("Your choice, 0 to quit: ");
userChoice = in.nextInt();
switch (userChoice) {
case 1:
float amount;
System.out.print("Amount to deposit: ");
amount = in.nextFloat();
if (amount <= 0)
System.out.println("Can't deposit nonpositive amount.");
else {
balance += amount;
System.out.println("$" + amount + " has been deposited.");
}
break;
case 2:
System.out.print("Amount to withdraw: ");
amount = in.nextFloat();
if (amount <= 0 || amount > balance)
System.out.println("Withdrawal can't be completed.");
else {
balance -= amount;
System.out.println("$" + amount + " has been withdrawn.");
}
break;
case 3:
System.out.println("Your balance: $" + balance);
break;
case 0:
quit = true;
break;
default:
System.out.println("Wrong choice.");
break;
}
System.out.println();
} while (!quit);
System.out.println("Bye!");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.