Yuval Levy
Yuval Levy

Reputation: 2516

enum in separate class in java

I am trying to write two classes:

one for a robot that will instance with unique ID and direction. and another class for direction that will use enum for the directions for a robot. I tried to write it in the following way but I think i am missing something...

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(int dir){
        this.direction = new Direction(dir);
        this.RoboID = IDGenerator;
        IDGenerator++;
    }
}

the class for the enum:

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

Upvotes: 0

Views: 387

Answers (4)

Duncan Jones
Duncan Jones

Reputation: 69329

Assuming you don't want to use a Direction parameter in your Robot constructor, then the classic solution is to provide a static method in your enum to return the right value for a given input:

public enum Direction {
    //... code omitted

    public static Direction fromInt(int direction) {
        for (Direction d : values()) {
            if (direction == d.getDirection()) {
                return d;
            }
        }
        throw new NoSuchElementException(Integer.toString(direction));
    }
}

Then use this in your constructor:

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

Upvotes: 2

Gaurav Gupta
Gaurav Gupta

Reputation: 4691

try something like

package SO;

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

    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return "Direction: " + direction;
    }

    public static void main(String[] args) {
        Robot r = new Robot(Direction.UP);
        System.out.println(r);
    }
}

Pass direction while creating object of Robot.

Apart from that i personally would have used Builder pattern here, something like following

package SO;

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

    public Robot setDirection(Direction dir){
        this.direction = dir;
        return this;
    }

    public Robot setId(int id){
        this.RoboID = id;
        return this;
    }
    // Constructor for Robot

    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return "Direction: " + direction;
    }

    public static void main(String[] args) {
        Robot r = new Robot();
        r = r.setDirection(Direction.UP).setId(212);
        System.out.println(r);
    }
}

Upvotes: 0

Ray
Ray

Reputation: 3201

Enums are not instantiated via new; instead, they have a defined set of instances at compile time. Just access the instances directly like this:

public Robot(Direction dir) {
    this.direction = dir;
}

You call this constructor e.g. like this:

Robot bot = new Robot(Direction.UP);

Upvotes: 3

nils
nils

Reputation: 1382

You are calling this.direction = new Direction(dir); although the constructor is private. Provide a (static) method in the enum to convert the integer into the correct direction.

Edit: Or use

public Robot(Direction direction) {
...

instead.

Upvotes: 0

Related Questions