I have an assignment due in two days. The assignment is to create in java a Bank
ID: 3664087 • Letter: I
Question
I have an assignment due in two days. The assignment is to create in java a BankAccount class, a SavingsAccount class that extends from the BankAccount class, and a driver class called FinalExam. The BankAccount and SavingsAccount classes are supposed to handle deposits and withdrawals as well as a NegativeAmountException and a InsufficientFundsException. Here is what I have:
1. BankAccount class
class NegativeAmountException extends Exception // creates NegativeAmountException
{
public NegativeAmountException()
{
}
public NegativeAmountException(String message)
{
super(message);
}
}
class InsufficientFundsException extends Exception //creates InsufficientFundsException
{
public InsufficientFundsException()
{
}
public InsufficientFundsException(String message)
{
super(message);
}
}
public class BankAccnt
{
String name;
double balance;
public BankAccnt(String name) throws NegativeAmountException
{
this.name = name;
balance = 0;
}
public BankAccnt(String name, double balance) throws NegativeAmountException
{
this.name = name;
if(balance < 0)
{
throw new NegativeAmountException("The amount entered cannot be negative");
}
this.balance = balance;
}
public void deposit(double amount) throws NegativeAmountException
{
if(amount < 0)
{
throw new NegativeAmountException ("The amount entered cannot be negative");
}
balance+=amount;
}
public void withdraw(double amount) throws InsufficientFundsException, NegativeAmountException
{
if(balance < amount)
{
throw new InsufficientFundsException("You do not have sufficient funds in your account to perform this operation");
}
if(amount < 0)
{
throw new NegativeAmountException("The amount entered cannot be negative");
}
balance-=amount;
}
public double getBalance()
{
return balance;
}
//print bank statement including customer name
//and current account balance
public void printStatement()
{
System.out.println(name+""+balance);
}
}
2. SavingsAccount class
import java.text.DecimalFormat;
public class SavingsAccnt extends BankAccnt
{
double currentRate;
public SavingsAccnt(String name,double balance,double rate) throws NegativeAmountException
{
super(name,balance);
if(balance < 0)
{
throw new NegativeAmountException("Cannot be negative");
}
this.currentRate = rate;
}
public void postInterest() throws NegativeAmountException //post monthly interest by multiplying current balance
{ //by current interest rate divided by 12 and then adding
double interestEarned = balance * currentRate / 12; //result to balance by making deposit
deposit(interestEarned);
}
public void printStatement()
{
DecimalFormat formatter = new DecimalFormat ("#0.00");// Create an object for dollars and cents
//print bank statement including customer name
// and current account balance( use print Statement from the BankAccount superclass)
// following this also print current interest rate
System.out.println("Amount for "+name+" "+"$ "+formatter.format(balance)+" balance"+" "+(currentRate*100)+"% Current Interest Rate");
}
}
3. FinalExam class
import java.util.Scanner;
import java.text.DecimalFormat;
public class FinalExam
{
public static void main(String args[]) throws InsufficientFundsException, NegativeAmountException
{
SavingsAccnt account = new SavingsAccnt("Penny Saved",1000.00,.05);
Scanner info = new Scanner(System.in);
DecimalFormat formatter = new DecimalFormat ("#0.00");// Create an object for dollars and cents
System.out.print("Enter amount to Deposit : ");
double credit = info.nextDouble();
account.deposit(credit);
account.printStatement();
System.out.println();
System.out.print("Enter amount to Withdraw : ");
double debit = info.nextDouble();
account.withdraw(debit);
account.printStatement();
account.postInterest();
System.out.println();
System.out.println("Posting Monthly Interest");
account.printStatement();
}
}
All three classes compile great and most of the output is what I am looking for.
Here's my question: Instead of throwing the exceptions, I need to catch them using a try/catch and possibly finally block. Can anyone give me an idea on how to do this?
Explanation / Answer
Its Simple, firstly you are trying to take care of the checked exception such as balance>0 and others. The most appropriate way to handle checked exception is by throwing them. Now as we throw an exception from an function there are manly two ways to handle it,
1. either the function throws the exception with the callback and the exception is handled there.
2. we write and try and catch block in the function to handle all the exception thrown inside the function.
for e.g.
This is one of your function which throws the exception if balance <0
public SavingsAccnt(String name,double balance,double rate) throws NegativeAmountException
{
super(name,balance);
if(balance < 0)
{
throw new NegativeAmountException("Cannot be negative");
}
this.currentRate = rate;
}
Instead of throwing this to the caller, we can handle it in the function itself by modifying it a little bit
public SavingsAccnt(String name,double balance,double rate)
{
super(name,balance);
try{
if(balance < 0)
{
throw new NegativeAmountException("Cannot be negative");
}
this.currentRate = rate;
}catch(NegativeAmountException e){
}
}
As shown the the exception would be thrown inside the catch block where you can print or manupulate it as per the requirement or you can override the toString methods and create an printstacktrace type of functionality
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.