Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.Properties;
- public class Karoch {
- // Data Source interface and plain simple factory, console input by default
- static interface DataSource {
- static final String CONFIG_KEY = "ds";
- static final String TYPE_CONSOLE = "console";
- static final String TYPE_STATIC = "static";
- String readLine() throws IOException;
- }
- static DataSource getDataSource(Properties properties) throws Exception {
- switch (properties.getProperty(DataSource.CONFIG_KEY, DataSource.TYPE_CONSOLE)) {
- case DataSource.TYPE_CONSOLE:
- return () -> new BufferedReader(new InputStreamReader(System.in)).readLine();
- case DataSource.TYPE_STATIC:
- return () -> {
- System.out.println("0");
- return "0";
- };
- default:
- throw new Exception("Data Source type is unknown");
- }
- }
- // Data Validator with plain simple factory, only positive number are valid by default
- static interface Validator {
- static final String CONFIG_KEY = "validator";
- static final String TYPE_POSITIVE_NUMBER = "posNumber";
- boolean isValid(String s);
- }
- static Validator getValidator(Properties properties) {
- switch (properties.getProperty(Validator.CONFIG_KEY, Validator.TYPE_POSITIVE_NUMBER)) {
- case Validator.TYPE_POSITIVE_NUMBER:
- return (String s) -> {
- try {
- return Double.parseDouble(s) >= 0;
- } catch (NumberFormatException e) {
- return false;
- }
- };
- default:
- return (String s) -> true;
- }
- }
- static class Context {
- public Properties properties;
- public String data;
- }
- // States
- static interface State {
- State proceed(Context context) throws Exception;
- }
- static class GreetingsState implements State {
- @Override
- public State proceed(Context context) {
- System.out.println("Enter value for A:");
- return new InputState();
- }
- }
- static class InputState implements State {
- @Override
- public State proceed(Context context) throws Exception {
- DataSource dataSource = getDataSource(context.properties);
- context.data = dataSource.readLine();
- return new ValidateState();
- }
- }
- static class ValidateState implements State {
- @Override
- public State proceed(Context context) {
- return getValidator(context.properties).isValid(context.data) ? new FinishState() : new InvalidState();
- }
- }
- static class InvalidState implements State {
- @Override
- public State proceed(Context context) {
- System.out.println("Try again:");
- return new InputState();
- }
- }
- static class FinishState implements State {
- @Override
- public State proceed(Context context) {
- System.out.println("A is " + context.data);
- return null;
- }
- }
- static class ErrorState implements State {
- private final Exception exception;
- public ErrorState(Exception exception) {
- this.exception = exception;
- }
- @Override
- public State proceed(Context context) {
- System.out.println("Something went wrong:\n" + exception.getMessage());
- return null;
- }
- }
- public static void main(String[] args) {
- Context context = new Context();
- context.properties = new Properties(); // todo: create INI file with configuration, load it here
- // Run states machine
- State state = new GreetingsState();
- while (state != null) {
- try {
- state = state.proceed(context);
- } catch (Exception e) {
- state = new ErrorState(e);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement