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

Please help creating a Java programming class called BankAccount. *NOTE - I have

ID: 667829 • Letter: P

Question

Please help creating a Java programming class called BankAccount.

*NOTE - I have found similar questions asked on Chegg, but with the incorrect answer and with incorrect output.

The BankAccount class should contain a String to store the customer name and a double to store the account balance. The BankAccount class should have two constructors, as follows:

public BankAccount(String name, double balance)
           throws NegativeAmountException
{
    // set name and balance
    // make sure balance is not negative
    // throw exception if balance is negative
}
public BankAccount(String name)
           throws NegativeAmountException
{
    // set name and use 0 balance
}

As can be seen, the first constructor throws a NegativeAmountException if the balance being used to create the bank account is negative. You will have to create this exception class yourself.

The BankAccount class should also contain methods to make a deposit, make a withdrawal, get the current balance, and print a bank account statement. The interfaces for these methods should appear as follows:

// update balance by adding deposit amount
// make sure deposit amount is not negative
// throw exception if deposit is negative

public void deposit(double amount) throws NegativeAmountException
  // update balance by subtracting withdrawal amount
// throw exception if funds are not sufficient
// make sure withdrawal amount is not negative
// throw NegativeAmountException if amount is negative
// throw InsufficientFundsException if balance < amount
  
  public void withdraw(double amount)
                throws InsufficientFundsException, NegativeAmountException
// return current balance
  
  public double getBalance()
// print bank statement including customer name
// and current account balance
  public void printStatement();

Use the BankAccount class as the superclass for a SavingsAccount class. In addition to the behaviors of a BankAccount, a SavingsAccount also accumulates interest; therefore, the SavingsAccount class contains a double that is populated with the current interest rate. In addition to its constructors (you decide what the constructors should be), the SavingsAccount class should contain the following methods:

  // post monthly interest by multiplying current balance
// by current interest rate divided by 12 and then adding
// result to balance by making deposit
  
  public void postInterest()
// print bank statement including customer name
// and current account balance (use printStatement from
// the BankAccount superclass)
// following this also print current interest rate
  
  public void printStatement()

Once these two classes are completed, create a driver class called FinalVerification containing a main method that tests the SavingsAccount class. Within the driver test class, create a SavingsAccount object and then use it to make deposits and withdrawals, and to post the monthly interest.

To make the program simpler, you can incorporate the initial data for the Savings Accounts directly in the program (e.g., no need to prompt for the account holder name or starting balance). The only things you need to prompt for are the deposit amount and the withdrawal amount. Also, to simplify the task, the only exceptions that you should handle are the NegativeAmountException and the InsufficientFundsException. If either of these exception conditions occurs, print an appropriate error message and terminate the application. You can simply re-throw any IOExceptions from the main.

Explanation / Answer

import java.util.Scanner;
import java.util.Random;
import java.lang.Exception;
class MyException extends Exception
{
MyException(String message)
{
super(message);
}
}
interface Account
{
void CreateAcc();
void Deposit();
void Withdraw();
}
class BankUtilities
{
int acctype,year=1,ssn=0,accnum;
float intr;
String name="";
String loc="";
Scanner in = new Scanner(System.in);
Random rnd = new Random();
double temp=0.0,bal=0.0;
}
class Bank extends BankUtilities implements Account
{
void getInfo()
{
try
{      
System.out.print("Enter Name: ");
name=in.nextLine();
System.out.print("Enter SSN: ");
ssn=in.nextInt();
in.nextLine();
System.out.print("Enter Location: ");
loc=in.nextLine();
System.out.println("Mention account type: 1.Savings (7% intr ) 2.Current (5% intr) ");
acctype=in.nextInt();
switch(acctype)
{
case 1:
System.out.println("Enter the initial amount of deposit:");
temp=in.nextFloat();
if(temp<0)
{
System.out.println("Invalid Amount Try again: ");
System.out.println("Enter the initial amount of deposit:");
temp=in.nextFloat();
}
Deposit(temp);
System.out.println("Enter noof years:");
year=in.nextInt();
if(year<=0)
{
System.out.println("Invalid year Try again.");
System.out.println("Enter noof years:");
year=in.nextInt();
}
break;
case 2:
System.out.println("Enter the initial amount of deposit:");
temp=in.nextFloat();
if(temp<0)
{
System.out.println("Invalid Amount Try again: ");
System.out.println("Enter the initial amount of deposit:");
temp=in.nextFloat();
}
Deposit(temp);
break;
default: System.out.println("Invalid Option");
}  
}
catch(Exception e)
{
System.out.println("Inbuilt Exception --> "+e);
System.exit(0);
}
}
public void CreateAcc()  
{
try
{
getInfo();
System.out.println(" Account Successfully Created!");
accnum=rnd.nextInt(1000)+1;
System.out.println("Hello "+name+" your account no is " +accnum+". ");
}
catch(Exception e)
{
System.out.println("Fatal Error");
}
}
void Deposit(double temp)  
{
try
{
if(temp>500)
{
bal+=temp;
System.out.println("SUCCESSFULLY CREDITED");
}
else
{
throw new MyException("Minimum Deposit Violation");
}
}
catch(MyException e)
{
System.out.println(e.getMessage());
System.out.println("TRANSACTION FAILURE");
System.exit(0);
}
}
public void Deposit() // regular deposit
{
System.out.println("Enter the amount to be deposited :");
temp=in.nextFloat();
try
{
if(temp>0)
{
bal+=temp;
System.out.println("SUCCESSFULLY CREDITED");
}
else
{
throw new MyException("INVALID AMOUNT EXCEPTION");
}
}
catch(MyException e)
{
System.out.println(e.getMessage());
System.out.println("TRANSACTION FAILURE");
}
}
public void Withdraw()// regular deposit
{
System.out.println("Enter the amount to be withdrawn :");
temp=in.nextFloat();
if(temp<=0)
{
System.out.println("Invalid Amount Error.");
System.exit(0);
}
try
{
if(temp<bal)
{
bal-=temp;
System.out.println("SUCCESSFULLY DEBITED");
}
else
{
throw new MyException("INSUFFIECEINT FUND EXCEPTION");
}
}
catch(MyException e)
{
System.out.println(e.getMessage());
System.out.println("TRANSACTION FAILURE");
}
}
void Interest()
{
try
{
if("".equals(name)&&(ssn==0))
{
throw new MyException("ACCONT NOT FOUND EXCEPTION");
}
else
{
if(acctype==1)
{
bal+=year*bal*0.07;
System.out.println("7% interest added");
}
else if(acctype==2)
{
bal+=year*bal*0.03;
System.out.println("3% interest added");
}
}
}
catch(MyException e)
{
System.out.println(e.getMessage());
System.out.println("TRANSACTION FAILURE");
}
}
void CheckBal()
{
System.out.println("Balance:"+bal);
}
}
class BankDemoExc  
{
public static void main(String arg[]) throws MyException
{
Scanner in = new Scanner(System.in);
Bank b = new Bank();
System.out.println(" ----Bank Simulator----");
try
{
while(true)
{
System.out.println("1.Create Account 2.Check Balance 3.Deposit 4.Withdraw 5.Interest 6.Exit.");
int ch = in.nextInt();
switch(ch)
{
case 1:
b.CreateAcc();
break;
case 2:
b.CheckBal();
break;
case 3:
b.Deposit();
break;
case 4:
b.Withdraw();
break;
case 5:
b.Interest();
break;
case 6:
System.exit(0);
break;
default: System.out.println("Invalid Option");
}
}
}
catch(Exception e)
{
System.out.println("SELF THROWN EXCEPTION IS--->"+e);
}
}
}

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