コード例 #1
0
        static void Main(string[] args)
        {
            var     carArgs            = Console.ReadLine().Split();
            var     carFuelQuantity    = double.Parse(carArgs[1]);
            var     carFuelConsumption = double.Parse(carArgs[2]);
            Vehicle car = new Car(carFuelQuantity, carFuelConsumption);

            var     truckArgs            = Console.ReadLine().Split();
            var     truckFuelQuantity    = double.Parse(truckArgs[1]);
            var     truckFuelConsumption = double.Parse(truckArgs[2]);
            Vehicle truck = new Truck(truckFuelQuantity, truckFuelConsumption);

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

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

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

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

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

            Console.WriteLine(car.ToString());
            Console.WriteLine(truck.ToString());
        }
コード例 #2
0
        private static void ProcessCommands(Car car, Truck truck, int numberOfCommands)
        {
            for (int i = 0; i < numberOfCommands; i++)
            {
                var commandTokens = Console.ReadLine()
                                    .Split(' ', StringSplitOptions.RemoveEmptyEntries);

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

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

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

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

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

                default:
                    throw new ArgumentException();
                }
            }
        }
コード例 #3
0
        private static void ExecuteCommands(Car car, Truck truck)
        {
            var numberOfCommands = int.Parse(Console.ReadLine());

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

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

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

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

                    default:
                        break;
                    }

                    break;

                default:
                    break;
                }

                numberOfCommands--;
            }
        }
コード例 #4
0
ファイル: Program.cs プロジェクト: nikolaspasov/C-Sharp-OOP
        static void Main(string[] args)
        {
            string carInfo   = Console.ReadLine();
            string truckInfo = Console.ReadLine();

            Vehicle car = new Car
                              (double.Parse(carInfo.Split(" ")[1]), double.Parse(carInfo.Split(" ")[2]));
            Vehicle truck = new Truck
                                (double.Parse(truckInfo.Split(" ")[1]), double.Parse(truckInfo.Split(" ")[2]));

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

            for (int i = 0; i < commandNum; i++)
            {
                string action      = Console.ReadLine();
                string vehicleType = action.Split(" ")[1];
                string actionType  = action.Split(" ")[0];

                if (vehicleType == "Car")
                {
                    switch (actionType)
                    {
                    case "Drive": Console.WriteLine(car.Drive(double.Parse(action.Split(" ")[2]))); break;

                    case "Refuel": car.Refuel(double.Parse(action.Split(" ")[2])); break;
                    }
                }
                else
                {
                    switch (actionType)
                    {
                    case "Drive": Console.WriteLine(truck.Drive(double.Parse(action.Split(" ")[2]))); break;

                    case "Refuel": truck.Refuel(double.Parse(action.Split(" ")[2])); break;
                    }
                }
            }
            Console.WriteLine($"Car: {car.FuelQuantity:f2}");
            Console.WriteLine($"Truck: {truck.FuelQuantity:f2}");
        }