Is there a more efficient way to set different cases of ActionEvent than using if else statements?

41 Views Asked by At

I'm trying make a currency exchange program and I'm using a ComboBox to list the type of conversions the user wants to make and when the user selects a choice, it will make the conversion. Is there a better way to use the equals.IgnoreCase than using a bunch of if else statements? I created an array of strings for the choices, then declared the JComboBox with the array as the parameter.

@Override
public void actionPerformed(ActionEvent e) {
    String text = textField.getText();
    double c = Double.parseDouble(text);
    if(e.getSource()==comboBox) {
        String choice = comboBox.getSelectedItem().toString();
        if(choice.equalsIgnoreCase("Dollars to Pesos")) { // placeholder text
            System.out.println(choice);
            
        }
        else if(choice.equalsIgnoreCase("Dollars to Pesos")) {
            System.out.println(choice);
            
        }
        else if(choice.equalsIgnoreCase("Pesos to Dollars")) {
            System.out.println(choice);
            
        }
        else if(choice.equalsIgnoreCase("Dollars to GB Pounds")) {
            System.out.println(choice);
            
        }
        else if(choice.equalsIgnoreCase("GB Pounds to Dollars")) {
            System.out.println(choice);
            
        }
        else if(choice.equalsIgnoreCase("Dollars to Yen")) {
            System.out.println(choice);
            
        }
        else if(choice.equalsIgnoreCase("Yen to Dollars")) {
            System.out.println(choice);
            
        }
        
    }
    
}
2

There are 2 best solutions below

2
Elliott Frisch On

I created an array of strings for the choices Yes. Use that array. Assuming it's called possibleChoices, that might look like

private static String[] possibleChoices = {
        "Dollars to Pesos",
        "Pesos to Dollars",
        "Dollars to GB Pounds",
        "GB Pounds to Dollars",
        "Dollars to Yen",
        "Yen to Dollars"
};

@Override
public void actionPerformed(ActionEvent e) {
    String text = textField.getText();
    double c = Double.parseDouble(text);
    if (e.getSource() == comboBox) {
        String choice = comboBox.getSelectedItem().toString();
        if (Arrays.stream(possibleChoices).anyMatch(choice::equalsIgnoreCase)) {
            System.out.println(choice);
        }
    }
}
0
Akhilesh Pandey On

Java 8 or above you can use a Map of functional interfaces to create a more flexible solution as below ,

Map<String, Runnable> map = new HashMap<>();
map.put("dollars to pesos", () -> System.out.println("Dollars to Pesos"));
map.put("pesos to dollars", () -> System.out.println("Pesos to Dollars"));
map.put("dollars to gb pounds", () -> System.out.println("Dollars to GB Pounds"));
map.put("gb pounds to dollars", () -> System.out.println("GB Pounds to Dollars"));
map.put("dollars to yen", () -> System.out.println("Dollars to Yen"));
map.put("yen to dollars", () -> System.out.println("Yen to Dollars"));

@Override
public void actionPerformed(ActionEvent e) {
    if(e.getSource()==comboBox) {
        String choice = comboBox.getSelectedItem().toString().toLowerCase();
        Runnable runnable = map.get(choice);
        if (runnable != null) {
            runnable.run();
        }
    }
}

This approach has the added benefit of making it easy to add or remove currency conversions simply by adding or removing entries in the map, without having to touch the logic in actionPerformed method. It is more scalable if you anticipate the list of conversions growing in the future.