0

I have created a currency conversion calculator. However, since the API I am using doesn't support certain African currencies I am hard-coding their exchange rates in.

The following is a sample of one of the conversions. The way it works is I get fromCurr and toCurr from two combo boxes. If the combo boxes detect one of the four African currencies selected (Egyptian Pound, Nigerian Naira, Algerian Dinar and Moroccan Dirham) the application should use the hard-coded values. Otherwise, the application should make use of the ExchangeRate class I have coded.

ExchangeRate er = new ExchangeRate();

    double result = 0;

    if ((fromCurr != "Egyptian Pound" && fromCurr != "Nigerian Naira" && fromCurr != "Algerian Dinar" && fromCurr != "Moroccan Dirham") && (toCurr != "Egyptian Pound" && toCurr != "Nigerian Naira" && toCurr != "Algerian Dinar" && toCurr != "Moroccan Dirham")) {
        try {
            String exRate = er.getExRate(fromCurr, toCurr);
            double exRateAmnt = Double.parseDouble(exRate);
            result = Double.parseDouble(txfEq.getText())*exRateAmnt;
        } catch (IOException fe) {
            System.out.println(fe);
        } catch (NumberFormatException nf) {
            // If the text field is empty, output an error
            txfDisp.setText("ERR");
        }
    } else if ((fromCurr == "Egyptian Pound" || fromCurr == "Nigerian Naira" || fromCurr == "Algerian Dinar" || fromCurr == "Moroccan Dirham") || (toCurr == "Egyptian Pound" || toCurr == "Nigerian Naira" || toCurr == "Algerian Dinar" || toCurr == "Moroccan Dirham")) {
        if (fromCurr == "Egyptian Pound") {
            if (toCurr == "United States Dollar") {
                result = Double.parseDouble(txfEq.getText())*0.063;
            } else if (toCurr == "Euro") {
                result = Double.parseDouble(txfEq.getText())*0.058;
            } else if (toCurr == "Japanese Yen") {
                result = Double.parseDouble(txfEq.getText())*6.77;
            } else if (toCurr == "British Pound") {
                result = Double.parseDouble(txfEq.getText())*0.051;
            } else if (toCurr == "Australian Dollar") {
                result = Double.parseDouble(txfEq.getText())*0.099;
            } else if (toCurr == "Swiss Franc") {
                result = Double.parseDouble(txfEq.getText())*0.061;
            } else if (toCurr == "New Zealand Dollar") {
                result = Double.parseDouble(txfEq.getText())*0.11;
            } else if (toCurr == "Canadian Dollar") {
                result = Double.parseDouble(txfEq.getText())*0.089;
            } else if (toCurr == "South Korean Won") {
                result = Double.parseDouble(txfEq.getText())*77.65;
            } else if (toCurr == "Mexican Peso") {
                result = Double.parseDouble(txfEq.getText())*1.56;
            } else if (toCurr == "Russian Ruble") {
                result = Double.parseDouble(txfEq.getText())*4.77;
            } else if (toCurr == "Turkish Lira") {
                result = Double.parseDouble(txfEq.getText())*0.45;
            } else if (toCurr == "UAE Dirham") {
                result = Double.parseDouble(txfEq.getText())*0.23;
            } else if (toCurr == "South African Rand") {
                result = Double.parseDouble(txfEq.getText())*1.18;
            } else if (toCurr == "Egyptian Pound") {
                result = Double.parseDouble(txfEq.getText())*1;
            } else if (toCurr == "Nigerian Naira") {
                result = Double.parseDouble(txfEq.getText())*24.7;
            } else if (toCurr == "Algerian Dinar") {
                result = Double.parseDouble(txfEq.getText())*8.13;
            } else if (toCurr == "Moroccan Dirham") {
                result = Double.parseDouble(txfEq.getText())*0.62;
            }

Conversions between an African currency and another African currency work. Conversions between a non-African currency and another non-African currency work. However, the problem arises when converting from an African currency to a non-African currency and vice-versa. The output turns up as zero no matter what initial value I try to convert.

I suspect this is a problem either with my if/else syntax, which shouldn't be the case since the African-African conversion works, or it could be with the fact that the non-African currencies are used in both the main if and the else if, but this shouldn't be the case either. Just trying to figure it out, but I still cannot figure out where the problem lies.

0 Answers0