Linked Questions

Popular Questions

How to convert my enumeration code into a switch

Asked by At

In my CustomerTypeApp class, I need to change the getDiscountPercent method to use a switch instead of a chain of if statements. Here is the if statement version:

public static double getDiscountPercent(CustomerType ct) {
        double discountPercent = 0;
        if (ct == CustomerType.RETAIL) {
            discountPercent = 0.156;
        } else if (ct == CustomerType.TRADE) {
            discountPercent = 0.30;
        } else if (ct == CustomerType.COLLEGE) {
            discountPercent = 0.20;
        }
        return discountPercent;
    }
}

Below is the switch statement I have tried, but which receives the error:

An enum switch case label must be the unqualified name of an enumeration constant

  double discountPercent = 0;

  switch(ct) {
      case CustomerType.RETAIL :
        discountPercent = 0.156;
        break;
     case CustomerType.TRADE :
        discountPercent = 0.30;
        break;
     case CustomerType.COLLEGE :
        discountPercent = 0.20;
        break;
     default :
        discountPercent = 0;
  }
  return discountPercent;

Related Questions