private static void ProcessCommands(Car car, Truck truck, Bus bus, int numberOfCommands)
        {
            for (int i = 0; i < numberOfCommands; i++)
            {
                var commandTokens = Console.ReadLine()
                                    .Split(' ', StringSplitOptions.RemoveEmptyEntries);

                var command = commandTokens[0];
                var vehicle = commandTokens[1];

                try
                {
                    switch (command)
                    {
                    case "Drive":
                        var distance = double.Parse(commandTokens[2]);

                        if (vehicle == "Car")
                        {
                            car.Drive(distance);
                        }
                        else if (vehicle == "Truck")
                        {
                            truck.Drive(distance);
                        }
                        else
                        {
                            bus.Drive(distance);
                        }
                        break;

                    case "Refuel":
                        var liters = double.Parse(commandTokens[2]);

                        if (vehicle == "Car")
                        {
                            car.Refuel(liters);
                        }
                        else if (vehicle == "Truck")
                        {
                            truck.Refuel(liters);
                        }
                        else
                        {
                            bus.Refuel(liters);
                        }
                        break;

                    case "DriveEmpty":
                        distance = double.Parse(commandTokens[2]);
                        bus.Drive(distance);
                        break;

                    default:
                        throw new ArgumentException("Invalid command");
                    }
                }
                catch (ArgumentException ae)
                {
                    Console.WriteLine(ae.Message);
                }
            }
        }
Example #2
0
        static void Main(string[] args)
        {
            var car   = new Car(0, 0, 0);
            var truck = new Truck(0, 0, 0);
            var bus   = new Bus(0, 0, 0);

            for (int i = 0; i < 3; i++)
            {
                var veichleInfo            = Console.ReadLine().Split();
                var veichleFuelQuantity    = double.Parse(veichleInfo[1]);
                var veichleFuelConsumption = double.Parse(veichleInfo[2]);
                var veichleTankCapacity    = double.Parse(veichleInfo[3]);

                switch (veichleInfo[0])
                {
                case "Car":
                    car = new Car(veichleFuelQuantity, veichleFuelConsumption, veichleTankCapacity);
                    break;

                case "Truck":
                    truck = new Truck(veichleFuelQuantity, veichleFuelConsumption, veichleTankCapacity);
                    break;

                case "Bus":
                    bus = new Bus(veichleFuelQuantity, veichleFuelConsumption, veichleTankCapacity);
                    break;
                }
            }

            var commandCount = int.Parse(Console.ReadLine());

            for (int i = 0; i < commandCount; i++)
            {
                var command = Console.ReadLine().Split();
                switch (command[0])
                {
                case "DriveEmpty":
                    var distance = double.Parse(command[2]);
                    Console.WriteLine(bus.DriveEmpty(distance));
                    break;

                case "Drive":
                    distance = double.Parse(command[2]);
                    switch (command[1])
                    {
                    case "Car":
                        Console.WriteLine(car.Drive(distance));
                        break;

                    case "Truck":
                        Console.WriteLine(truck.Drive(distance));
                        break;

                    case "Bus":
                        Console.WriteLine(bus.Drive(distance));
                        break;
                    }
                    break;

                case "Refuel":
                    var amount = double.Parse(command[2]);
                    try
                    {
                        switch (command[1])
                        {
                        case "Car":
                            car.Refuel(amount);
                            break;

                        case "Truck":
                            truck.Refuel(amount);
                            break;

                        case "Bus":
                            bus.Refuel(amount);
                            break;
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                    }
                    break;
                }
            }

            Console.WriteLine(car.ToString());
            Console.WriteLine(truck.ToString());
            Console.WriteLine(bus.ToString());
        }
        private static void ExecuteCommands(Car car, Truck truck, Bus bus)
        {
            var numberOfCommands = int.Parse(Console.ReadLine());

            while (numberOfCommands > 0)
            {
                numberOfCommands--;

                var command     = Console.ReadLine().Split();
                var action      = command[0];
                var vehicleType = command[1];
                var value       = double.Parse(command[2]);

                try
                {
                    switch (action)
                    {
                    case "Drive":
                        Console.WriteLine(vehicleType == "Car"
                                ? car.Drive(value)
                                : vehicleType == "Truck"
                                ? truck.Drive(value)
                                : bus.Drive(value));
                        break;

                    case "Refuel":
                        switch (vehicleType)
                        {
                        case "Car":
                            car.Refuel(value);
                            break;

                        case "Truck":
                            truck.Refuel(value);
                            break;

                        case "Bus":
                            bus.Refuel(value);
                            break;

                        default:
                            break;
                        }

                        break;

                    case "DriveEmpty":
                        bus.IsAirConditionerTurnedOn = false;
                        Console.WriteLine(bus.Drive(value));
                        bus.IsAirConditionerTurnedOn = true;
                        break;

                    default:
                        break;
                    }
                }
                catch (ArgumentException ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }