Home > Enterprise >  Return enum into toString and accessor methods
Return enum into toString and accessor methods

Time:12-20

As the code showed below, I'm trying to make user input the speed (1,2,3) into the enum and return the input back into toString method.

private enum speed
{
    SMALL(1), 
    MEDIUM(2), 
    FAST(3);
    
    private int speedValue;
    private speed (int speedValue)
    {
        this.speedValue = speedValue;
    }

    public int getSpeed()
    {
        return speedValue;
    }

    public static Optional<speed> get(int speedValue)
    {
        return Arrays.stream(speed.values()) 
        .filter(spe -> spe.speedValue == speedValue)
        .findFirst();
    }

}

private boolean on;

The problem is when I put this.speed = speed or any other stuff, the speed class will be missing with error "speed cannot be resolved or is not a field"

This happened the same in the toString class.

public Fan(speed seed, boolean on)
{
    speed.get() = seed; //what shall i put here
    this.on = on;
}

public boolean getOn()
{
    return this.on;
}

public String toString()
{
    return speed; //what shall i put here
}

public static void main(String[] args) {
    
    Scanner sc = new Scanner(System.in);

    System.out.println("Please enter speed");
    int sp = sc.nextInt();
    System.out.println("On/Off");
    boolean on = sc.nextBoolean();

    Optional<speed>spe = speed.get(sp); //getting enum integer values
    System.out.println(spe.get());

    Fan fan = new Fan(sp, on)

Is there any solution that I would be able to return the integer value of enum into the public class and toString class?

CodePudding user response:

With private enum speed {...} you declare the enum type speed, but you never declare a field of this type.

If you want to have a field named speed of this enum type you must declare it with private speed speed;.

This looks confusing and therefore I suggest that you follow the Java naming conventions where names of classes start with an uppercase letter (and enum types are classes).

That means your enum type should be written as

    public enum Speed {
        SMALL(1), 
        MEDIUM(2), 
        FAST(3);
    
        private int speedValue;
        private Speed (int speedValue) {
            this.speedValue = speedValue;
        }

        public int getSpeed() {
            return speedValue;
        }

        public static Optional<Speed> get(int speedValue) {
            return Arrays.stream(Speed.values()) 
            .filter(spe -> spe.speedValue == speedValue)
            .findFirst();
        }
    }

Your Fan class needs these fields:

    private boolean on;
    private Speed speed;

The constructor is

    public Fan(Speed seed, boolean on) {
        speed = seed;
        this.on = on;
    }

or, assuming that the parameter name seed is a spelling mistake and it should be speed instead:

    public Fan(Speed speed, boolean on) {
        this.speed = speed;
        this.on = on;
    }

The other methods:

    public boolean getOn() {
        return this.on;
    }

    public String toString() {
        return speed.toString();
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.println("Please enter speed");
        int sp = sc.nextInt();
        System.out.println("On/Off");
        boolean on = sc.nextBoolean();

        Optional<Speed> spe = Speed.get(sp); //getting enum integer values
        System.out.println(spe.get());

        Fan fan = new Fan(spe.get(), on);
        // note that the above line produces not output. Why should it?
        // if you want to see the result of f.toString() you need to print it out:
        System.out.println(fan.toString());
        // or shorter (since println() calls toString() automatically):
        System.out.println(fan);
    }

Note 1: I also changed the placement of the opening braces ({) to follow general Java conventions - for seasoned Java programmers this looks less surprising.

Note 2: as Mark Rotteveel correctly remarks: the Fan class has a public constructor and therefore the Speed enum should also be declared public. Otherwise no one outside of the Fan class will be able to construct a new Fan object.

  • Related