Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Liam Cassidy
9,739 PointsJava Closures Excersise
Hello,
I solved the closures exercise with the following solution:
public static Function<Integer, String> getArgentinaPriceConverter() {
Locale argLocale = Locale.forLanguageTag("es-AR");
// This fluctuates and is hardcoded temporarily
BigDecimal argPesoToUsdRate = new BigDecimal("15.48");
Function<Integer, BigDecimal> usdToArgentinePesoConverter =
usd -> argPesoToUsdRate.multiply(new BigDecimal(usd));
Function<BigDecimal, String> argentineCurrencyFormatter = price -> {
Currency currentCurrency = Currency.getInstance(argLocale);
NumberFormat currencyFormatter =
NumberFormat.getCurrencyInstance(argLocale);
return String.format("%s (%s)",
currencyFormatter.format(price),
currentCurrency.getDisplayName()
);
};
return usdToArgentinePesoConverter.andThen(argentineCurrencyFormatter);
}
public static Function<Integer, String> createPriceConverter(
Locale locale,
BigDecimal usdRate) {
// TODO: Examine the hardcoded `getArgentinaPriceConverter` method
// TODO: Using the arguments passed into this method return a function that will work for any locale and rate
return usdPrice -> {
Currency currentCurrency = Currency.getInstance(locale);
NumberFormat currencyFormatter = NumberFormat.getCurrencyInstance(locale);
return String.format("%s (%s)", currencyFormatter.format(usdPrice), currentCurrency.getDisplayName());
};
}
However, I am confused with my solution. My solution seems to only return a function that formats the current price into a locale format and displays that current currency. Yet, the function name is createPriceConverter. I feel that I may have missed an operation in here on manipulating the usdRate yet the tests did not catch this. Or, perhaps the function name should be different (e.g. "getCurrencyFormatter").
Any feedback on what I am missing is most welcome.
Cheers,
Liam
2 Answers
Fahad Mutair
10,359 Pointshi Liam Cassidy , you are missed multiplication to change from Peso to Usd , you can solve it in this way
public static Function<Integer, String> createPriceConverter(Locale locale, BigDecimal usdRate) {
// TODO: Examine the hardcoded `getArgentinaPriceConverter` method
// TODO: Using the arguments passed into this method return a function that will work for any locale and rate
return usdPrice -> {
Currency currentCurrency = Currency.getInstance(locale);
NumberFormat currencyFormatter = NumberFormat.getCurrencyInstance(locale);
return String.format("%s (%s)", currencyFormatter.format(usdRate.multiply(new BigDecimal(usdPrice))), currentCurrency.getDisplayName());
};
}
Ben Basty
23,772 PointsHave anyone came into this error : Expected: a string containing "1,00 (Euro)" but: was "1,00 € (Euro)" Here is how i resolved it.
public static Function<Integer, String> createPriceConverter(Locale locale, BigDecimal usdRate) {
// TODO: Examine the hardcoded `getArgentinaPriceConverter` method
// TODO: Using the arguments passed into this method return a function that will work for any locale and rate
return usdPrice -> {
Currency currentCurrency = Currency.getInstance(locale);
NumberFormat currencyFormatter = NumberFormat.getCurrencyInstance(locale);
if (currencyFormatter instanceof DecimalFormat) {
DecimalFormat df = (DecimalFormat) currencyFormatter;
DecimalFormatSymbols dfs = df.getDecimalFormatSymbols();
dfs.setCurrencySymbol("");
df.setDecimalFormatSymbols(dfs);
df.applyPattern(".00");
}
return String.format("%s (%s)", currencyFormatter.format(usdRate), currentCurrency.getDisplayName()
);
};
}
Don't forget to import
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
Liam Cassidy
9,739 PointsLiam Cassidy
9,739 PointsThanks, Fahad. Much appreciated!