Pregunta

This is for my android app Unit Converter. I have three spinners: unit, from and to. Eg. Angle, Degree & Radian.

I have added a listener for the unit spinner. Upon selecting a unit, the from and to spinners will be filled. The user will enter input in the from EditText and upon pressing the Calculate button, a TextView will contain the answer.

I implemented this using if else.

if unit_spinner is Angle
    if from_spinner is Degree
        if to_spinner is Radian
            return input*0.0174532925 //1 degree = 0.0174532925 rad
        else if to_spinner is Gradian
            return input*1.111111111111111 //1 degree = 1.111111111111111 grad

        ...and so on, the cartesian product of all units

This became very long for multiple units. So can you suggest another logic?

¿Fue útil?

Solución 2

Depending on your variables, you could use a switch statement.

switch(someintegervariable){
  case SOME_INT_CONSTANT_1:
    /* ... */
    break;
  case SOME_INT_CONSTANT_2:
    /* ... */
    break;
  default:
    /* ... */
}

Otros consejos

The way you have it now, You have two write NxN if statements for each category that has N units.

if from_spinner is Degree
    if to_spinner is Degree
        return input
    if to_spinner is Radian
        return input * 0.0174532925199
    if to_spinner is Gradian
        return input * 1.11111111111
if from_spinner is Radian
    if to_spinner is Degree
        return input * 57.2957795131
    if to_spinner is Radian
        return input
    if to_spinner is Gradian
        return input * 63.6619772368
if from_spinner is Gradian
    if to_spinner is Degree
        return input * 0.9
    if to_spinner is Radian
        return input * 0.0157079632679
    if to_spinner is Gradian
        return input

Instead, choose a unit that will act as an intermediary between the input and the output. Then you need N if statements to convert from input to intermediary, and N if statements to convert from intermediary to output, for a total of 2N.

//we will use degrees as the intermediary unit
intermediary = null
//caluclate intermediary
if from_spinner is Degree
    intermediary = input
if from_spinner is Radian
    intermediary = input * 57.2957795131
if from_spinner is Gradian
    intermediary = input * 0.9

//calculate final
if to_spinner is Degree
    return intermediary
if to_spinner is Radian
    return intermediary / 57.2957795131
if to_spinner is Gradian
    return intermediary / 0.9

It doesn't seem like it's much more efficient when you have only three units, but for larger values of N, it saves you a lot of effort. For instance, compare this 105 line doubly nested method to its 29 line equivalent that uses intermediary values:

if from_spinner is Millimeter
    if to_spinner is Millimeter
        return input
    if to_spinner is Centimeter
        return input * 0.1
    if to_spinner is Meter
        return input * 0.001
    if to_spinner is Kilometer
        return input * 1e-06
    if to_spinner is Inch
        return input * 0.0393700787402
    if to_spinner is Foot
        return input * 0.00328083989501
    if to_spinner is Mile
        return input * 6.2137273665e-07
if from_spinner is Centimeter
    if to_spinner is Millimeter
        return input * 10.0
    if to_spinner is Centimeter
        return input
    if to_spinner is Meter
        return input * 0.01
    if to_spinner is Kilometer
        return input * 1e-05
    if to_spinner is Inch
        return input * 0.393700787402
    if to_spinner is Foot
        return input * 0.0328083989501
    if to_spinner is Mile
        return input * 6.2137273665e-06
if from_spinner is Meter
    if to_spinner is Millimeter
        return input * 1000.0
    if to_spinner is Centimeter
        return input * 100.0
    if to_spinner is Meter
        return input
    if to_spinner is Kilometer
        return input * 0.001
    if to_spinner is Inch
        return input * 39.3700787402
    if to_spinner is Foot
        return input * 3.28083989501
    if to_spinner is Mile
        return input * 0.00062137273665
if from_spinner is Kilometer
    if to_spinner is Millimeter
        return input * 1000000.0
    if to_spinner is Centimeter
        return input * 100000.0
    if to_spinner is Meter
        return input * 1000.0
    if to_spinner is Kilometer
        return input
    if to_spinner is Inch
        return input * 39370.0787402
    if to_spinner is Foot
        return input * 3280.83989501
    if to_spinner is Mile
        return input * 0.62137273665
if from_spinner is Inch
    if to_spinner is Millimeter
        return input * 25.4
    if to_spinner is Centimeter
        return input * 2.54
    if to_spinner is Meter
        return input * 0.0254
    if to_spinner is Kilometer
        return input * 2.54e-05
    if to_spinner is Inch
        return input
    if to_spinner is Foot
        return input * 0.0833333333333
    if to_spinner is Mile
        return input * 1.57828675109e-05
if from_spinner is Foot
    if to_spinner is Millimeter
        return input * 304.8
    if to_spinner is Centimeter
        return input * 30.48
    if to_spinner is Meter
        return input * 0.3048
    if to_spinner is Kilometer
        return input * 0.0003048
    if to_spinner is Inch
        return input * 12.0
    if to_spinner is Foot
        return input
    if to_spinner is Mile
        return input * 0.000189394410131
if from_spinner is Mile
    if to_spinner is Millimeter
        return input * 1609340.0
    if to_spinner is Centimeter
        return input * 160934.0
    if to_spinner is Meter
        return input * 1609.34
    if to_spinner is Kilometer
        return input * 1.60934
    if to_spinner is Inch
        return input * 63359.8425197
    if to_spinner is Foot
        return input * 5279.98687664
    if to_spinner is Mile
        return input

.

intermediary = null
if from_spinner is Millimeter
    intermediary = input * 0.001
if from_spinner is Centimeter
    intermediary = input * 0.01
if from_spinner is Meter
    intermediary = input * 1.0
if from_spinner is Kilometer
    intermediary = input * 1000.0
if from_spinner is Inch
    intermediary = input * 0.0254
if from_spinner is Foot
    intermediary = input * 0.3048
if from_spinner is Mile
    intermediary = input * 1609.34
if to_spinner is Millimeter
    return intermediary / 0.001
if to_spinner is Centimeter
    return intermediary / 0.01
if to_spinner is Meter
    return intermediary / 1.0
if to_spinner is Kilometer
    return intermediary / 1000.0
if to_spinner is Inch
    return intermediary / 0.0254
if to_spinner is Foot
    return intermediary / 0.3048
if to_spinner is Mile
    return intermediary / 1609.34

I'd recommend using a switch/case statement. With this, you can have a case for each unit type, and then for the calculations.

You could also write methods for each calculation. This may shorten it by a bit, but probably not much.

I'd just go with the case statement. It is relatively simple, but I can put together some code for you if you need help. Just let me know.

Here is the code I might use:

static double DoCalc()
{
    int uid;
    if unit_roller is Angle
        uid = 1;
    else if unit_roller is Length
        uid = 2;
    //etc...

    switch(uid)
        case 1:
            return AngleCalc(input);
        case 2:
            return LengthCalc(input);
        //etc...
        default;
}

static double AngleCalc(double input)
{
    if (from_spinner == to_spinner)
        return input;
    else if (from_spinner is Degree && to_spinner is radian)
        return input*0.0174532925
    //etc...    
}

I will say that this wasn't really any shorter once I saw it put together, put it does avoid the use of nested if statements, and could possibly shorten your code when you have a large number of options.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top