Andrew
Andrew

Reputation: 462

How do I access a particular Enum value in another class?

Basically, I'm trying to create my own Craps game with a "betting" feature where you can play and see an update of how much money you've won or lost. To do this, I've created the Craps Class which allows the player to play Craps a single time, and my code is reliant on a custom enumeration that I call "Status" (private static enum Status) with the values of PASS_WON, PASS_LOST, DP_WON, DP_LOST, KEEP_ROLLING.

The part I'm struggling with is the first step of the BetMoney class. I want to start by saying if at the end of the Craps Game, if the status is WON, then money is added to the money you have already if the status is LOST, money is subtracted, etc, etc. However, I cannot access the private Status enum that I declared in the Craps Class, in the BetMoney class in order to do this if statement. I am completely unsure how to create a getter for an enum like this. Is there any way to "get" the values of the enum in my BetMoney class, so I can use them in an if Statement? What I want to do is something like (if newGame [craps object].getGameStatus() == PASS_WON), increment wins.

I do have a getter for a private variable "GameStatus" which is used to loop through the game. This serves as the current GameStatus and it's of the type Status from the enumeration that I have. I just can't use it in BetMoney properly.

Summary:

Right now, I have 2 classes. Craps class (which is used to play an individual game of Craps) and then a BetMoney (which will contain win/loss tracker) and money tracker.

Status is currently a private static enum in my Craps class, and I'm trying to use it in BetMoney.

//This is where my Status Enum is declared, these are all in Craps Class
private static enum Status {
        PASS_WON, PASS_LOST, KEEP_ROLLING, DP_WON, DP_LOST;
}; 

//Status variable for each single game
private static Status GameStatus;

//Getter for the single game status (there's also a setter)
public Status getGameStatus() {
    return GameStatus;
}

//Throughout Craps, I have a lot of code similar to this, where 
//getPoint gets the original "point" (first sum rolled) and for certain 

//sums, you win or lose the game, and GameStatus is set for use later.
switch (CrapsGame.getPoint()) {
        case 7:
        case 11:
            CrapsGame.setGameStatus(Status.PASS_WON); 
}

//How I keep looping:
while(CrapsGame.getGameStatus().equals(Status.KEEP_ROLLING)) { 
   ...logic to keep the game working...

}

//At the end, I return GameStatus.

Upvotes: 1

Views: 858

Answers (1)

Andy Thomas
Andy Thomas

Reputation: 86411

You've declared the enum class private. This prevents its use outside your Craps class.

private static enum Status {
    PASS_WON, PASS_LOST, KEEP_ROLLING, DP_WON, DP_LOST;
};

You'll need to make this something other than private. It is good practice to make each class as inaccessible as possible. But you know that you want to access it outside Craps. So, if Craps and BetMoney are in the same package, you could simply remove private to make the enum package-protected:

static enum Status {
    PASS_WON, PASS_LOST, KEEP_ROLLING, DP_WON, DP_LOST;
};

Inside BetMoney, you'll either need to import the constants you want to use, or qualify their use with their enum class name, as you've done in your example code (e.g., Status.PASS_WON).

Upvotes: 3

Related Questions