Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In this assignment we'll create a very simple bank account class that throws an exception. In order to focus on exception mechanics, our bank account

In this assignment we'll create a very simple bank account class that throws an exception. In order to focus on exception mechanics, our bank account will contain a minimal set of fields/methods. We will also have a custom exception class and a "driver" class (ATM) that contains main. To keep this simple, the ATM class will hardcode withdrawal amounts, as described below.

A NegativeBalanceException that will be thrown when a user attempts to withdraw more money than is available. It should extend Exception and include:

one instance variable of type double named negativeBalance.

a default constructor that calls super() and sets the exception message to: "Error: negative balance" (generic error message, in case method that throws does not provide the negative balance)

a 1-parameter constructor that sets the instance variable and calls super() with the message "Amount exceeds balance by [negativeBalance]". This constructor should also write the same message to a log file (for simplicity, just call it "logfile.txt").

NOTE: We are not using the more sophisticated logging options available in Java (e.g., Log4j). Just use PrintWriter to write to a file.

a toString() method that returns "Balance of [negativeBalance] not allowed"

A BankAccount class that should include:

an instance variable of type double named balance

a 1-parameter constructor that sets the balance

an instance method named withdraw() that:

takes a parameter of type double representing the amount to withdraw.

if the amount to withdraw is greater than the balance, throws a NegativeBalanceException passing in the negative balance.

if amount is not greater, update the balance.

must have a throws clause, so that the error is propagated back to the caller.

a second instance method named quickWithdraw() that:

takes the amount to withdraw.

if amount > balance, throws a NegativeBalanceException with no parameters, otherwise updates balance.

also must have a throws clause.

A ATM class that should include:

one instance variable of type BankAccount

a constructor that creates the BankAccount with a balance of $500

an instance method named handleTransactions() which:

within a try-catch, calls withdraw() with amount $600

the catch should have two printouts (just to see how this works):

System.out.println(e);

System.out.println(e.getMessage());

within a second try-catch, calls quickWithdraw() with amount $600

the catch should contain the same printouts:

System.out.println(e);

System.out.println(e.getMessage());

the main() method will simply create an ATM object and call handleTransactions.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

7. Identify six intercultural communication dialectics.

Answered: 1 week ago