-1

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;
Nick
  • 138,499
  • 22
  • 57
  • 95
Avery O
  • 63
  • 5
  • 2
    Are you facing any *specific* problem while writing your code which would involve switch? This looks like homework task so can't simply spoon feed you solution without seeing any honest attempt first. – Pshemo Feb 03 '19 at 17:10
  • From the above, you don't seem to have tried to use `switch` (yet). Your best bet is to try to use it, and then post a question **if** you run into a specific problem doing so. (Converting the above to `switch` is very straightforward, you won't run into a problem.) – T.J. Crowder Feb 03 '19 at 17:11
  • 2
    Possible duplicate: [Java using enum with switch statement](https://stackoverflow.com/q/8108980) – Pshemo Feb 03 '19 at 17:17
  • `switch(ct)` knows what is type of `ct` so `CustomerType.` part is not necessary in `case` part – Pshemo Feb 03 '19 at 17:34
  • Pshemo is right. See this answer: https://stackoverflow.com/a/8109054/7098259 – Patrick Parker Feb 03 '19 at 18:00

2 Answers2

1

You want to switch for the variable ct

switch(ct) {
        case CustomeType.retail:
            /*Command*/
            break;
        case CustomerType.TRADE:
            /*Command*/
            break;
        default:
            /*else*/
}

If you need further help read these Java Docs

PixelRayn
  • 392
  • 2
  • 13
0

Try this: (It's Very Simple)

public static double getDiscountPercent(CustomerType ct) {

      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;

   }
sh7411usa
  • 196
  • 1
  • 13
  • I had tried this thinking it didn't work. I get the error message "An enum switch case label must be the unqualified name of an enumeration constant" – Avery O Feb 03 '19 at 17:20
  • @AveryO To get better help use edit option under your question and include your invalid code along with error message. – Pshemo Feb 03 '19 at 17:22
  • how is your enumeration declared? – sh7411usa Feb 03 '19 at 17:41