Yuval Levy
Yuval Levy

Reputation: 2516

enum integer and names

can i approach a enum by values or only by names? for example i have a robot that initialize with direction "UP" then i want to change his direction, turning left but it will be much more convenient to deal with the numbers representing the direction instead of the Strings "UP RIGHT DOWN LEFT"...

enum class:

package Q1;

public enum Direction{
    UP(1), RIGHT(2), DOWN(3), LEFT(4);
    private final int dir;

    //constructor for Direction enum for a robot 
    private Direction(int dir){
        this.dir = dir;
    }

    //return facing direction of a robot
    public int getDirection(){
        return this.dir;
    }

    //return the direction for represented number
    public Direction getDirFromNumber(){
        return this.dir;
    }
}

Robot class:

package Q1;

public class Robot {
    static int IDGenerator = 1000;  //ID generator for class Robot
    int RoboID;                     //The ID of the robot
    Direction direction;            //The Direction the robot is facing

    //Constructor for Robot
    public Robot(Direction dir){
        this.direction = dir;
        this.RoboID = IDGenerator;
        IDGenerator++;
    }

    //Turning the robot left
    public void turnLeft(){
        if (direction.getDirection()==1)
            this.direction = Direction.LEFT;
        else this.direction
    }
}

my question is, how can I approach Direction one time by the number representing in enum and the other time with "UP RIGHT DOWN LEFT"?

Upvotes: 0

Views: 232

Answers (3)

reap
reap

Reputation: 202

To get the enum value of certain number, you have to implement the method for it yourself. For example loop through the enum-values and pick the one with correct number. For example

//return the direction for represented number
public static Direction getDirFromNumber(int direction){
    for (Direction dir : Direction.values()) {
        if (dir.getDirection() == direction) {
            return dir;
        }
    }
    return null;
}

Upvotes: 0

Hirak
Hirak

Reputation: 3649

Sample usage

 //Turning the robot left
    public void turnLeft(){
       this.direction = Direction.getDirFromNumber(1);
    }

Modified enum method to suit this need is

public static Direction getDirFromNumber(int n){
    Direction[] dir = Direction.values();
    for(int i=0;i<dir.length;i++){
        if(dir[i].getDirection() == n){
            return dir[i];
        }
    }
    return null;
}

Upvotes: 0

Thilo
Thilo

Reputation: 262504

You are free to implement such a method.

In your case, something like

public static Direction getDirection(int number){
    return Direction.values()[number-1];
}

But why is this more convenient? The whole point of enums is to avoid magic numbers (or strings).

I think what you really want is a method on Direction that returns the Direction when you turn left or right from it.

public Direction turnLeft(){
    switch(this){
       case LEFT: return DOWN;
       case DOWN: return RIGHT;
       case RIGHT: return UP;
       case UP: return LEFT;
    }
}

Upvotes: 3

Related Questions