Q:

Get a property of an enum based on another property

If I have this enum
public static enum Motorcycle {
    YAMAHA("Y", "commons.blue"), BMW("B", "commons.red"), HONDA("H", "commons.yellow"), KAWASAKI("K", "commons.green");

    private String abbreviation;
    private String color;

    SampleStatus(String abbreviation, String color) {
        this.abbreviation = abbreviation;
        this.color = color;
    }

    public String getAbbreviation() {
        return abbreviation;
    }

    public String getColor() {
        return color;
    }
}

How can I get the color if I have the abbreviation?
E.g.:
String brand = "Y";
How can I get the corresponding color ("commons.blue")

A:

The main method for that :
  public static void main(String... s){
    for(Motorcycle m : Motorcycle.values()){
        if(m.getAbbreviation().equals("Y")){
            System.out.println(m.getColor());
            break;
        }
    }
  }

EDIT use this:
 public static String getColorByAbbreviation(String abbreviation){
    for(Motorcycle m : Motorcycle.values()){
        if(m.getAbbreviation().equals(abbreviation)){
            return m.getColor();
        }
    }
    return "";
}

You can call it by Motorcycle.getColorByAbbreviation("B")

