Loading...
Loading...
A wallet starts with some balance and then gets n payment requests. Write a method pay(balance, amount) that returns the new balance. It must throw an IllegalArgumentException:
amount must be positive when the amount is 0 or less,insufficient balance when the amount is more than the balance.In main, for each request print Paid A, balance B, or Failed: <message> when pay throws, or Invalid amount: <text> when the request is not a whole number. At the end print Final balance: B.
Final balance: <balance>.intExample 1
Input
1000 4 250 900 abc 750
Output
Paid 250, balance 750 Failed: insufficient balance Invalid amount: abc Paid 750, balance 0 Final balance: 0
The failed and invalid requests do not change the balance.
Example 2
Input
500 2 0 -20
Output
Failed: amount must be positive Failed: amount must be positive Final balance: 500
Try the problem first. Open one hint at a time.
In pay, check the amount first and throw new IllegalArgumentException("...") with the exact message.
In main, call pay inside a try and print e.getMessage() in the catch block.
NumberFormatException is a kind of IllegalArgumentException, so its catch block must come first.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor