javaif-statementscenebuilderreducing

Reducing IF statement java


i am trying to make a unit converter using javafx - i've been searching around for two days on how can i reduces these if-statements. somehow, i found some similar issue but it didn't help me since i am new with Java - i do not know the right approach in my case.

Hope you guys could help me -

thanks

/**
     * CELSIUS TO
     */
    Celsius celsius = new Celsius(parseInput);
        if(cbOne.getValue().equals("Celsius")) {
            if(cbTwo.getValue().equals("Celsius") ) {
                showAnswer.setText(celsius.celsiusToCelsius());
            }
            if(cbTwo.getValue().equals("Fahrenheit")) {
                showAnswer.setText(celsius.celsiusToFahrenheit());
            }
            if(cbTwo.getValue().equals("Kelvin")) {
                showAnswer.setText(celsius.celsiusToKelvin());
            }
        }
    /**
     * FAHRENHEIT TO
     */
    Fahrenheit fahr = new Fahrenheit(parseInput);
        if(cbOne.getValue().equals("Fahrenheit") ) {
            if(cbTwo.getValue().equals("Celsius") ) {
                showAnswer.setText(fahr.fahrenheitToCelsius());
            }
            if(cbTwo.getValue().equals("Fahrenheit")) {
                showAnswer.setText(fahr.fahrenheitToFahrenheit());
            }
            if(cbTwo.getValue().equals("Kelvin")) {
                showAnswer.setText(fahr.fahrenheitToKelvin());
            }
        }
    /**
     * KELVIN TO
     */
    Kelvin kelvin = new Kelvin(parseInput);
        if(cbOne.getValue().equals("Kelvin")) {
            if(cbTwo.getValue().equals("Celsius") ) {
                showAnswer.setText(kelvin.kelvinToCelsius());
            }
            if(cbTwo.getValue().equals("Fahrenheit")) {
                showAnswer.setText(kelvin.kelvinToFahrenheit());
            }
            if(cbTwo.getValue().equals("Kelvin")) {
                showAnswer.setText(kelvin.kelvinToKelvin());
            }
        }
    }

Solution

  • You do not need 3 classes to represent temperatures in different scales. Create a class that always keeps temperature in Kelvins internally and can convert it to any other scale for output. Having a class like this:

    public final class Temperature {
      public enum Scale {
        Celsius, Fahrenheit, Kelvin
      }
    
      private final double temperature;
    
      private Temperature(double temperature) {
        this.temperature = temperature;
      }
    
      public static Temperature create(double temperature, Scale scale) {
        switch (scale) {
          case Celsius:
            return new Temperature(temperature + 273.15);
          case Fahrenheit:
            return new Temperature((temperature + 459.67) * 5.0 / 9.0);
          case Kelvin:
            return new Temperature(temperature);
          default:
            throw new IllegalArgumentException("Unknown scale");
        }
      }
    
      public double convertTo(Scale scale) {
        switch (scale) {
          case Celsius:
            return temperature - 273.15;
          case Fahrenheit:
            return temperature * 9.0 / 5.0 - 459.67;
          case Kelvin:
            return temperature;
          default:
            throw new IllegalArgumentException("Unknown scale");
        }
      }
    }
    

    Your code becomes:

    Temperature temp = Temperature.create(parseInput, Scale.valueOf(cbOne.getValue()));
    showAnswer.setText(temp.convertTo(Scale.valueOf(cbTwo.getValue())));