private void Add(UserConsole console, StateKeeper stateKeeper)
        {
            stateKeeper.ArgsCount = 2;
            var args = stateKeeper.GetArgs();
            var a    = args[0] == null?int.Parse(console.ReadLine().Trim(), Culture) : int.Parse(args[0], Culture);

            stateKeeper.Args[0] = a.ToString(Culture);
            stateKeeper.SaveState();
            var b = args[1] == null?int.Parse(console.ReadLine().Trim(), Culture) : int.Parse(args[1], Culture);

            stateKeeper.Args[1] = b.ToString(Culture);
            stateKeeper.SaveState();
            console.WriteLine((a + b).ToString(Culture));
        }
        public override void Run(UserConsole userConsole, Storage storage)
        {
            while (true)
            {
                var input = userConsole.ReadLine();
                switch (input.Trim())
                {
                case "exit":
                    return;

                case "add":
                    Add(userConsole);
                    break;

                case "median":
                    Median(userConsole);
                    break;

                case "help":
                    Help(userConsole);
                    break;

                case "rand":
                    x = Random(userConsole, x);
                    break;

                default:
                    userConsole.WriteLine("Такой команды нет, используйте help для списка команд");
                    break;
                }
            }
        }
        private void Median(UserConsole console, StateKeeper stateKeeper)
        {
            stateKeeper.ArgsCount = stateKeeper.ArgsCount == -1
                ? int.Parse(console.ReadLine().Trim(), Culture)
                : stateKeeper.ArgsCount;
            stateKeeper.SaveState();
            var args = stateKeeper.GetArgs();

            for (var i = 0; i < stateKeeper.ArgsCount; i++)
            {
                args[i] = args[i] ?? console.ReadLine().Trim();
                stateKeeper.SaveState();
            }

            var result = CalculateMedian(args.Select(x => int.Parse(x, Culture)).ToList());

            console.WriteLine(result.ToString(Culture));
        }
        public override void Run(UserConsole userConsole, Storage storage)
        {
            var x          = 420L;
            var storedData = storage.Read();

            if (storedData.Length != 0)
            {
                var binaryReader = new BinaryReader(new MemoryStream(storedData));
                var typeByte     = binaryReader.ReadByte();
                var type         = (CommandType)typeByte;
                var command      = Load(userConsole, storage, type);

                if (!command.IsComplete)
                {
                    command.Run();
                }
                x = command.LastRandValue;
            }
            while (true)
            {
                Command command;
                var     input = userConsole.ReadLine();
                switch (input.Trim())
                {
                case "exit":
                    storage.Write(Array.Empty <byte>());
                    return;

                case "add":
                    command = new AddCommand(userConsole, storage, x);
                    break;

                case "median":
                    command = new MedianCommand(userConsole, storage, x);
                    break;

                case "help":
                    command = new HelpCommand(userConsole, storage, x);
                    break;

                case "rand":
                    command = new RandomCommand(userConsole, storage, x);
                    break;

                default:
                    command = new NotFoundCommand(userConsole, storage, x);
                    break;
                }
                command.Run();
                x = command.LastRandValue;
            }
        }
        private string TakeNewLine(UserConsole console)
        {
            string input;

            if (commandsLines.Count > 0)
            {
                input = commandsLines.Dequeue();
            }
            else
            {
                input = console.ReadLine().Trim();
                AddToData(input);
            }

            return(input);
        }
            public override string ReadLine()
            {
                string readLine;

                if (_unusedStorageLines.Count != 0)
                {
                    readLine = _unusedStorageLines.Dequeue();
                }
                else
                {
                    readLine = _console.ReadLine();
                    AddToStorage(readLine);
                }

                return(readLine);
            }
        private void Random(UserConsole console, StateKeeper stateKeeper)
        {
            const int a = 16807;
            const int m = 2147483647;

            stateKeeper.ResultsCount = stateKeeper.ResultsCount == 0
                ? int.Parse(console.ReadLine().Trim(), Culture)
                : stateKeeper.ResultsCount;
            stateKeeper.SaveState();
            for (var i = stateKeeper.ShowedResults; i < stateKeeper.ResultsCount; i++)
            {
                console.WriteLine(stateKeeper.x.ToString(Culture));
                stateKeeper.x             = a * stateKeeper.x % m;
                stateKeeper.ShowedResults = i + 1;
                stateKeeper.SaveState();
            }
        }
        private static void Help(UserConsole console)
        {
            const string exitMessage = "Чтобы выйти из режима помощи введите end";
            const string commands    = "Доступные команды: add, median, rand";

            console.WriteLine("Укажите команду, для которой хотите посмотреть помощь");
            console.WriteLine(commands);
            console.WriteLine(exitMessage);
            while (true)
            {
                var command = console.ReadLine();
                switch (command.Trim())
                {
                case "end":
                    return;

                case "add":
                    console.WriteLine("Вычисляет сумму двух чисел");
                    console.WriteLine(exitMessage);
                    break;

                case "median":
                    console.WriteLine("Вычисляет медиану списка чисел");
                    console.WriteLine(exitMessage);
                    break;

                case "rand":
                    console.WriteLine("Генерирует список случайных чисел");
                    console.WriteLine(exitMessage);
                    break;

                default:
                    console.WriteLine("Такой команды нет");
                    console.WriteLine(commands);
                    console.WriteLine(exitMessage);
                    break;
                }
            }
        }
        public override void Run(UserConsole userConsole, Storage storage)
        {
            var stateKeeper = new StateKeeper(storage, Culture);

            while (true)
            {
                stateKeeper.CurrentCommand ??= userConsole.ReadLine().Trim();
                stateKeeper.SaveState();
                switch (stateKeeper.CurrentCommand)
                {
                case "exit":
                    stateKeeper.Dispose();
                    return;

                case "add":
                    Add(userConsole, stateKeeper);
                    break;

                case "median":
                    Median(userConsole, stateKeeper);
                    break;

                case "help":
                    Help(userConsole, stateKeeper);
                    break;

                case "rand":
                    Random(userConsole, stateKeeper);
                    break;

                default:
                    userConsole.WriteLine("Такой команды нет, используйте help для списка команд");
                    break;
                }

                stateKeeper.Clear();
                stateKeeper.SaveState();
            }
        }
        public override string ReadLine()
        {
            string line;

            if (lastInputCommand < data.lastInputCommand) //нужно эмулировать считивание
            {
                line = data.inputCommands[lastInputCommand];
                lastInputCommand++;
            }
            else
            {
                line = userConsole.ReadLine();

                data.inputCommands.Add(line);

                // уже ничего плохого не произойдет
                data.lastInputCommand++;
                lastInputCommand++;
                Serialize(data);
            }
            return(line);
        }
 protected int ReadNumber()
 {
     return(int.Parse(Console.ReadLine(), Culture));
 }
 private int ReadNumber(UserConsole console)
 {
     return(int.Parse(console.ReadLine().Trim(), Culture));
 }
        private void Help(UserConsole console, StateKeeper stateKeeper)
        {
            const string exitMessage = "Чтобы выйти из режима помощи введите end";
            const string commands    = "Доступные команды: add, median, rand";

            stateKeeper.ArgsCount = 1;
            if (stateKeeper.ResultsCount == 0)
            {
                var introText = new List <string>
                {
                    "Укажите команду, для которой хотите посмотреть помощь", commands, exitMessage
                };
                PrintHelp(console, stateKeeper, introText);
                stateKeeper.ResultsCount  = 1;
                stateKeeper.ShowedResults = 0;
                stateKeeper.SaveState();
            }

            while (true)
            {
                var command = stateKeeper.GetArgs()[0] == null || stateKeeper.GetArgs()[0].Equals("")
                    ? console.ReadLine().Trim()
                    : stateKeeper.GetArgs()[0];
                stateKeeper.Args[0] = command;
                stateKeeper.SaveState();
                switch (command)
                {
                case "end":
                    return;

                case "add":
                    var addText = new List <string> {
                        "Вычисляет сумму двух чисел", exitMessage
                    };
                    PrintHelp(console, stateKeeper, addText);
                    break;

                case "median":
                    var medianText = new List <string> {
                        "Вычисляет медиану списка чисел", exitMessage
                    };
                    PrintHelp(console, stateKeeper, medianText);
                    break;

                case "rand":
                    var randText = new List <string> {
                        "Генерирует список случайных чисел", exitMessage
                    };
                    PrintHelp(console, stateKeeper, randText);
                    break;

                default:
                    var defaultText = new List <string> {
                        "Такой команды нет", commands, exitMessage
                    };
                    PrintHelp(console, stateKeeper, defaultText);
                    break;
                }

                stateKeeper.Args[0]       = null;
                stateKeeper.ShowedResults = 0;
            }
        }