package ru.apetrov.Calculator;

/**
 * Created by Andrey on 05.01.2017.
 */
public class MenuCalculator {

    Input input;
    Calculator calculator;
    BaseActions[] actions = new BaseActions[6];
    boolean isClean = false;
    boolean isExit = false;
    double first;
    double second;
    String operation;

    public MenuCalculator(Input input, Calculator calculator) {
        this.input = input;
        this.calculator = calculator;
        fillAction();
    }

    public void fillAction() {
        actions[0] = new Help("help", "Help");
        actions[1] = new Add("+", "Addition");
        actions[2] = new Sub("-", "Subtraction");
        actions[3] = new Mult("*", "Multiplication");
        actions[4] = new Div("/", "Division");
        actions[5] = new CleanResult("clean", "Clean Result");
    }

    private void show() {
        for (BaseActions action : actions) {
            System.out.println(action.info());
        }
    }

    private void select(String key) {
        for (BaseActions action : actions) {
            if (key.equalsIgnoreCase(action.getKey())) {
                action.execute(this.calculator, this.input);
            }
        }
    }

    public void init() {
        show();
        isClean = false;
        while (!isExit) {
            this.first = this.input.askArgument("Введите число:");
            isClean = false;
            while (!isClean) {
                this.operation = this.input.ask("Введите операцию:");
                select(this.operation);
                this.first = calculator.getResult();
            }
            if (input.ask("Для выхода введите exit").equalsIgnoreCase("exit")) {
                isExit = true;
            }
        }
    }

    private class Help extends BaseActions {
        public Help(String key, String name) {
            super(key, name);
        }

        @Override
        public void execute(Calculator calculator, Input input) {

        }
    }

    private class Add extends BaseActions {

        public Add(String key, String name) {
            super(key, name);
        }

        @Override
        public void execute(Calculator calculator, Input input) {
            second = input.askArgument("Введите второй аргумент");
            calculator.add(first, second);
            System.out.printf("%s%n", calculator.getResult());
        }
    }

    private class Sub extends BaseActions {
        public Sub(String key, String name) {
            super(key, name);
        }

        @Override
        public void execute(Calculator calculator, Input input) {
            second = input.askArgument("Введите второй аргумент");
            calculator.sub(first, second);
            System.out.printf("%s%n", calculator.getResult());
        }
    }

    private class Mult extends BaseActions {
        public Mult(String key, String name) {
            super(key, name);
        }

        @Override
        public void execute(Calculator calculator, Input input) {
            second = input.askArgument("Введите второй аргумент");
            calculator.mult(first, second);
            System.out.printf("%s%n", calculator.getResult());
        }
    }

    private class Div extends BaseActions {
        public Div(String key, String name) {
            super(key, name);
        }

        @Override
        public void execute(Calculator calculator, Input input) {
            second = input.askArgument("Введите второй аргумент");
            calculator.div(first, second);
            System.out.printf("%s%n", calculator.getResult());
        }
    }

    private class CleanResult extends BaseActions {

        public CleanResult(String key, String name) {
            super(key, name);
        }

        @Override
        public void execute(Calculator calculator, Input input) {
            isClean = true;
        }
    }
}

@@ -192,9 +192,9 @@
 ons%5B
-6
+5
 %5D;%0A 
@@ -523,55 +523,8 @@
 0%5D =
- new Help(%22help%22, %22Help%22);%0A        actions%5B1%5D =
  new
@@ -562,17 +562,17 @@
 actions%5B
-2
+1
 %5D = new 
@@ -616,9 +616,9 @@
 ons%5B
-3
+2
 %5D = 
@@ -670,9 +670,9 @@
 ons%5B
-4
+3
 %5D = 
@@ -713,17 +713,17 @@
 actions%5B
-5
+4
 %5D = new 
@@ -1561,31 +1561,34 @@
 sk(%22
-%D0%94%D0%BB%D1%8F %D0%B2%D1%8B%D1%85%D0%BE%D0%B4%D0%B0 %D0%B2%D0%B2%D0%B5%D0%B4%D0%B8%D1%82%D0%B5 exit
+%D0%92%D1%8B%D0%B9%D1%82%D0%B8 %D0%B8%D0%B7 %D0%BF%D1%80%D0%BE%D0%B3%D1%80%D0%B0%D0%BC%D0%BC%D1%8B? (y/n):
 %22).e
@@ -1608,12 +1608,9 @@
 se(%22
-exit
+y
 %22)) 
@@ -1677,243 +1677,8 @@
  %7D%0A%0A
-    private class Help extends BaseActions %7B%0A        public Help(String key, String name) %7B%0A            super(key, name);%0A        %7D%0A%0A        @Override%0A        public void execute(Calculator calculator, Input input) %7B%0A%0A        %7D%0A    %7D%0A%0A
     

update IntractCalc