Task: Finite state machine

C: #include <stdio.h>
#include <ctype.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
  typedef enum State { READY, WAITING, REFUND, DISPENSE, COLLECT, QUIT } State;

  typedef struct statechange {
    const int in;
    const State out;
  } statechange;

#define MAXINPUTS 3
  typedef struct FSM {
    const State state;
    void (*Action)(void);
    const statechange table[MAXINPUTS]; 
  } FSM;

  char str[10];
  void Ready(void)    { fprintf(stderr, "\nMachine is READY. (D)eposit or (Q)uit :"); scanf("%s", str); }
  void Waiting(void)  { fprintf(stderr, "(S)elect product or choose to (R)efund :"); scanf("%s", str); }
  void Refund(void)   { fprintf(stderr, "Please collect refund.\n"); }
  void Dispense(void) { fprintf(stderr, "Dispensing product...\n"); }
  void Collect(void)  { fprintf(stderr, "Please (C)ollect product. :"); scanf("%s", str); }
  void Quit(void)     { fprintf(stderr, "Thank you, shutting down now.\n"); exit(0); }

  const FSM fsm[] = {
    { READY,    &Ready,    {{'D', WAITING},  {'Q', QUIT },    {-1, READY}    }},
    { WAITING,  &Waiting,  {{'S', DISPENSE}, {'R', REFUND},   {-1, WAITING}  }},
    { REFUND,   &Refund,   {{ -1, READY}                                     }},
    { DISPENSE, &Dispense, {{ -1, COLLECT}                                   }},
    { COLLECT,  &Collect,  {{'C', READY},    { -1, COLLECT }                 }},
    { QUIT,     &Quit,     {{ -1, QUIT}                                      }},
  };

  int each;
  State state = READY;

  for (;;) {
    fsm[state].Action();
    each = 0;
    while (!( ((fsm[state].table[each].in == -1)
               
               || (isalpha(str[0]) && fsm[state].table[each].in == toupper(str[0]) )))) each++;
    state = fsm[state].table[each].out;
  }
 
  return 0;
}





Java: import java.util.*;

public class FiniteStateMachine {

    private enum State {
        Ready(true, "Deposit", "Quit"),
        Waiting(true, "Select", "Refund"),
        Dispensing(true, "Remove"),
        Refunding(false, "Refunding"),
        Exiting(false, "Quiting");

        State(boolean exp, String... in) {
            inputs = Arrays.asList(in);
            explicit = exp;
        }

        State nextState(String input, State current) {
            if (inputs.contains(input)) {
                return map.getOrDefault(input, current);
            }
            return current;
        }

        final List<String> inputs;
        final static Map<String, State> map = new HashMap<>();
        final boolean explicit;

        static {
            map.put("Deposit", State.Waiting);
            map.put("Quit", State.Exiting);
            map.put("Select", State.Dispensing);
            map.put("Refund", State.Refunding);
            map.put("Remove", State.Ready);
            map.put("Refunding", State.Ready);
        }
    }

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

        while (state != State.Exiting) {
            System.out.println(state.inputs);
            if (state.explicit){
                System.out.print("> ");
                state = state.nextState(sc.nextLine().trim(), state);
            } else {
                state = state.nextState(state.inputs.get(0), state);
            }
        }
    }
}