James Ng
James Ng

Reputation: 11

Is there a better way to process these if statements?

I'm writing a Java application that plots graphs for min/max/avg values of an accelerometer and gyroscope in their three fields xyz. I would like to be able to plot each factor independently, i.e. maximum acceleration in the X field with the average acceleration in the Z field etc.

I have used radio buttons for min/max/avg and for acceleration/y/z and gyroscopex/y/z.

I also thought about making radio buttons for min/max/avg for each variable but then I would have 18 separate variables.

Here is a snippet of my code:

if (selectionAccelX == 1 && selectionAccelY == 0 && selectionAccelZ == 0 && 
    selectionGyroX == 0 && selectionGyroY == 0 && selectionGyroZ == 0 && 
    selectionXAxis == 0) {

    Graphing graphing = new Graphing("Average AccelerationX");
    graphing.setAccelerationX(avgAccXSec);
    graphing.displayGraphing();
    graphing.pack();
    RefineryUtilities.centerFrameOnScreen(graphing);
    graphing.setVisible(true);
}

Is there an easier way to go about this?

Upvotes: 0

Views: 98

Answers (1)

Th. Thielemann
Th. Thielemann

Reputation: 2824

Your code maps one variable (the radio button selector) to another variable (the value selector). One possible solution can be to

  • add an enum (with 18 values again)
  • bind every value of the enum to the return value of the radio button
  • add an switch/case based on the return value to trigger the right behaviour

I assume that is not lesser code but can be more readable.

Upvotes: 1

Related Questions