-
-
Save andersonmo/7dfe346ae2b24cfbc6783219e08caded to your computer and use it in GitHub Desktop.
import java.util.HashMap; | |
import java.util.Map; | |
import java.util.Scanner; | |
/* | |
* Create TransactionException, DigitalWallet, and DigitalWalletTransaction classes here. | |
*/ | |
//---- My code ---- | |
class TransactionException extends Exception { | |
String errorMessage, errorCode; | |
TransactionException(String errorMessage, String errorCode){ | |
super(errorMessage); | |
this.errorCode = errorCode; | |
} | |
public String getErrorCode(){ | |
return errorCode; | |
} | |
} | |
//----------------------------------------- | |
class DigitalWallet{ | |
String walletId, userName, userAccessCode; | |
int walletBalance; | |
public DigitalWallet(String walletId, String userName){ | |
this.walletId = walletId; | |
this.userName = userName; | |
} | |
public DigitalWallet(String walletId, String userName, String userAccessCode){ | |
this.walletId = walletId; | |
this.userName = userName; | |
this.userAccessCode = userAccessCode; | |
} | |
public String getWalletId(){ | |
return walletId; | |
} | |
public String getUsername(){ | |
return userName; | |
} | |
public String getUserAccessToken(){ | |
return userAccessCode; | |
} | |
public int getWalletBalance(){ | |
return walletBalance; | |
} | |
public void setWalletBalance(int walletBalance){ | |
this.walletBalance = walletBalance; | |
} | |
} | |
//------------------------------ | |
class DigitalWalletTransaction{ | |
public void addMoney(DigitalWallet digitalWallet, int amount) throws TransactionException { | |
if (digitalWallet.getUserAccessToken() == null){ | |
throw new TransactionException("User not authorized", "USER_NOT_AUTHORIZED"); | |
} else if(amount <=0){ | |
throw new TransactionException("Amount should be greater than zero", "INVALID_AMOUNT"); | |
} else { | |
digitalWallet.setWalletBalance(digitalWallet.getWalletBalance() + amount); | |
} | |
} | |
public void payMoney(DigitalWallet digitalWallet, int amount) throws TransactionException{ | |
if(digitalWallet.getWalletBalance()<amount){ | |
throw new TransactionException("Insufficient balance", "INSUFFICIENT_BALANCE"); | |
} else if (digitalWallet.getUserAccessToken() == null){ | |
throw new TransactionException("User not authorized", "USER_NOT_AUTHORIZED"); | |
} else if(amount <= 0){ | |
throw new TransactionException("Amount should be greater than zero", "INVALID_AMOUNT"); | |
} else{ | |
digitalWallet.setWalletBalance(digitalWallet.getWalletBalance() - amount); | |
} | |
} | |
} | |
//---- End my code ---- | |
public class Solution { | |
private static final Scanner INPUT_READER = new Scanner(System.in); | |
private static final DigitalWalletTransaction DIGITAL_WALLET_TRANSACTION = new DigitalWalletTransaction(); | |
private static final Map<String, DigitalWallet> DIGITAL_WALLETS = new HashMap<>(); | |
public static void main(String[] args) { | |
int numberOfWallets = Integer.parseInt(INPUT_READER.nextLine()); | |
while (numberOfWallets-- > 0) { | |
String[] wallet = INPUT_READER.nextLine().split(" "); | |
DigitalWallet digitalWallet; | |
if (wallet.length == 2) { | |
digitalWallet = new DigitalWallet(wallet[0], wallet[1]); | |
} else { | |
digitalWallet = new DigitalWallet(wallet[0], wallet[1], wallet[2]); | |
} | |
DIGITAL_WALLETS.put(wallet[0], digitalWallet); | |
} | |
int numberOfTransactions = Integer.parseInt(INPUT_READER.nextLine()); | |
while (numberOfTransactions-- > 0) { | |
String[] transaction = INPUT_READER.nextLine().split(" "); | |
DigitalWallet digitalWallet = DIGITAL_WALLETS.get(transaction[0]); | |
if (transaction[1].equals("add")) { | |
try { | |
DIGITAL_WALLET_TRANSACTION.addMoney(digitalWallet, Integer.parseInt(transaction[2])); | |
System.out.println("Wallet successfully credited."); | |
} catch (TransactionException ex) { | |
System.out.println(ex.getErrorCode() + ": " + ex.getMessage() + "."); | |
} | |
} else { | |
try { | |
DIGITAL_WALLET_TRANSACTION.payMoney(digitalWallet, Integer.parseInt(transaction[2])); | |
System.out.println("Wallet successfully debited."); | |
} catch (TransactionException ex) { | |
System.out.println(ex.getErrorCode() + ": " + ex.getMessage() + "."); | |
} | |
} | |
} | |
System.out.println(); | |
DIGITAL_WALLETS.keySet() | |
.stream() | |
.sorted() | |
.map((digitalWalletId) -> DIGITAL_WALLETS.get(digitalWalletId)) | |
.forEachOrdered((digitalWallet) -> { | |
System.out.println(digitalWallet.getWalletId() | |
+ " " + digitalWallet.getUsername() | |
+ " " + digitalWallet.getWalletBalance()); | |
}); | |
} | |
} |
Awesome coding
Awesome coding
Thanks, fell free to give opinions or make questions.
@andersonmo In Pay Money TokenShould be validated first. So the pay function should be :
public void payMoney(DigitalWallet digitalWallet, int amount) throws TransactionException{
if (digitalWallet.getUserAccessToken() == null){
throw new TransactionException("User not authorized", "USER_NOT_AUTHORIZED");
} else if(digitalWallet.getWalletBalance()<amount){
throw new TransactionException("Insufficient balance", "INSUFFICIENT_BALANCE");
} else if(amount <= 0){
throw new TransactionException("Amount should be greater than zero", "INVALID_AMOUNT");
} else{
digitalWallet.setWalletBalance(digitalWallet.getWalletBalance() - amount);
}
}
Wrote this same test, can't understand my test cases failed as my code is exactly the same except I missed calling the super constructor on the exception subclass constructor, however i assumed the getErrorCode was the focus
Koszonom szepen, it helped very much!
I'm relieved I went with Yellow.Systems to develop my electronic money software. They gave me an excellent product that was simple to operate and featured everything I required. Their customer support was excellent as well; I received prompt and helpful responses to my questions. If you need anything related to software creation, you should hire them. You also can check how to create e wallet website at their blog.
Wrote this same test, can't understand my test cases failed as my code is exactly the same except I missed calling the super constructor on the exception subclass constructor, however i assumed the getErrorCode was the focus
yeah same happended with me. Don't know what was the issue. Anyone knows why test cases could fail?
Can anyone, share the question link? TIA
is line 11 String errorMessage
mandatory? because i assumed that it is set by super(errorMessage) to parent's variable
Digital wallets have made sending and receiving money easier, but they require authentication. A user must have an access token to initiate transactions in this digital wallet system. There are two types of transactions:
Both the transactions are validated before being processed. For an invalid transaction the following error code and the error message are displayed:
In this challenge, you are building the digital wallet workflow by writing the complete implementation of the following three classes:
The locked stub code in the editor validates the correctness of the TransactionException, DigitalWallet, and DigitalWalletTransaction class implementations by first creating the authorized and unauthorized digital wallets and then processing the following two type of transactions:
After processing all the queries, the locked stub code prints the wallet ID, username, and the balance amount for each of the digital wallets.
Constraints
1 ≤ numberOfWallets ≤ 100
1 ≤ numberOfTransactions ≤ 2 × 103
Input Format For Custom Testing
The first line contains the value of numberOfWallets describing the total number of wallets.
The next numberOfWallets lines contain the required information of the wallets.
The next line contains the value of numberOfTransactions describing the total number of transactions.
The next numberOfTransactions lines contains one of the above-mentioned two transactions.
Sample Case
Sample Input For Custom Testing
2
1 Julia bff834a2c117a76d9ceb782f98e428686ca3c4ea
2 Samantha
10
1 pay 50
1 add 100
1 add 0
1 pay 30
2 add 500
1 add -5
1 add 1000
1 pay -20
1 pay 100
1 add 720
Sample Output
INSUFFICIENT_BALANCE: Insufficient balance.
Wallet successfully credited.
INVALID_AMOUNT: Amount should be greater than zero.
Wallet successfully debited.
USER_NOT_AUTHORIZED: User not authorized.
INVALID_AMOUNT: Amount should be greater than zero.
Wallet successfully credited.
INVALID_AMOUNT: Amount should be greater than zero.
Wallet successfully debited.
Wallet successfully credited.
1 Julia 1690
2 Samantha 0
Explanation