简体   繁体   中英

How to use Enum if your value is not fixed?

I know I was not clear with my question but what I am trying to do here is use enums to convert Temperature Units and all I know that enums have a fixed value so for example Converting from Fahrenheit to Celsius needs the input for the calculation to work but I cant use the Input which is in the main activity so I tried to declare an input with its setter and use the class Convert2 to set the input in the main activity(something like that c.setInput(input)) and getting its value to be able to use it in the class thinking that if you get the value you can add it in the constant then make the return value at the end equals to 1 multiplied by the multiplier which is = to the constant but that did not work and I am stuck here since I am new to enums. hope you will understand what I mean in the code.

public class Convert2 {

    private final double multiplier;

    private double input;

    public void setInput(double input) {
        this.input = input;
    }

    public double getInput() {
        return input;
    }

    public enum Unit2 {
        Fahrenheit,
        Celsius,
        Kelvin,
        ;

        public static Unit2 from(String text) {
            if (text != null) {
                for (Convert2.Unit2 unit2 : Convert2.Unit2.values()) {
                    if (text.equalsIgnoreCase(unit2.toString())) {
                        return unit2;

                    }
                }
            }

            throw new IllegalArgumentException("Cannot find a value for " + text);
        }
    }

    public Convert2(Convert2.Unit2 from, Convert2.Unit2 to) {
        double constant = 1;

        switch (from) {
            case Fahrenheit:
                if (to == Convert2.Unit2.Celsius) {
                    constant = (input - 32)
                            * 1.8;///here you can see how the calculation work as it needs the input value///


                } else if (to == Convert2.Unit2.Kelvin) {
                    constant = (5 / 9) + 273.15 - 32;
                }
                break;
        }
        multiplier = constant;

    }

    public double convert2(double input) {
        return (input - input + 1)
                * multiplier;///(input-input+1) gives 1 multiplied by the multiplier gives the constant value///
    }
}

You can take a look at using switch statements. For example

If you define a Unit enum as follows

enum Unit {
    FAHRENHEIT,
    CELSIUS,
    KELVIN
}

A complete "convert" method will evalute all cases:

  1. f -> f

  2. f -> c

  3. f -> k

  4. c -> f

  5. c -> c

  6. c -> k

  7. k -> f

  8. k -> c

  9. k -> k

So in all you have something like

public double convert(double input, Unit from, Unit to) {
    if (from == null || to == null) {
        throw new IllegalArgumentException("units cannot be null");
    }
    switch (from) {
        case FAHRENHEIT:
            switch (to) {
                case FAHRENHEIT:
                    return input;
                case CELSIUS:
                    return (input - 32) * (5D / 9D);
                case KELVIN:
                    return (input + 459.67) * (5D / 9D);
                default:
                    throw new IllegalStateException("Unhandled from / to values: " + from + " " + to);
            }
        case CELSIUS:
            switch (to) {
                case FAHRENHEIT:
                    return input * (9D / 5D) + 32;
                case CELSIUS:
                    return input;
                case KELVIN:
                    return input + 273.5;
                default:
                    throw new IllegalStateException("Unhandled from / to values: " + from + " " + to);
            }
        case KELVIN:
            switch (to) {
                case FAHRENHEIT:
                    return (input - 273.5) * (9D / 5D) + 32;
                case CELSIUS:
                    return input - 273.5;
                case KELVIN:
                    return input;
                default:
                    throw new IllegalStateException("Unhandled from / to values: " + from + " " + to);
            }
        default:
            throw new IllegalStateException("Unhandled from value: " + from);
    }

}

Which, if you want to, you can tidy up a bit by refactoring into smaller methods based on the input to reduce the branching in the switch statements.

An alternative to this, if you are using java 13 or higher, is to use an enhanced switch. At that point the convert method would look like this:

public double convert(double input, Unit from, Unit to) {
    if (from == null || to == null) {
        throw new IllegalArgumentException("units cannot be null");
    }
    return switch (from) {
        case FAHRENHEIT -> switch (to) {
            case FAHRENHEIT -> input;
            case CELSIUS -> (input - 32) * (5D / 9D);
            case KELVIN -> (input + 459.67) * (5D / 9D);
        };
        case CELSIUS -> switch (to) {
            case FAHRENHEIT -> input * (9D / 5D) + 32;
            case CELSIUS -> input;
            case KELVIN -> input + 273.5;
        };
        case KELVIN -> switch (to) {
            case FAHRENHEIT -> (input - 273.5) * (9D / 5D) + 32;
            case CELSIUS -> input - 273.5;
            case KELVIN -> input;
        };
    };

See here , I think it's much more readable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM