A. Create a BankAccount class that models a typical bank account where you depos
ID: 3741821 • Letter: A
Question
A. Create a BankAccount class that models a typical bank account where you deposit and withdraw money. To keep things simple, assume this bank account deals only with amounts of type int. You should provide a constructor, a toString method, a getTotal method that returns an int, and both deposit and withdraw methods that take int arguments and return void. Also create an application TestDriver that demonstrates that the BankAccount class works correctly.
B. Extend a BankAccount class and create a SafeBankAccount class. Create a BankAccountException class. Override your deposit method so that it throws an appropriate exception if an attempt is made to deposit a negative amount. Do the same with the withdraw method, but also have it throw an exception if an attempt is made to withdraw more money than is available. In each case include appropriate exception messages.
C. Create a new application, Banker, that creates a SafeBankAccount object and then interacts with users, allowing them to deposit or withdraw funds, or to request an account total. This application should not bomb in any of the exceptional situations - it should catch the exception, pass the message to the user, and continue processing.
Explanation / Answer
BankAccount.java
package bank1;
//account class
public class BankAccount{
//variable declaration
int balance;
//default constructor
public BankAccount(){
balance=0;
}
//method to get balance in account
public int getTotal(){
return balance;
}
//method to deposit money to the account
public void deposit(int amt){
balance=balance+amt;
}
//method to withdraw money from the account
public void withDraw(int amt){
balance=balance-amt;
}
}
TestDriver.java
package bank1;
import java.util.Scanner;
public class TestDriver{
public static void main(String[] s){
//Create scanner object to take input
Scanner in=new Scanner(System.in);
//declaring the variable
int choice,amt;
//creating object
BankAccount b=new BankAccount();
//Show message to choose an option
System.err.print("Choose an option 1. Deposit 2.Withdraw 3. Check Balance 4. Exit=");
//taking input for choice
choice=in.nextInt();
//loop continues unless user enter 4 to exit
while(choice!=4){
if(choice==1){//if choice is deposit
//showing message to user for entering an amount to deposit
System.out.print("Enter the amount=");
//taking amount as input
amt=in.nextInt();
//call deposit function
b.deposit(amt);
}
else if(choice==2){//if choise is withdraw
//showing message to user for entering an amount to deposit
System.out.print("Enter the amount=");
//taking amount as input
amt=in.nextInt();
//call withraw function
b.withDraw(amt);
}
else if(choice==3){//if choice is to check balance
System.out.println(b.getTotal());
}
else {//show message to user if he enters choice other than specified
System.out.println("Choice is not valid, Choose again");
}
//asking user for new operation
System.err.print("Choose an option 1. Deposit 2.Withdraw 3. Check Balance 4. Exit=");
//taking new choice
choice=in.nextInt();
}
}
}
Output:
Choose an option 1. Deposit 2.Withdraw 3. Check Balance 4. Exit=1
Enter the amount=10000
Choose an option 1. Deposit 2.Withdraw 3. Check Balance 4. Exit=3
10000
Choose an option 1. Deposit 2.Withdraw 3. Check Balance 4. Exit=2
Enter the amount=200
Choose an option 1. Deposit 2.Withdraw 3. Check Balance 4. Exit=3
9800
Choose an option 1. Deposit 2.Withdraw 3. Check Balance 4. Exit=1
Enter the amount=-800
Choose an option 1. Deposit 2.Withdraw 3. Check Balance 4. Exit=3
9000
Choose an option 1. Deposit 2.Withdraw 3. Check Balance 4. Exit=2
Enter the amount=10000
Choose an option 1. Deposit 2.Withdraw 3. Check Balance 4. Exit=3
-1000
Choose an option 1. Deposit 2.Withdraw 3. Check Balance 4. Exit=2
Enter the amount=-500
Choose an option 1. Deposit 2.Withdraw 3. Check Balance 4. Exit=3
-500
Choose an option 1. Deposit 2.Withdraw 3. Check Balance 4. Exit=7
Choice is not valid, Choose again
Choose an option 1. Deposit 2.Withdraw 3. Check Balance 4. Exit=4
SafeBankAccount.java
package bank1;
//account class
import java.util.logging.Level;
import java.util.logging.Logger;
public class SafeBankAccount extends BankAccount{
//method to deposit money to the account
@Override
public void deposit(int amt){
//checking for correct amount
if(amt<0){
try {
throw new BankAccountException("Amount is not valid");//throwing an exception
} catch (BankAccountException ex) {//catchingthe exception
System.out.println(ex);
}
}
else{
//add the given amount
balance=balance+amt;
}
}
//method to withdraw money from the account
@Override
public void withDraw(int amt){
if(balance<amt || amt<0){
try {
//throwing exception
throw new BankAccountException("Either Amount is invalid or you have no balance in your account");
} catch (BankAccountException ex) {
System.out.println(ex);
}
}
else{
//subtract the given amount
balance=balance-amt;
}
}
}
BankAccountException.java
package bank1;
//class declaration
public class BankAccountException extends Exception{
//variable declaration
String str;
//constructor
public BankAccountException(String st) {
str=st;
}
@Override
public String toString() {
return str; //show error message
}
}
Banker.java
package bank1;
import java.util.Scanner;
//class declaration
public class Banker extends SafeBankAccount{
public static void main(String[] args) {
//Create scanner object to take input
Scanner in=new Scanner(System.in);
//declaring the variable
int choice,amt;
//creating object
Banker b=new Banker();
//Show message to choose an option
System.err.print("Choose an option 1. Deposit 2.Withdraw 3. Check Balance 4. Exit=");
//taking input for choice
choice=in.nextInt();
//loop continues unless user enter 4 to exit
while(choice!=4){
if(choice==1){//if choice is deposit
//showing message to user for entering an amount to deposit
System.out.print("Enter the amount=");
//taking amount as input
amt=in.nextInt();
//call deposit function
b.deposit(amt);
}
else if(choice==2){//if choise is withdraw
//showing message to user for entering an amount to deposit
System.out.print("Enter the amount=");
//taking amount as input
amt=in.nextInt();
//call withraw function
b.withDraw(amt);
}
else if(choice==3){//if choice is to check balance
System.out.println(b.getTotal());
}
else {//show message to user if he enters choice other than specified
System.out.println("Choice is not valid, Choose again");
}
//asking user for new operation
System.err.print("Choose an option 1. Deposit 2.Withdraw 3. Check Balance 4. Exit=");
//taking new choice
choice=in.nextInt();
}
}
}
Output:
Choose an option 1. Deposit 2.Withdraw 3. Check Balance 4. Exit=1
Enter the amount=-10
Amount is not valid
Choose an option 1. Deposit 2.Withdraw 3. Check Balance 4. Exit=2
Enter the amount=4
Either Amount is invalid or you have no balance in your account
Choose an option 1. Deposit 2.Withdraw 3. Check Balance 4. Exit=1
Enter the amount=5000
Choose an option 1. Deposit 2.Withdraw 3. Check Balance 4. Exit=3
5000
Choose an option 1. Deposit 2.Withdraw 3. Check Balance 4. Exit=2
Enter the amount=6000
Either Amount is invalid or you have no balance in your account
Choose an option 1. Deposit 2.Withdraw 3. Check Balance 4. Exit=2
Enter the amount=3000
Choose an option 1. Deposit 2.Withdraw 3. Check Balance 4. Exit=3
2000
Choose an option 1. Deposit 2.Withdraw 3. Check Balance 4. Exit=9
Choice is not valid, Choose again
Choose an option 1. Deposit 2.Withdraw 3. Check Balance 4. Exit=4
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.