0

I have a question involving EnumSet and Enum. More specifically, about passing them as paramaters and returning them from methods.

Basically, I want to have a method in one class create an EnumSet of certain elements of a Enum than I want said class to pass the EnumSet to another class where the new class uses logic to choose one specific element from the set and pass it to a third class that performs some desired action. If that is too confusing, I tried to provide some context below.

I'm trying to make a blackjack simulator program which has a several classes. The classes that pertain to my question are Dealer, Player, and Hand.

The issue I'm having is I want the Hand class to have an enum of options e.g.

enum Options { SPLIT, DOUBLE, HIT, STAND}

Then I want the hand to examine the contents of the hand which is an ArrayList cards (the Integer value in the array list simply represents the cards rank, suit is unimportant), to determine which options the Player object has available.

I would like a getPlayerOptions() method in the Hand class that determines the possible Options and creates an

EnumSet<Options> availableOptions = EnumSet.of(.....)

containing the available options.

I fully know how the Hand class can perform the logic and create the EnumSet I need, but I struggle to find out how to send "EnumSet availableOptions" to the player class as a paramater and than have the player class run a playerOption() method which returns one "option" to the Dealer object where dealer deals cards accordingly.

this is just a snippet without any logic but does this work for returning the enum set:

import java.util.EnumSet;

public class Hand {  

    enum Options {  
        SPLIT, FREEDOUBLE, DOUBLE, HIT, STAND  
    }  

    public EnumSet getOptions() {  
        EnumSet<Options> playerOptions = EnumSet.of(Options.STAND);  
        return playerOptions;
    }
}
Glenn
  • 8,932
  • 2
  • 41
  • 54
  • Can you show some code and explain *exactly* what you don't understand? An `EnumSet` is an object like any other object, that can be passed to and returned from methods like any other method parameter or return value. There's nothing special with `EnumSet` that makes this different than with any other object. – Jesper Jan 03 '16 at 19:10
  • this is just a snippet without any logic but does this work for returning the enum set `import java.util.EnumSet; public class Hand { enum Options { SPLIT, FREEDOUBLE, DOUBLE, HIT, STAND } public EnumSet getOptions() { EnumSet playerOptions = EnumSet.of(Options.STAND); return playerOptions; }` } @Jesper – Shawn Anderson Jan 03 '16 at 19:25
  • Your code looks good. I don't see what is it you're having problems with. – Rudi Angela Jan 03 '16 at 19:35
  • 6
    The return type of the method should be `EnumSet`. Don't use raw types. I would also rename the enum `Option`: each instance is one Option, not several Options. – JB Nizet Jan 03 '16 at 20:04

0 Answers0