-3

I have multiple inputs from a user based on that I wrote some methods to perform some operations.

To handle user input I have to use if else if or switch case. Looking for more feasible solutions rather than using multiple if-else or switch cases.

Input samples include radio buttons, checkboxes, dropdowns. So I have already written some methods to perform some operations.

Instead of that is there any feasible solution.

Bharadwaj Pendyala
  • 324
  • 1
  • 3
  • 16
Niranjan V
  • 15
  • 4
  • 4
    Provide a [a minimal, complete and verifiable example ][1] [1]: https://stackoverflow.com/help/minimal-reproducible-example – Shubham Saraswat Oct 25 '19 at 05:44
  • Perhaps you're looking for [Alternative to Switch Case in Java](https://stackoverflow.com/q/1425659) or [Is there Any Alternative for Switch Case Statements in Java ? Any Good Design Pattern ? My Switch case Statement Increasing How to Avoid](https://stackoverflow.com/q/47172767) or [Can somebody recommend a java 8 pattern to replace a switch statement?](https://stackoverflow.com/q/25873571)? – dbc Oct 25 '19 at 06:35

1 Answers1

0

You can create an abstract layer. Something like this:

    public class TestInputs() {

    boolean radioButtonFoo1
    boolean checkBoxFoo2

    public boolean getRadioButtonFoo1() {
    return radioButtonFoo1
    }

    public boolean getCheckBoxFoo2() {
    return checkBoxFoo2
    }

    public TestInputs(boolean radioButtonFoo1, boolean checkBoxFoo2) {
    this.radioButtonFoo1 = radioButtonFoo1
    this.checkBoxFoo2 = checkBoxFoo2
    }

    TestInputs testInputs = new TestInputs(true, false)
    }

Your test case class can extend this TestInputs class so you can call your test data directly:

if (testInputs.getRadioButtonFoo1 == true) {
// do something
}
pburgr
  • 1,722
  • 1
  • 11
  • 26