@@ -250,9 +250,9 @@ Now, we can begin implementing our function logic in the `eval` method.
250250As a reminder, we want to convert a string like "€100" into "100 EUR". To do this, we can use the following steps:
251251
2522521 . Get the first character of the string, which is the currency symbol (e.g. "€").
253- 2 . Get the rest of the string, which is the amount (e.g. "100 ").
254- 3 . Look up the currency symbol in our ` enum ` to get the corresponding currency code (e.g. "€" => "EUR") .
255- 4 . If the lookup failed (e.g. currency symbol was not found), we can return "ERR" as the currency code .
253+ 2 . Look up the currency symbol in our ` enum ` to get the corresponding currency code (e.g. "€" => "EUR ").
254+ 3 . If the lookup failed (e.g. currency symbol was not found), we can return "ERR" as the currency code .
255+ 4 . Get the rest of the string, which is the amount (e.g. "100") .
2562565 . Concatenate the currency code to the amount, and return the result (e.g. "100 EUR").
257257
258258A possible implementation could look like this:
@@ -286,30 +286,33 @@ public class CurrencyConverter extends ScalarFunction {
286286 return isoCode;
287287 }
288288
289+ public static Currency fromCurrencyAmount (String currencyAmount ) {
290+ // 1. Get the first character of the string, which is the currency symbol (e.g. '€').
291+ String currencySymbol = currencyAmount. substring(0 , 1 );
292+
293+ // 2. Look up the currency symbol in our enum to get the corresponding currency code (e.g. "€" => "EUR").
294+ try {
295+ return Currency . valueOf(currencySymbol);
296+ } catch (Exception e) {
297+ // 3. If the lookup failed (e.g. currency symbol was not found), we can return "ERR" as the currency code (e.g. ">" => "ERR").
298+ return Currency . ERR ;
299+ }
300+ }
301+
289302 public String concatToAmount (String amount ) {
290303 return amount + SEPARATOR + isoCode;
291304 }
292305 }
293306
294307 // Value of passed field (e.g. "unit_cost") is passed in e.g. "€100"
295308 public String eval (String currencyAmount ) {
296- // 1. Get the first character of the string, which is the currency symbol (e.g. '€').
297- String currencySymbol = currencyAmount. substring(0 , 1 );
309+ String currencySymbol = Currency . fromCurrencyAmount(currencyAmount);
298310
299- // 2 . Get the rest of the string, which is the amount (e.g. "100").
311+ // 4 . Get the rest of the string, which is the amount (e.g. "100").
300312 String amount = currencyAmount. substring(1 );
301313
302- Currency currency;
303- try {
304- // 3. Look up the currency symbol in our enum to get the corresponding currency code (e.g. "€" => "EUR").
305- currency = Currency . valueOf(currencySymbol);
306- } catch (Exception e) {
307- // 4. If the lookup failed (e.g. currency symbol was not found), we can return "ERR" as the currency code.
308- currency = Currency . ERR ; // e.g. ">" => "ERR"
309- }
310-
311314 // 5. Concatenate the currency code to the amount, and return the result (e.g. "100 EUR").
312- return currency. concatToAmount(amount); // e.g. "100 EUR"
315+ return currency. concatToAmount(amount);
313316 }
314317}
315318```
0 commit comments