0

So I've looked at a few examples on the site and haven't found anything needed. The exercise I'm trying to do is to link the enum class to the switch statement found at the bottom of the page.

The exercise question for this small part is: "The talk method should ask the user to input a phrase, which should be printed on the console. An Entertainment Robot can walk any number of paces in one of four directions (forwards, backwards, left, right). Therefore the walk method should ask the user how many paces they wish the robot to take and in which direction. Note you must validate the user’s input. The directions must be stored as an enum and accessed appropriately."

Enum class:

package robot;

public enum Directions {
    FORWARD, BACKWARD, LEFT, RIGHT;
}

Main class:

package robot;

import java.util.Scanner;

public class EntertainmentRobot extends Robot implements Walk, Talkable {

    private Directions direction;

    public EntertainmentRobot(double height, double weight, String manufacturer, String name) {
        super(height, weight, manufacturer, name);
        setEnergy(10);
        setEnergyUnitsRequired(0.75);
    }

    // ACCESSOR METHOD
    public Directions getDirection() {
        return direction;
    }

    // MUTATOR METHOD
    public void setDirection(Directions direction) {
        this.direction = direction;
    }

    @Override
    public void start() {
        System.out.println("-----I AM THE ENTERAINIMENT ROBOT-----");
    }

    @Override
    public void stop() {
        System.out.println("I have stopped entertaining people.");
    }

    @Override
    public void doTask() {
        // TODO Auto-generated method stub
    }

    @Override
    public void doTask(Scanner in) {
        System.out.println("I am entertaining people.");
        System.out.println("How many people would you like to play with me?");
        int times = in.nextInt();
        for (int i = 0; i < times; i++) {
            play();
        }
        System.out.println(" ");
    }

    public String toString() {
        return "[Name= " + getName() + "\nWeight= " + getWeight() + "\nHeight:" + getHeight() + "\nManufacturer: "
            + getManufacturer() + "\nPurpose: " + getPurpose() + "]";
    }

    public void play() {
        if (getEnergy() >= energyRequired()) {
            energyComsumption();
        } else {
            System.out.println("\n-----WHOOPS-----");
            System.out.println("I do not have enough energy to study");
            regenerate();
            System.out.println("I must get more energy");
            System.out.println("I have regenerated my energy");
        }
    }

    @Override
    public void talk(Scanner in) {
        System.out.println("Please enter a phrase for me to speak: ");
        String talk = in.nextLine();
        System.out.println("You asked me to say the following phrase: " + talk);
    }

    @Override
    public void walk(Scanner in) {
        System.out.println("Would you like me to walk? [YES/NO]");
        String walk = in.nextLine();
        if (walk.equalsIgnoreCase("Yes")) {
        }
    }

    public void directionChoice(Scanner in) {
        System.out.println("For how many paces?");
        int paces = in.nextInt();
        System.out.println("1 - FORWARD" + "2 - BACKWARD" + "3 - LEFT" + "4 - RIGHT");
        switch (paces) {
        case 1: 
            break;
        case 2:
        }
    }
}
António Ribeiro
  • 4,129
  • 5
  • 32
  • 49
  • First, after you retrieved the paces, you need to query the user for the direction in which the robot should go. Only after that, you should validate the user's input and then match it against your `enum`. What you're doing is validating the amount of paces against the direction, which doesn't seem to be what you want. – António Ribeiro Mar 01 '16 at 23:12
  • Possible duplicate of [How do I convert String to enum for at switch statement?](http://stackoverflow.com/questions/5783061/how-do-i-convert-string-to-enum-for-at-switch-statement) – PM 77-1 Mar 01 '16 at 23:13
  • @aribeiro Yes, so how would I do this? Cheers Darshen for your example. –  Mar 01 '16 at 23:17
  • @JudgeDredd, check my answer. – António Ribeiro Mar 01 '16 at 23:42

2 Answers2

1

Below is an example of how to use Direction enum in switch case:

Direction direction = //get direction input
switch(direction){
case BACKWARD:
    //do something
    break;

case FORWARD:
    //do something
    break;

case LEFT:
    //do something
    break;

case RIGHT:
    //do something
    break;
}

However, before we execute this switch, we need to get the value of this enum, I guess it would be user input (String). We can use valueOf() method of enum to get the actual value. Please note that valueOf() throws IllegalArgumentException if input is not one of the values, so we can do the following to validate users input:

String input = //get user's input
Direction direction;
try{
    direction = Direction.valueOf(input);
}catch(IllegalArgumentException iae){
    //Invalid input
}
Darshan Mehta
  • 30,102
  • 11
  • 68
  • 102
  • This is ok, but please note that it's not generally considered a good idea to take a string from user input and convert it to an enum using valueOf. It's not internationalizable, for a start. Also, it's too easy for the developer to change the enum names and think it won't affect the behaviour of the program, when in fact it will. You can find more advice on this from e.g. Bloch's "Effective Java" book. However, you might decide that for your use case it doesn't matter. – Klitos Kyriacou Mar 01 '16 at 23:42
1

Try the following:

public enum Direction {
    FORWARD, BACKWARD, LEFT, RIGHT;

    public static Direction getDirection(String direction) {
        for (Direction dir : values()) {
            if (dir.name().equalsIgnoreCase(direction)) {
                return dir;
            }
        }
        return null;
    }
}

...

public void directionChoice(Scanner in) {
    System.out.println("For how many paces?");
    int paces = Integer.parseInt(in.nextLine()); // To avoid doing nextInt and then nextLine
    System.out.println("In which direction: FORWARD, BACKWARD, LEFT or RIGHT?");
    Direction direction = Direction.getDirection(in.nextLine());

    while(direction == null) {
        System.out.println("Invalid direction!");
        System.out.println("In which direction: FORWARD, BACKWARD, LEFT or RIGHT?");
        direction = Direction.getDirection(in.nextLine());
    }

    switch (direction) {
    case FORWARD:
        // do stuff
        break;
    case BACKWARD:
         // do stuff
        break;
    case LEFT:
        // do stuff
        break;
    case RIGHT:
        // do stuff
        break;
    default:
        // do stuff (or not)
        break;
    }
}
António Ribeiro
  • 4,129
  • 5
  • 32
  • 49