THINK FIRST·CODE LATER

← All labs

InsufficientFundsException

Problem

Write a checked exception InsufficientFundsException whose message is Missing 50.00, where the number is the amount that is lacking.

Write a class Account with a private balance and a method

public void withdraw(double amount) throws InsufficientFundsException

which throws the exception when the balance is too small, and otherwise subtracts the amount.

Bank reads the initial balance and an amount to withdraw, then prints either

New balance: 50.00

or, when the withdrawal fails, the message of the caught exception:

Refused: Missing 50.00

Data for this lab

Your program reads its data from standard input. Download the file, then give it to your program as shown below. Start with data set 1: its expected output is given.

l16-1-data-1.txt

100 50
What your program must print for l16-1-data-1.txt
New balance: 50.00

Run your program first, then open this and compare line by line: your output must contain these lines exactly.

How to give the file to my program

Terminal (macOS, Linux, or Windows Command Prompt), after compiling:

java Main < l16-1-data-1.txt

Windows PowerShell does not understand <:

Get-Content l16-1-data-1.txt | java Main

IntelliJ IDEA: Run → Edit Configurations → Redirect input from. Or simply paste the content of the file into the console when the program waits for input.

Write it here or in your IDE, then paste it. Compile and test it yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.