-1

I created this enum:

public enum CoffeeSorts {
    Coffee("Kaffee"), Espresso("Espresso"), Mocca("Mocca"), Cappuccino(
            "Cappuccino"), LatteMacchiato("Latte Macchiato"), DoubleEspresso(
            "doppelter Espresso");

    private final String stringValue;

    private CoffeeSorts(final String s) {
        stringValue = s;
    }

    public String toString() {
        return stringValue;
    }
}

I tried the following way to use it

public ACoffee createCoffee(String type) {

        switch (type) {
        case CoffeeSorts.Cappuccino :
            try {
                return new ChocolateSprincles(new Cream(new Coffee()));
            } catch (Exception e) {}
            return null;
            break;
        case CoffeeSorts.LatteMacchiato :
            try {
                return new ...
            }
        .
        .
        .
    }

It only gives me an Error saying "cannot convert from CoffeeSorts to String". Can you tell me what i did wrong?

baxbear
  • 679
  • 2
  • 6
  • 24

3 Answers3

5

Your type variable is a String, but you're trying to specify values which are CoffeeSort values. You'll need to convert the String to a CoffeeSort first, or change the signature.

For example:

public ACoffee createCoffee(CoffeeSort type) {
    ...
}

or

public ACoffee createCoffee(String typeName) {
    CoffeeSort type = CoffeeSort.valueOf(typeName);
    ...
}

Also note that you can't break; after a return statement, as it's unreachable code. (I hope your exception handling isn't really like that, either...)

Finally, consider changing your code entirely to put a createCoffee method inside the enum itself. Then you won't need a switch statement at all. You can make it an abstract method which is overridden in each enum value.

public enum CoffeeSort {
    Coffee("Kaffee") {
        @Override public ACoffee createCoffee() {
           ...
        }
    },
    Espresso("Espresso") { ... },
    Mocca("Mocca") { ... },
    Cappuccino("Cappuccino") { ... },
    LatteMacchiato("Latte Macchiato") { ... },
    DoubleEspresso("doppelter Espresso") { ... };

    private final String stringValue;

    private CoffeeSorts(final String s) {
        stringValue = s;
    }

    @Override
    public String toString() {
        return stringValue;
    }

    public abstract ACoffee createCoffee();
}
Jon Skeet
  • 1,261,211
  • 792
  • 8,724
  • 8,929
  • I like most of your post, but that pattern you're proposing with the factory methods inside the enum values is really not my cup of tea ;-) – Robby Cornelissen Jun 10 '14 at 10:56
  • 1
    @RobbyCornelissen: Coffee/tea differences, aside, it depends on the context. I *love* putting behaviour inside enums when it really belongs there - when it's not context-specific, for example. Is the `CoffeeSort` type the piece of code which should unambiguously know how to create a cup of coffee? If so, my solution is great IMO - polymorphism wins over switch/case where feasible. If, however, you've got different situations (e.g. "create a cup of coffee in XML form" vs "create a cup of coffee as JSON") then it's a different matter. – Jon Skeet Jun 10 '14 at 11:00
  • Agreed. It most certainly depends on the case. – Robby Cornelissen Jun 10 '14 at 11:09
4

The type in your switch(type) should be an object of type CoffeeSorts. You're passing in a string.

Robby Cornelissen
  • 72,308
  • 17
  • 104
  • 121
2

Your method signature should be:

public ACoffee createCoffee(CoffeeSorts type)

Your switch statement works on type and the case statements that you put there only make sense if type is of type CoffeeSorts.

Sidenote: enums are typically capitalized in Java (see Coding Conventions - Naming Enums).

Community
  • 1
  • 1
reto
  • 8,367
  • 4
  • 47
  • 50