Как вызывать методы enum с динамическими значениями в Java - PullRequest
0 голосов
/ 30 июня 2018

Я работаю над одной программой, мне нужно найти положение робота в сетке, он может двигаться в прямом направлении и может менять свое направление на север, юг, восток и запад и получил заданную команду последовательность. Итак, какой будет конечная позиция робота. Использование любого типа ветвления условия (например, if / else, switch / case) запрещено.

ПРИМЕР-
ГРИД (100 * 500)
Начальная позиция робота - (5,3)
Возможные команды -
N-Север,
E-East,
W-Запад,
S-Юг,
M-движение вперед
Пример ввода - {N, S, M.M, E, W, E, S, M, S, M}

Я пытался использовать Enum, но проблема, с которой я сталкиваюсь, заключается в том, как вызывать методы Enum с динамическими значениями, которые я получаю с помощью команд.

public class RobotMovesInGrid {
    Scanner input = new Scanner(System.in);
    String command=input.next();
    int commLength = command.length();

    static enum Command {
        N{@Override public void execute(String g, String r){ System.out.println("do the N move here"); }},
        E{@Override public void execute(String g, String r){ System.out.println("do the E move here"); }},
        S{@Override public void execute(String g, String r){ System.out.println("do the S move here"); }},
        W{@Override public void execute(String g, String r){ System.out.println("do the W move here"); }},
        M{@Override public void execute(String g, String r){ System.out.println("do the M move here"); }};
        public abstract void execute(String g, String r);

    }
    public void nextPosition() {
        Command c1;
        for(int i=0;i<commLength;i++) {
            if (command.charAt(i)=='N'||command.charAt(i)=='E'|| command.charAt(i)=='S'|| command.charAt(i)=='W'||command.charAt(i)=='M')

                c1= Command.M;// Here instead of M, I am trying to give dynamic commands but it is not taking it
            System.out.println("Current position is"+c1);
        }
    }
}

Может, кто-нибудь подскажет, как вызывать методы Enum, используя команды, заданные в качестве ввода.

Ответы [ 4 ]

0 голосов
/ 01 июля 2018

Один из способов сделать это - с помощью константы, статической Map внутри внутреннего класса enum Commands; он связывает String с Commands. Command.parse ищет команду на карте.

import java.lang.String;
import java.util.Map;
import java.util.HashMap;
import java.util.Collections;
import java.util.function.Function;
import java.awt.Point;
import java.util.Scanner;

class Robot {

    public static void main(String args[]) {
        try(Scanner input = new Scanner(System.in)) {
            input.useDelimiter(",\\s*|\n");
            Robot r = new Robot();
            while(input.hasNext()) {
                try {
                    Command.parse(input.next().trim()).apply(r);
                } catch(NullPointerException e) {
                    System.out.printf("Syntax error.\n");
                } catch(RuntimeException e) {
                    System.out.printf("Can't go that way: %s.\n",
                        e.getMessage());
                }
            }
        } catch(Exception e) {
            System.err.printf("%s: %s.\n", e, e.getMessage());
        }
    }

    Point x;
    static Point dim;
    Command last;

    Robot() {
        dim = new Point(100, 500);
        x = new Point(5, 3);
    }

    enum Command {
        N("N", "north",  true,  (r) -> new Point(r.x.x, r.x.y - 1)),
        E("E", "east",   true,  (r) -> new Point(r.x.x + 1, r.x.y)),
        S("S", "south",  true,  (r) -> new Point(r.x.x, r.x.y + 1)),
        W("W", "west",   true,  (r) -> new Point(r.x.x - 1, r.x.y)),
        M("M", "farther",false, (r) -> r.last != null ?r.last.go.apply(r):null);

        private String command, name;
        private boolean isDir;
        private Function<Robot, Point> go;
        private static final Map<String, Command> map;

        /* Map for turning commands into Directions; mod->const map. */
        static {
            Map<String, Command> mod = new HashMap<>();
            for(Command c : values()) mod.put(c.command, c);
            map = Collections.unmodifiableMap(mod);
        }

        /** Called from definition of enum. */
        private Command(final String command, final String name, boolean isDir,
            final Function<Robot, Point> go) {
            this.command = command;
            this.name    = name;
            this.isDir   = isDir;
            this.go      = go;
        }

