Example #1
0
        static void Main(string[] args)
        {
            Vehicle car   = CreateVehice();
            Vehicle truck = CreateVehice();

            int n = int.Parse(Console.ReadLine());

            for (int i = 0; i < n; i++)
            {
                string[] input       = Console.ReadLine().Split();
                string   command     = input[0];
                string   vehicleType = input[1];
                double   parameter   = double.Parse(input[2]);

                if (command == "Drive")
                {
                    try
                    {
                        if (vehicleType == nameof(Car))
                        {
                            car.DriveDistance(parameter);
                        }
                        else
                        {
                            truck.DriveDistance(parameter);
                        }
                        Console.WriteLine($"{vehicleType} travelled {parameter} km");
                    }
                    catch (InvalidOperationException ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }
                else
                {
                    if (vehicleType == nameof(Car))
                    {
                        car.Refuel(parameter);
                    }
                    else
                    {
                        truck.Refuel(parameter);
                    }
                }
            }

            Console.WriteLine(car);
            Console.WriteLine(truck);
        }