-
-
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
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
Awesome coding