Loading...
Loading...
A debit card account can never go below 0 and allows only a fixed total of withdrawals per day. newday resets the day's total.
Write a class DebitAccount with private balance, dailyLimit and withdrawnToday, and no setters. The constructor refuses a negative balance or a limit of 0 or less. deposit and withdraw throw an IllegalArgumentException with a clear message and change nothing when a rule would break.
deposit x, withdraw x or newday.NOT OPENED: message. Otherwise one line per command: BALANCE b, LIMIT LEFT l, or REFUSED: message. Withdrawal checks, in order: amount must be positive, balance is only b, daily limit left is l.Example 1
Input
5000 2000 4 withdraw 1500 withdraw 1000 newday withdraw 1000
Output
BALANCE 3500, LIMIT LEFT 500 REFUSED: daily limit left is 500 BALANCE 3500, LIMIT LEFT 2000 BALANCE 2500, LIMIT LEFT 1000
Only 500 of the limit is left on day one; newday resets it.
Example 2
Input
-100 500 0
Output
NOT OPENED: opening balance cannot be negative
A negative opening balance is refused.
Try the problem first. Open one hint at a time.
Keep the limit left as a getter: dailyLimit - withdrawnToday.
Do every check before you change balance or withdrawnToday.
Read the amount inside the try and print "REFUSED: " + e.getMessage() in the catch.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor