In: Computer Science
Modify the BankAccount class to throw IllegalArgumentException exceptions, create your own three exceptions types. when the account is constructed with negative balance, when a negative amount is deposited, or when an amount that is not between 0 and the current balance is withdrawn. Write a test program that cause all three exceptions occurs and catches them all.
/**
A bank account has a balance that can be changed by
deposits and withdrawals.
*/
public class BankAccount
{
private double balance;
/**
Constructs a bank account with a zero balance
*/
public BankAccount()
{
balance = 0;
}
/**
Constructs a bank account with a given balance
@param initialBalance the initial balance
*/
public BankAccount(double initialBalance)
{
if (initialBalance < 0)
throw new NegativeBalanceException(
"Cannot create account: " + initialBalance + " is less than zero");
balance = initialBalance;
}
/**
Deposits money into the bank account.
@param amount the amount to deposit
*/
public void deposit(double amount)
{
if (amount < 0)
throw new NegativeAmountException(
"Deposit of " + amount + " is less than zero");
double newBalance = balance + amount;
balance = newBalance;
}
/**
Withdraws money from the bank account.
@param amount the amount to withdraw
*/
public void withdraw(double amount)
{
if (amount < 0)
throw new NegativeAmountException(
"Withdrawal of " + amount + " is less than zero");
if (amount > balance)
throw new InsufficientFundsException(
"Withdrawal of " + amount + " exceeds balance of " +
balance);
double newBalance = balance - amount;
balance = newBalance;
}
/**
Gets the current balance of the bank account.
@return the current balance
*/
public double getBalance()
{
return balance;
}
}
---------------
/**
A class to test the BankAccount class.
*/
public class BankAccountTester2
{
public static void main(String[] args)
{
BankAccount harrysChecking = new BankAccount();
try
{
harrysChecking.deposit(300);
System.out.println("success");
}
catch (IllegalArgumentException e)
{
System.out.println("exception");
}
System.out.println("Expected: success");
try
{
harrysChecking.withdraw(100);
System.out.println("success");
}
catch (IllegalArgumentException e)
{
System.out.println("exception");
}
System.out.println("Expected: success");
try
{
harrysChecking.deposit(-100);
System.out.println("success");
}
catch (IllegalArgumentException e)
{
System.out.println("exception");
}
System.out.println("Expected: exception");
try
{
harrysChecking.withdraw(300);
System.out.println("success");
}
catch (IllegalArgumentException e)
{
System.out.println("exception");
}
System.out.println("Expected: exception");
}
}
-------------------------
Hint:
/**
This exception reports a negative initial balance on a bank
account.
*/
public class NegativeBalanceException extends
RuntimeException
{
public NegativeBalanceException()
{
}
public NegativeBalanceException(String message)
{
super(message);
}
}
Code For Above Problem:
BankAccount.java
/**
* A bank account has a balance that can be changed by deposits and withdrawals.
*/
public class BankAccount {
private double balance;
/**
* Constructs a bank account with a zero balance
*/
public BankAccount() {
balance = 0;
}
/**
* Constructs a bank account with a given balance
*
* @param initialBalance the initial balance
*/
public BankAccount(double initialBalance) {
if (initialBalance < 0)
throw new NegativeBalanceException("Cannot create account: " + initialBalance + " is less than zero");
balance = initialBalance;
}
/**
* Deposits money into the bank account.
*
* @param amount the amount to deposit
* @throws NegativeAmountException
*/
public void deposit(double amount) throws NegativeAmountException {
if (amount < 0)
throw new NegativeAmountException("Deposit of " + amount + " is less than zero");
double newBalance = balance + amount;
balance = newBalance;
}
/**
* Withdraws money from the bank account.
*
* @param amount the amount to withdraw
* @throws NegativeAmountException
* @throws InsufficientFundsException
*/
public void withdraw(double amount) throws NegativeAmountException, InsufficientFundsException {
if (amount < 0)
throw new NegativeAmountException("Withdrawal of " + amount + " is less than zero");
if (amount > balance)
throw new InsufficientFundsException("Withdrawal of " + amount + " exceeds balance of " + balance);
double newBalance = balance - amount;
balance = newBalance;
}
/**
* Gets the current balance of the bank account.
*
* @return the current balance
*/
public double getBalance() {
return balance;
}
}
BankAccountTester2.java
/**
* A class to test the BankAccount class.
*/
public class BankAccountTester2 {
public static void main(String[] args) {
BankAccount harrysChecking = new BankAccount();
try {
harrysChecking.deposit(300);
System.out.println("success");
} catch (IllegalArgumentException | NegativeAmountException e) {
System.out.println("exception");
}
System.out.println("Expected: success");
try {
harrysChecking.withdraw(100);
System.out.println("success");
} catch (IllegalArgumentException | NegativeAmountException | InsufficientFundsException e) {
System.out.println("exception");
}
System.out.println("Expected: success");
try {
harrysChecking.deposit(-100);
System.out.println("success");
} catch (IllegalArgumentException | NegativeAmountException e) {
System.out.println("exception");
}
System.out.println("Expected: exception");
try {
harrysChecking.withdraw(300);
System.out.println("success");
} catch (IllegalArgumentException | NegativeAmountException | InsufficientFundsException e) {
System.out.println("exception");
}
System.out.println("Expected: exception");
}
}
NegativeBalanceException.java
/**
* This exception reports a negative initial balance on a bank account.
*/
public class NegativeBalanceException extends RuntimeException {
/**
*
*/
private static final long serialVersionUID = 1L;
public NegativeBalanceException() {
}
public NegativeBalanceException(String message) {
super(message);
}
}
NegativeAmountException.java
/**
* This exception reports a negative amount on with draw or deposite
*/
public class NegativeAmountException extends Exception {
/**
*
*/
private static final long serialVersionUID = 1L;
public NegativeAmountException() {
}
public NegativeAmountException(String message) {
super(message);
}
}
InsufficientFundsException.java
/**
* This exception reports a InsufficientFund on with draw
*/
public class InsufficientFundsException extends Exception {
/**
*
*/
private static final long serialVersionUID = 1L;
public InsufficientFundsException() {
}
public InsufficientFundsException(String message) {
super(message);
}
}
Output Of BankAccountTester2:
success
Expected: success
success
Expected: success
exception
Expected: exception
exception
Expected: exception