Shayd3
Shayd3

Reputation: 141

Shorten Redundant if statements Into a single Method (Java)

How can I write this section of code within a method to make this shorter? Is there a way to take the input and use that for the action? Example: User enters in white and it will replace x in bgColor = Color.x; I'm just trying to get rid of all of the if statements.

    sg.print("Please enter a background color");

    String colorString = sg.keyReadString();
    Color bgColor = DEFAULT_COLOR;

    if("white".equalsIgnoreCase(colorString))
    {
        bgColor = Color.WHITE;
    }
    else if("red".equalsIgnoreCase(colorString))
    {
        bgColor = Color.RED;
    }
    else if("orange".equalsIgnoreCase(colorString))
    {
        bgColor = Color.ORANGE;
    }
    else if("yellow".equalsIgnoreCase(colorString))
    {
        bgColor = Color.YELLOW;
    }
    else if("green".equalsIgnoreCase(colorString))
    {
        bgColor = Color.GREEN;
    }
    else if("blue".equalsIgnoreCase(colorString))
    {
        bgColor = Color.BLUE;
    }
    else if("gray".equalsIgnoreCase(colorString))
    {
        bgColor = Color.GRAY;
    }
    else if("magenta".equalsIgnoreCase(colorString))
    {
        bgColor = Color.MAGENTA;
    }
    else if("cyan".equalsIgnoreCase(colorString))
    {
        bgColor = Color.CYAN;
    }
    else if("pink".equalsIgnoreCase(colorString))
    {
        bgColor = Color.PINK;
    }
    else
    {
        sg.print("couldn't understand your color, so defaulting to black");
    }

    sg.setBackgroundColor(bgColor);  

Upvotes: 0

Views: 79

Answers (3)

Ken Geis
Ken Geis

Reputation: 922

A short and quick way would be to do the following

Color bgColor = (Color) Color.class.getDeclaredField(colorString.toUpperCase()).get(null);

if(gbColor == null) {
    bgColor = DEFAULT_COLOR;
    sg.print("couldn't understand your color, so defaulting to black");
}

Upvotes: 0

Madushan Perera
Madushan Perera

Reputation: 2598

Or you can easily put all your required colors into a map and filter given color form the map

Color bgColor;
String givenColor="blUe";
Map<String, Color> colors = new HashMap<>();
colors.put("RED", Color.RED);
colors.put("BLUE", Color.BLUE);
colors.put("WHITE", Color.WHITE);
colors.put("GREEN", Color.GREEN);

bgColor=colors.get(givenColor.toUpperCase());

Upvotes: 2

Bon
Bon

Reputation: 3103

In java 7 or above, you can use switch on string to make your logic more concise. Convert string to lower case first so that you don't need to worry about the case of the user input.

String colorString = sg.keyReadString().toLowerCase();

    switch (colorString) {
        case "white":
            bgColor = Color.WHITE;
            break;
        case "red":
            bgColor = Color.RED;
            break;
        case "orange":
            bgColor = Color.ORANGE;
            break;
        case "yellow":
            bgColor = Color.YELLOW;
            break;
        case "green":
            bgColor = Color.GREEN;
            break;
        default:
            bgColor = DEFAULT_OPTION;
            break;
    }

Upvotes: 1

Related Questions