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