1.Digital wallets have simplified the process of sending and receiving money, but they necessitate authentication. To initiate transactions within this digital wallet system, a user must possess an access token. The types of transactions include:
- Adding money to the wallet.
- Paying money from the wallet.
Both types of transactions undergo validation before processing. The following error codes and messages are displayed for invalid transactions:
- USER_NOT_AUTHORIZED: This error code reports an unauthorized transaction, such as when there is no access token for the digital wallet. The error message should be “User not authorized.”
- INVALID_AMOUNT: This error code indicates an attempt to add or pay a zero or negative amount. The error message should be “Amount should be greater than zero.”
- INSUFFICIENT_BALANCE: This error code signifies an attempt to make a payment greater than the available digital wallet balance. The error message should be “Insufficient balance.”
The challenge involves implementing the digital wallet workflow through three classes:
- TransactionException class:
- Constructor: TransactionException(String errorMessage, String errorCode)
- Method: String getErrorCode() to return the error code of the exception.
- DigitalWallet class:
- Constructor: DigitalWallet(String walletId, String userName)
- Constructor: DigitalWallet(String walletId, String userName, String userAccessCode)
- Method: String getWalletId() to return the wallet ID.
- Method: String getUsername() to return the owner’s username.
- Method: String getUserAccessToken() to return the access token.
- Method: int getWalletBalance() to return the wallet balance.
- Method: void setWalletBalance(int walletBalance) to update the wallet balance.
- DigitalWalletTransaction class:
- Method: void addMoney(DigitalWallet digitalWallet, int amount) to add money to the wallet, throwing necessary exceptions for invalid transactions.
- Method: void payMoney(DigitalWallet digitalWallet, int amount) to make a payment from the wallet, throwing necessary exceptions for invalid transactions.
The locked stub code will validate the implementations by creating authorized and unauthorized digital wallets and processing two types of transactions:
- walletId add amount: Handled by DigitalWalletTransaction.addMoney(digitalWallet, amount)
- walletId pay amount: Handled by DigitalWalletTransaction.payMoney(digitalWallet, amount)
After processing all queries, the stub code prints the wallet ID, user name, and balance for each digital wallet.
Constraints
- 1 ≤ numberOfWallets ≤ 100
- 1 ≤ numberOfTransactions ≤ 2 × 103