0

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"?

1
  • 1
    your turnLeft method seems odd. Do you want it to turn to some globally defined "left" rather than left relative to itself Commented Apr 29, 2014 at 11:02

3 Answers 3

3

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;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

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;
}

Comments

0

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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.