Skip to content

Commit

Permalink
Merge pull request #212 from priyan-coder/add-exceptions-currencyconv…
Browse files Browse the repository at this point in the history
…erter

Handled exceptions for Currency Converter to resolve issues #199 , #189 , #183 , #155 , #130
  • Loading branch information
priyan-coder authored Nov 5, 2019
2 parents 7e2044e + ad245a7 commit 980ed96
Showing 1 changed file with 35 additions and 20 deletions.
55 changes: 35 additions & 20 deletions src/main/java/executor/command/CommandConvert.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ public CommandConvert(String userInput) {
this.use = ""; // whether to use "from code" or "to code" for fetching exchange rate from json
this.to = getCurrencyConvertTo(userInput);
this.description = "Command that converts the user input cash amount from"
+ " one currency to another and prints it on the User Interface.";
+ " one currency to another and prints it on the User Interface.\n"
+ "FORMAT : convert <amount> /from <Base currency ISO e.g USD > /to <Required Currency ISO e.g SGD>";
}

@Override
Expand All @@ -60,7 +61,8 @@ private Double extractAmount(CommandType commandType, String userInput) {
try {
return Double.parseDouble(amountStr);
} catch (Exception e) {
return 0.0;
Ui.dukeSays("Please enter a valid amount");
return null;
}
}

Expand All @@ -71,7 +73,10 @@ private Double extractAmount(CommandType commandType, String userInput) {
*/
private String getCurrencyCovertFrom(String userInput) {
String fromStr = Parser.parseForFlag("from", userInput);
return fromStr;
if (fromStr != null) {
return fromStr.toUpperCase();
}
return null;
}

/**
Expand All @@ -81,7 +86,10 @@ private String getCurrencyCovertFrom(String userInput) {
*/
private String getCurrencyConvertTo(String userInput) {
String toStr = Parser.parseForFlag("to", userInput);
return toStr;
if (toStr != null) {
return toStr.toUpperCase();
}
return null;
}

/**
Expand All @@ -102,7 +110,11 @@ private String consultCurrencyApi(String from, String to) {
}
return completeJson;
} catch (Exception ex) {
Ui.dukeSays("Please enter a valid country code \n");
Ui.dukeSays("Exchange rate data is unavailable \n"
+ "1. Please ensure you have active internet access. \n"
+ "2. Also please follow the correct format for currency conversion"
+ " available under CONVERT if you type help on the CLI. \n"
+ "3. Please ensure that you enter proper ISO 4217 Country codes. \n");
return null;
}
}
Expand All @@ -121,7 +133,6 @@ private Double deriveExchangeRateFromJson(String json, String countryCode) {
double exRate = exchangeRate.doubleValue();
return exRate;
} catch (Exception e) {
Ui.dukeSays("Please enter a valid country code \n");
return null;
}
}
Expand Down Expand Up @@ -160,7 +171,6 @@ private String generateApiUrl(String from, String to) {
}
return url;
} catch (Exception e) {
Ui.dukeSays("Please enter a valid country code \n");
return null;
}
}
Expand All @@ -187,7 +197,6 @@ private Double convertCurrencyToOrFromEur(Double rate, String from, Double amoun
return convertedAmount;
}
} catch (Exception e) {
Ui.dukeSays("Please enter a valid country code \n");
return null;
}
}
Expand All @@ -211,7 +220,6 @@ private Double convertNonEurCurrencies(String json, String from, String to, Doub
setExchangeRate(originalToOutputExRate);
return convertedAmount;
} catch (Exception e) {
Ui.dukeSays("Please enter a valid country code \n");
return null;
}
}
Expand All @@ -236,10 +244,12 @@ private Double convertCurrency(String from, String to, Double amount) {
}
}
} catch (Exception e) {
Ui.dukeSays(e.getMessage());
Ui.dukeSays(Ui.LINE);
Ui.dukeSays("Please enter in the following format : "
+ "convert 2000 /from USD /to EUR");
String errorMessage = "DUKE$$$ could not understand the input. \n"
+ "Please follow the following formatting to convert : \n"
+ "For example : convert <amount> /from USD /to SGD \n"
+ "\n";
Ui.dukeSays(errorMessage);
return null;
}
return null;
}
Expand All @@ -250,12 +260,17 @@ private Double convertCurrency(String from, String to, Double amount) {
* @return string of output is returned
*/
private String result(Double convertedAmount) {
convertedAmount = roundByDecimalPlace(convertedAmount, 2);
return "DUKE$$$ has converted " + this.from
+ " " + roundByDecimalPlace(this.amount, 2) + " "
+ "to" + " "
+ this.to + " " + convertedAmount + "\n"
+ rateUsed();

if (convertedAmount != null) {
convertedAmount = roundByDecimalPlace(convertedAmount, 2);
return "DUKE$$$ has converted " + this.from
+ " " + roundByDecimalPlace(this.amount, 2) + " "
+ "to" + " "
+ this.to + " " + convertedAmount + "\n"
+ rateUsed();
} else {
return "Please try again :) \n";
}
}

/**
Expand Down Expand Up @@ -295,7 +310,7 @@ private Double getAmount() {
return amount;
}

public Double getExchangeRate() {
private Double getExchangeRate() {
return exchangeRate;
}

Expand Down

0 comments on commit 980ed96

Please sign in to comment.