henrycharles henrycharles - 6 months ago 38
Java Question

getting currency code if we have only country code in database

i have problem getting the country currency, i am having the country code in database through these code i have to get the country code in java how do i make the code for this please suggest me.
i m trying with this example but its not working.

class Utils {
public static SortedMap<Currency, Locale> currencyLocaleMap;
static {
currencyLocaleMap = new TreeMap<Currency, Locale>(
new Comparator<Currency>() {
public int compare(Currency c1, Currency c2) {
return c1.getCurrencyCode().compareTo(
c2.getCurrencyCode());
}
});
for (Locale locale : Locale.getAvailableLocales()) {
try {
Currency currency = Currency.getInstance(locale);
currencyLocaleMap.put(currency, locale);
} catch (Exception e) {
}
}
}

public static String getCurrencySymbol(String currencyCode) {
Currency currency = Currency.getInstance(currencyCode);
System.out.println(currencyCode + ":-"
+ currency.getSymbol(currencyLocaleMap.get(currency)));
return currency.getSymbol(currencyLocaleMap.get(currency));
}
}

public class GetCurrency {
public static void main(String[] args) {

Utils.getCurrencySymbol(Currency.getInstance("INR").getCurrencyCode());
Utils.getCurrencySymbol(Currency.getInstance(Locale.JAPAN)
.getCurrencyCode());
Utils.getCurrencySymbol(Currency.getInstance(Locale.UK)
.getCurrencyCode());

Utils.getCurrencySymbol("INR");
}
}

Answer

i found the solution for my problem this is the code i have done.

        public static Map<Currency, Locale> getCurrencyLocaleMap() {
        Map<Currency, Locale> currencyLocaleMap = new HashMap<Currency, Locale>();
        for (Locale locale : Locale.getAvailableLocales()) {
            try {
                Currency currency = Currency.getInstance(locale);
                currencyLocaleMap.put(currency, locale);
            } catch (Exception e) {
            }
        }
        return currencyLocaleMap;
    }

    public static String getCurrencySymbol(String currencyCode) {
        String currencySymbol = null;
        if (currencyCode == null || currencyCode.isEmpty()) {
            currencySymbol = currencyCode;
        } else {
            Locale currencyLocale = null;
            Map<Currency, Locale> currencyLocaleMap = null;
            Currency currency = null;
            try {
                currency = Currency.getInstance(currencyCode);
                currencyLocaleMap = getCurrencyLocaleMap();
                currencyLocale = currencyLocaleMap.get(currency);
            } catch (Exception e) {
                System.out.println("No symbol is there for currencyCode="
                        + currencyCode);
            }
            if (currency != null && currencyLocale != null) {
                currencySymbol = currency.getSymbol(currencyLocaleMap
                        .get(currency));
            } else {
                currencySymbol = currencyCode;
            }
        }
        return currencySymbol;
    }
}