Loading...
Loading...
A UPI wallet asks for a 4-digit PIN to pay, to see the balance and to change the PIN. Three wrong PINs in a row lock the wallet for good; a correct PIN resets the count. Adding money needs no PIN and works even when locked.
Write a class Wallet with private balance, pin, wrongTries and locked, private helpers isValidPin and checkPin, and public addMoney, pay, showBalance and changePin that return their message. No method returns the PIN.
add x, pay x PIN, balance PIN or changepin OLD NEW. PINs are words, so 0042 keeps its zeros.WALLET LOCKED; INVALID AMOUNT (x ≤ 0); a wrong PIN: WRONG PIN, 2 TRIES LEFT, WRONG PIN, 1 TRY LEFT or, on the third, WRONG PIN, WALLET LOCKED; then INSUFFICIENT BALANCE or PIN MUST BE 4 DIGITS. Success: ADDED x, BALANCE b, PAID x, BALANCE b, BALANCE b or PIN CHANGED.Example 1
Input
1234 500 6 pay 200 1234 pay 100 9999 pay 100 1111 balance 1234 pay 900 1234 changepin 1234 12a4
Output
PAID 200, BALANCE 300 WRONG PIN, 2 TRIES LEFT WRONG PIN, 1 TRY LEFT BALANCE 300 INSUFFICIENT BALANCE PIN MUST BE 4 DIGITS
Two wrong PINs, then a correct one resets the count.
Example 2
Input
0042 100 5 balance 4200 balance 0420 pay 50 0000 pay 10 0042 add 50
Output
WRONG PIN, 2 TRIES LEFT WRONG PIN, 1 TRY LEFT WRONG PIN, WALLET LOCKED WALLET LOCKED ADDED 50, BALANCE 150
The third wrong PIN locks the wallet; adding money still works.
Try the problem first. Open one hint at a time.
Put the PIN check in one private method: it compares, counts wrong tries, locks on the third and returns an error message or null.
Check the lock first in every PIN method, then the amount, then the PIN.
A valid PIN has length 4 and every character passes Character.isDigit.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor