Ejemplo n.º 1
0
        /// <summary>
        /// Method RunCommandReader
        /// Starts reading commands.
        /// </summary>
        public void RunCommandReader()
        {
            printer.DisplayBeginInfo();
            bool exit = false;

            // The cycle of reading commands, while there is no command "exit".
            while (!exit)
            {
                Console.Write("Write a command : ");
                var command = Console.ReadLine();
                // Finds out the type of command.
                TypeOfCommands CommandType = GetTypeOfCommands(command);

                // Check for access to an empty catalog.
                if ((CommandType != TypeOfCommands.Add) && (CommandType != TypeOfCommands.None) &&
                    (CommandType != TypeOfCommands.Exit) && (catalog.Counter == 0))
                {
                    Console.WriteLine("Empty catalog.\n");
                    continue;
                }
                var splitCommand = command.Split(' ');
                // Execute the command, depending on the type.
                switch (CommandType)
                {
                case TypeOfCommands.None:
                    Console.WriteLine("Invalid command.\n");
                    break;

                case TypeOfCommands.Add:
                    catalogCommand = new Add(catalog, splitCommand[1], splitCommand[2],
                                             int.Parse(splitCommand[3]), int.Parse(splitCommand[4]));
                    break;

                case TypeOfCommands.CountAll:
                    catalogCommand = new CountAll(catalog);
                    break;

                case TypeOfCommands.CountTypes:
                    catalogCommand = new CountType(catalog);
                    break;

                case TypeOfCommands.AveragePriceAll:
                    catalogCommand = new AveragePrice(catalog);
                    break;

                case TypeOfCommands.AveragePriceType:
                    catalogCommand = new AveragePriceType(catalog, splitCommand[2]);
                    break;

                case TypeOfCommands.Exit:
                    exit = true;
                    break;
                }

                if (!exit && (CommandType != TypeOfCommands.None))
                {
                    catalogCommand.Execute();
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Method AddCommandInList
        /// Add command in actual list.
        /// </summary>
        /// <param name="command">Console command</param>
        private void AddCommandInList(string command)
        {
            ICatalogCommand catalogCommand = null;
            var             splitCommand   = command?.ToLower().Split(' ');

            if ((command == null) || (command == string.Empty) || (2 > splitCommand.Length))
            {
                Console.WriteLine("Invalid command.");
                return;
            }
            IReceiver Catalog;

            switch (splitCommand[1])
            {
            case "car":
                Catalog = CatalogOfCar;
                break;

            case "truck":
                Catalog = CatalogOfTruck;
                break;

            default:
                throw new Exception();
            }

            switch (splitCommand[0])
            {
            case "count_all":
                catalogCommand = new CountAllCommand(Catalog);
                break;

            case "count_types":
                catalogCommand = new CountTypeCommand(Catalog);
                break;

            case "average_price":
                catalogCommand = new AveragePriceCommand(Catalog);
                break;

            case "average_price_type":
                catalogCommand = new AveragePriceTypeCommand(Catalog, splitCommand[2]);
                break;
            }

            if (catalogCommand != null)
            {
                ListOfCommand.Add(catalogCommand);
            }
            else
            {
                Console.WriteLine("Invalid command.");
            }
        }