        /** @param str A string representing the direction.
         @return The command or null. */
        public static Command parse(final String str) { return map.get(str); }

        /** Applies this command to r. */
        public void apply(Robot r) {
            Point x = this.go.apply(r);
            if(x == null)
                throw new RuntimeException("don't have a direction");
            if(x.x < 0 || x.x >= dim.x || x.y < 0 || x.y >= dim.y)
                throw new RuntimeException("at the edge");
            r.x = x;
            if(this.isDir == true) r.last = this;
            System.out.printf("Went %s to (%d, %d).\n", r.last.name, x.x, x.y);
        }

        /** @return The name of the direction. */
        public String toString() { return name; }
    }

}

Это заканчивается на EOF. Я нашел это полезным,

0 голосов
/ 30 июня 2018

Вы можете использовать Command.valueOf().

Как

c1 = Command.valueOf(command.toUpperCase());

Это установит в c1 значение перечисления Command, соответствующее введенной команде.

toUpperCase() удостоверяется, что оно совпадает с именем enum.

0 голосов
/ 30 июня 2018

Вот еще одно решение.

    static enum Command {
        N(-1,0),E(0,1),W(0,-1),S(1,0);
        private int rowIncrement;
        private int colIncrement;

        private Command(int rowIncrement, int colIncrement)
        {
            this.rowIncrement = rowIncrement;
            this.colIncrement = colIncrement;
        }

        public int getRowIncrement()
        {
            return rowIncrement;
        }

        public int getColIncrement()
        {
            return colIncrement;
        }
    }

и вот код для оценки команды.

    //input
    String command = "NSMMEWESMSM";
    int[] pos = new int[]{5,3};
    int[] size = new int[]{100, 500};

    char[] chars = command.toCharArray();
    //replace M by the previous char, ie move in the same direction
    for (int i = 0; i < chars.length; i++)
    {
        char dir = chars[i];
        //this assumes that M cannot be the first char
        if (dir == 'M')
        {
            dir = chars[i-1];
            chars[i] = dir;
        }
    }


    for (char dir : chars)
    {
        Command cmd = Command.valueOf(String.valueOf(dir));
        pos[0] += cmd.rowIncrement;
        //row is within the region
        pos[0] = Math.min(Math.max(pos[0], 0), size[0] -1);

        pos[1] += cmd.colIncrement;
        //col is within the region
        pos[1] = Math.min(Math.max(pos[1], 0), size[1] -1);
    }
0 голосов
/ 30 июня 2018

Простейшим способом будет преобразование ввода char в String, а затем вызовет метод перечисления valueOf(String) для получения перечисления, например:

for(int i=0;i<commLength;i++) {
   Command command = Command.valueOf(String.valueOf(command.charAt(i))
                                           .toUpperCase());
   if (command != null){
       command.execute(...);
   }
}

Это будет работать, но я думаю, что у ваших значений перечисления действительно бессмысленные имена.
Вам следует переименовать их в соответствии с их значением, обогатить класс enum конструктором, который содержит символ отображения, и ввести статический метод для получения значения enum, связанного с входным символом:

static enum Command {
    NORTH('N'){@Override public void execute(String g, String r){ System.out.println("do the N move here"); }},
    EAST('E'){@Override public void execute(String g, String r){ System.out.println("do the E move here"); }},
    SOUTH('S'){@Override public void execute(String g, String r){ System.out.println("do the S move here"); }},
    WEST('W'){@Override public void execute(String g, String r){ System.out.println("do the W move here"); }},
    MOVE_FORWARD('M'){@Override public void execute(String g, String r){ System.out.println("do the M move here"); }};

    private char mappingChar;

    Command (char mappingChar){
        this.mappingChar = mappingChar;
    }

    public abstract void execute(String g, String r);

    public static Optional<Command> getFrom(char mappingChar) {
        for (Command command : values()) {
            if (Character.toUpperCase(mappingChar)==command.mappingChar) {
                return Optional.of(command);
            }
        }
        return Optional.empty();
    }
}

Теперь вы можете динамически получать значение enum и использовать его:

for(int i=0;i<commLength;i++) {
    Optional<Command> optCommand = Command.getFrom(command.charAt(i)); 
    if (optCommand.isPresent()){
        optCommand.get().execute(...)
    }

    // or alternatively 
    optCommand.ifPresent(c -> c.execute(...));        
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...