0

I'm not sure how to title this question, but basically we are building a currency converter app and we have a JSON file that looks like this:

{
  "base": "SGD",
  "date": "2016-02-26",
  "rates": {
    "AUD": 0.99008,
    "BGN": 1.2677,
    "BRL": 2.8102,
    "CAD": 0.96636,
    "CHF": 0.70839,
    "CNY": 4.6639,
    "CZK": 17.542,
    "DKK": 4.8354,
    "GBP": 0.5104,
    "HKD": 5.5426,
    "HRK": 4.941,
    "HUF": 201.27,
    "IDR": 9537.9,
    "ILS": 2.7826,
    "INR": 49.002,
    "JPY": 80.646,
    "KRW": 881.73,
    "MXN": 12.914,
    "MYR": 3.0044,
    "NOK": 6.1735,
    "NZD": 1.0596,
    "PHP": 33.884,
    "PLN": 2.828,
    "RON": 2.8934,
    "RUB": 53.827,
    "SEK": 6.074,
    "THB": 25.432,
    "TRY": 2.0966,
    "USD": 0.71338,
    "ZAR": 11.183,
    "EUR": 0.64817
  }
}

And we have a "rates" class that basically contains everything inside the JSON file after the "rates".

private double AUD; ... 

Aand we have a spinneradapter class where we are able to choose what currency we want to convert to, and we need to call the GET method when that item from the spinner. My current solution is to use the traditional method of if-else or cases to retrieve the data.

if( ((Spinner)findViewById(R.id.spinner1)).getSelectedItem().toString() == "AUD" )
    {
            myRates.getAUD();
    }

But we'll need to do this consistently for 30 other variables. Is there any better way to work around this?

Getting the rates from a parser:

protected void onPostExecute(JSONArray json) {

        if (json != null) {
            // looping through All records
            for (int i = 0; i < json.length(); i++) {

                try {
                    JSONObject c = json.getJSONObject(i);

                    rates.setAUD(c.getDouble("AUD"));
                    rates.setBRL(c.getDouble("BRL"));
                    rates.setBGN(c.getDouble("BGN"));
                    rates.setCAD(c.getDouble("CAD"));
                    rates.setCNY(c.getDouble("CNY"));

                    rates.setCHF(c.getDouble("CHF"));
                    rates.setCZK(c.getDouble("CZK"));
                    rates.setDKK(c.getDouble("DKK"));
                    rates.setEUR(c.getDouble("EUR"));
                    rates.setGBP(c.getDouble("GBP"));

                    rates.setHKD(c.getDouble("HKD"));
                    rates.setHRK(c.getDouble("HRK"));
                    rates.setHUF(c.getDouble("HUF"));
                    rates.setIDR(c.getDouble("IDR"));
                    rates.setILS(c.getDouble("ILS"));

                    rates.setINR(c.getDouble("INR"));
                    rates.setJPY(c.getDouble("JPY"));
                    rates.setKRW(c.getDouble("KRW"));
                    rates.setMXN(c.getDouble("MXN"));
                    rates.setMYR(c.getDouble("MYR"));

                    rates.setNOK(c.getDouble("NOK"));
                    rates.setNZD(c.getDouble("NZD"));
                    rates.setPHP(c.getDouble("PHP"));
                    rates.setPLN(c.getDouble("PLN"));
                    rates.setRON(c.getDouble("RON"));

                    rates.setRUB(c.getDouble("RUB"));
                    rates.setSEK(c.getDouble("SEK"));
                    rates.setTRY(c.getDouble("TRY"));
                    rates.setTHB(c.getDouble("THB"));
                    rates.setUSD(c.getDouble("USD"));
                    rates.setZAR(c.getDouble("ZAR"));
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

        }

    }
}
Kei
  • 315
  • 1
  • 4
  • 18

1 Answers1

1

Possible options are

  • Use switch case instead of if else : if you are using java 7 or greater then only switch case of string will work.
  • Create a HashMap of String to Double : and add all rates in that HashMap
  • Implementing the cases as elements of an Enum may help somewhat - example
Community
  • 1
  • 1
ELITE
  • 5,815
  • 3
  • 19
  • 29