protected Command(UserConsole console, Storage storage, long x)
 {
     Console       = console;
     Storage       = storage;
     Schedule      = new List <Action>();
     LastRandValue = x;
 }
        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;
                }
            }
        }
            public ConsoleWithStorage(UserConsole console, Storage storage)
            {
                _console      = console;
                _storage      = storage;
                _storageBytes = _storage.Read();

                var storageLines = UTF8.GetString(_storageBytes)
                                   .Split(new[] { StorageLinesSeparator }, StringSplitOptions.RemoveEmptyEntries);

                _unusedStorageLines = new Queue <string>(storageLines);

                var isStorageEmpty = _storageBytes == null || _storageBytes.Length == 0;

                if (isStorageEmpty)
                {
                    const long firstRandomValue = 420L;
                    AddToStorage(firstRandomValue);
                    _nextRandomValue = firstRandomValue;
                }
                else
                {
                    var nextRandomValue = _unusedStorageLines.Dequeue();
                    _nextRandomValue = System.Convert.ToInt64(nextRandomValue);
                }
            }
        public static HelpCommand Load(UserConsole console, Storage storage)
        {
            var mem          = new MemoryStream(storage.Read());
            var schedule     = new List <Action>();
            var helpMessages = new List <HelpMessage>();

            using var binReader = new BinaryReader(mem);

            binReader.ReadByte();
            var x     = binReader.ReadInt64();
            var count = binReader.ReadInt32();

            for (var i = 0; i < count; i++)
            {
                schedule.Add((Action)binReader.ReadByte());
            }

            count = binReader.ReadInt32();
            for (var i = 0; i < count; i++)
            {
                var helpMsg = new HelpMessage((Action)binReader.ReadByte(), binReader.ReadString());
                helpMessages.Add(helpMsg);
            }

            var command = new HelpCommand(console, storage, x)
            {
                _helpMessages = helpMessages,
                Schedule      = schedule
            };

            command.Save();
            return(command);
        }
        public NotFoundCommand(UserConsole console, Storage storage, long x) : base(console, storage, x)
        {
            Schedule.Add(Action.Write);
            Type = CommandType.NotFound;

            Save();
        }
        private bool ExecuteHelp(UserConsole console, string command)
        {
            const string exitMessage = "Чтобы выйти из режима помощи введите end";
            const string commands    = "Доступные команды: add, median, rand";

            switch (command)
            {
            case "end":
                return(true);

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

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

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

            default:
                WriteInConsole(console, "Такой команды нет");
                WriteInConsole(console, commands);
                WriteInConsole(console, exitMessage);
                break;
            }

            return(false);
        }
        private void Add(UserConsole console)
        {
            var a = ReadNumber(console);
            var b = ReadNumber(console);

            WriteInConsole(console, (a + b).ToString(Culture));
        }
        private void Add(UserConsole console)
        {
            var a = ReadNumber(console);
            var b = ReadNumber(console);

            console.WriteLine((a + b).ToString(Culture));
        }
        private bool ExecuteLine(UserConsole userConsole, string input)
        {
            switch (input)
            {
            case "exit":
                ClearAllData();
                return(true);

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

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

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

            case "rand":
                Random(userConsole);
                UpgrageX();
                break;

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

            return(false);
        }
        public static NotFoundCommand Load(UserConsole console, Storage storage)
        {
            var mem       = new MemoryStream(storage.Read());
            var schedule  = new List <Action>();
            var variables = new List <int>();

            using var binReader = new BinaryReader(mem);

            binReader.ReadByte();
            var x     = binReader.ReadInt64();
            var count = binReader.ReadInt32();

            for (var i = 0; i < count; i++)
            {
                schedule.Add((Action)binReader.ReadByte());
            }

            var command = new NotFoundCommand(console, storage, x)
            {
                Schedule = schedule
            };

            command.Save();
            return(command);
        }
        public RandomCommand(UserConsole console, Storage storage, long x) : base(console, storage, x)
        {
            Schedule.Add(Action.Read);
            Type = CommandType.Random;

            Save();
        }
        public static MedianCommand Load(UserConsole console, Storage storage)
        {
            var mem       = new MemoryStream(storage.Read());
            var schedule  = new List <Action>();
            var variables = new List <int>();

            using var binReader = new BinaryReader(mem);

            binReader.ReadByte();
            var x     = binReader.ReadInt64();
            var count = binReader.ReadInt32();

            for (var i = 0; i < count; i++)
            {
                schedule.Add((Action)binReader.ReadByte());
            }

            count = binReader.ReadInt32();
            for (var i = 0; i < count; i++)
            {
                variables.Add(binReader.ReadInt32());
            }

            var firstLaunch = binReader.ReadBoolean();

            var command = new MedianCommand(console, storage, x)
            {
                _variables     = variables,
                _isFirstLaunch = firstLaunch,
                Schedule       = schedule
            };

            command.Save();
            return(command);
        }
        public MedianCommand(UserConsole console, Storage storage, long x) : base(console, storage, x)
        {
            _variables = new List <int>();
            Type       = CommandType.Median;
            Schedule.Add(Action.Read);
            _isFirstLaunch = true;

            Save();
        }
 private void PrintHelp(UserConsole console, StateKeeper stateKeeper, List <string> helpText)
 {
     for (var i = stateKeeper.ShowedResults; i < helpText.Count; i++)
     {
         console.WriteLine(helpText[i]);
         stateKeeper.ShowedResults = i + 1;
         stateKeeper.SaveState();
     }
 }
        public AddCommand(UserConsole console, Storage storage, long x) : base(console, storage, x)
        {
            Type       = CommandType.Add;
            _variables = new List <int>();
            Schedule.Add(Action.Read);
            Schedule.Add(Action.Read);
            Schedule.Add(Action.Write);

            Save();
        }
 public HelpCommand(UserConsole console, Storage storage, long x) : base(console, storage, x)
 {
     Type          = CommandType.Help;
     _helpMessages = new List <HelpMessage>()
     {
         new HelpMessage(Action.Write, "Укажите команду, для которой хотите посмотреть помощь"),
         new HelpMessage(Action.Write, Commands),
         new HelpMessage(Action.Write, ExitMessage)
     };
     Save();
 }
 private void WriteInConsole(UserConsole console, string line)
 {
     if (previousIgnoreCounter > 0)
     {
         previousIgnoreCounter--;
     }
     else
     {
         console.WriteLine(line);
         UpgradeIgnore();
     }
 }
        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 void Random(UserConsole console)
        {
            const int a = 16807;
            const int m = 2147483647;

            var count = ReadNumber(console);

            for (var i = 0; i < count; i++)
            {
                WriteInConsole(console, x.ToString(Culture));
                x = (a * x) % m;
            }
        }
        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));
        }
        private void Median(UserConsole console)
        {
            var count   = ReadNumber(console);
            var numbers = new List <int>();

            for (var i = 0; i < count; i++)
            {
                numbers.Add(ReadNumber(console));
            }

            var result = CalculateMedian(numbers);

            console.WriteLine(result.ToString(Culture));
        }
        private long Random(UserConsole console, long x)
        {
            const int a = 16807;
            const int m = 2147483647;

            var count = ReadNumber(console);

            for (var i = 0; i < count; i++)
            {
                console.WriteLine(x.ToString(Culture));
                x = a * x % m;
            }

            return(x);
        }
        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 void Run(UserConsole userConsole, Storage storage)
 {
     userConsole = userConsole ?? throw new ArgumentNullException(nameof(userConsole));
     InitializeData(storage);
     while (true)
     {
         if (commandsLines.Count == 0)
         {
             ClearCommandInfo();
         }
         if (ExecuteLine(userConsole, TakeNewLine(userConsole)))
         {
             break;
         }
     }
 }
        public override void Run(UserConsole userConsole, Storage storage)
        {
            var myConsole = new MyConsole(storage, userConsole);

            while (true)
            {
                var input = myConsole.ReadLine();

                switch (input.Trim())
                {
                case "exit":
                    myConsole.data.x = 420;
                    myConsole.Serialize(new Datas());
                    myConsole.data = new Datas();

                    return;

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

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

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

                case "rand":
                    myConsole.data.x = Random(myConsole, myConsole.data.x);
                    break;

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

                long tempx = myConsole.data.x;
                myConsole.data              = new Datas();
                myConsole.lastInputCommand  = 0;
                myConsole.lastOutputCommand = 0;
                myConsole.data.x            = tempx;

                myConsole.Serialize(myConsole.data);
            }
        }
        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 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));
        }
        private void Help(UserConsole console)
        {
            const string exitMessage = "Чтобы выйти из режима помощи введите end";
            const string commands    = "Доступные команды: add, median, rand";

            WriteInConsole(console, "Укажите команду, для которой хотите посмотреть помощь");
            WriteInConsole(console, commands);
            WriteInConsole(console, exitMessage);

            while (true)
            {
                string command = TakeNewLine(console);
                if (ExecuteHelp(console, command))
                {
                    break;
                }
            }
        }
        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();
            }
        }