Пример #1
0
        static void Main(string[] args)
        {
            OfferSystem      offerSystem = new OfferSystem();
            OfferInterpreter interpreter = new OfferInterpreter(new ConsoleOutputWriter());

            while (true)
            {
                var line = Console.ReadLine();
                if (!interpreter.ProcessCommand(line, offerSystem))
                {
                    break;
                }
            }
        }
Пример #2
0
        public bool ProcessCommand(string line, OfferSystem offerSystem)
        {
            if (string.IsNullOrWhiteSpace(line))
            {
                Console.WriteLine("Error: Valid command is missing");
                return(true);
            }

            var codeAndComment = line.Split(new char[] { '#' });

            var parts = codeAndComment[0].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

            string command = parts[0].ToLowerInvariant();

            switch (command)
            {
            case "driver":
                if (parts.Length < 3)
                {
                    OutputInsufficientArguments(parts[0]);
                    return(true);
                }

                uint capacity;
                if (!uint.TryParse(parts[1], out capacity))
                {
                    OutputInvalidArgument(command, parts[1]);
                    return(true);
                }

                offerSystem.Driver(GetDriverName(parts, 2), capacity);
                break;

            case "shipment":
                if (parts.Length < 3)
                {
                    OutputInsufficientArguments(parts[0]);
                    return(true);
                }

                uint shipmentId;
                if (!uint.TryParse(parts[1], out shipmentId))
                {
                    OutputInvalidArgument(command, parts[1]);
                    return(true);
                }
                uint shipCapacity;
                if (!uint.TryParse(parts[2], out shipCapacity))
                {
                    OutputInvalidArgument(command, parts[2]);
                    return(true);
                }

                var result = offerSystem.Shipment(shipmentId, shipCapacity);
                if (result != null)
                {
                    _outputWriter.WriteLine(result);
                }
                else
                {
                    _outputWriter.WriteLine(NoValidDriversString);
                }
                break;

            case "offer":
                if (parts.Length < 4)
                {
                    OutputInsufficientArguments(parts[0]);
                    return(true);
                }

                if (!uint.TryParse(parts[1], out shipmentId))
                {
                    OutputInvalidArgument(command, parts[1]);
                    return(true);
                }
                OfferResult offerResult;
                if (!Enum.TryParse <OfferResult>(parts[2].ToLowerInvariant(), out offerResult))
                {
                    OutputInvalidArgument(command, parts[2]);
                    return(true);
                }
                bool hasError;
                result = offerSystem.Offer(shipmentId, offerResult, GetDriverName(parts, 3), out hasError);
                if (offerResult != OfferResult.accept || hasError)
                {
                    if (result != null)
                    {
                        _outputWriter.WriteLine(result);
                    }
                    else
                    {
                        _outputWriter.WriteLine(NoValidDriversString);
                    }
                }
                break;

            case _endCommand:
                return(false);

            default:
                OutputUnrecognizedCommand(command);
                break;
            }

            return(true);
        }