コード例 #1
0
        public static void Main(string[] args)
        {
            var garage1 = new Garage();

            garage1.AddCar("Audi RS6", "Red", 305, 2020);
            while (true)
            {
                Console.Clear();
                Console.WriteLine("Добро пожаловать в ваш гараж, что хотите сделать?");
                Console.WriteLine(
                    "1)Вывести список автомобилей\n" +
                    "2)Добавить автомобиль\n" +
                    "3)Убрать автомобиль\n" +
                    "4)Прокатиться на автомобиле!\n" +
                    "5)Выход\n");
                int input;

                while (!int.TryParse(Console.ReadLine(), out input) || input < 0 || input > 5)
                {
                    Console.Write("Некорректный ввод. Попробуйте снова: ");
                }
                switch (input)
                {
                //Показать автомобили
                case 1:
                    Console.Clear();
                    garage1.ShowCars();
                    Console.ReadKey();
                    break;

                //Добавить автомобиль
                case 2:
                    Console.Clear();
                    string carModel, color;
                    float  speed;
                    int    yearOfIssue;
                    Console.Write("Введите марку автомобиля: ");
                    while (true)
                    {
                        carModel = Console.ReadLine();
                        if (string.IsNullOrEmpty(carModel))
                        {
                            Console.Write("Некорректный ввод. Попробуйте снова: ");
                        }
                        else
                        {
                            break;
                        }
                    }

                    Console.Write("Введите цвет машины: ");
                    while (true)
                    {
                        color = Console.ReadLine();
                        if (string.IsNullOrEmpty(color))
                        {
                            Console.Write("Некорректный ввод. Попробуйте снова: ");
                        }
                        else
                        {
                            break;
                        }
                    }

                    Console.Write("Введите скорость машины: ");
                    while (!float.TryParse(Console.ReadLine(), out speed) || speed <= 0)
                    {
                        Console.Write(
                            "Некорректный ввод. Скорость не может быть равна или меньше 0. Попробуйте снова: ");
                    }

                    Console.Write("Введите год выпуска машины: ");
                    while (!int.TryParse(Console.ReadLine(), out yearOfIssue) || yearOfIssue <= 0)
                    {
                        Console.Write(
                            "Некорректный ввод. Год выпуска не может быть равен или меньшим 0. Попробуйте снова: ");
                    }

                    garage1.AddCar(carModel, color, speed, yearOfIssue);
                    break;

                //Удалить автомобиль
                case 3:
                    Console.Clear();

                    if (garage1.CountCars() > 1)
                    {
                        int lowInput;
                        Console.WriteLine("Какой из автомобилей хотите убрать из гаража?");
                        garage1.ShowCars();

                        while (!int.TryParse(Console.ReadLine(), out lowInput) || lowInput <= 0 ||
                               lowInput > garage1.CountCars())
                        {
                            Console.Write("Некорректный ввод. Попробуйте снова: ");
                        }
                        garage1.DeleteCar(lowInput);
                    }
                    else
                    {
                        garage1.DeleteCar(0);
                    }

                    break;

                //Выехать на автомобиле
                case 4:
                    if (garage1.CountCars() > 1)
                    {
                        int lowInput;
                        Console.WriteLine("На чём прокатимся?");
                        garage1.ShowCars();

                        while (!int.TryParse(Console.ReadLine(), out lowInput) || lowInput <= 0 ||
                               lowInput > garage1.CountCars())
                        {
                            Console.Write("Некорректный ввод. Попробуйте снова: ");
                        }
                        garage1.RideCar(lowInput);
                    }
                    else
                    {
                        garage1.RideCar(0);
                    }

                    break;

                case 5:
                    break;
                }

                if (input == 5)
                {
                    break;
                }
            }
        }
コード例 #2
0
        static void Main(string[] args)
        {
            while (true)
            {
                int n = 0;
                Console.Write("Enter the option(Add - 1, Remove - 2, Find - 3, Show the garage - 4:");
                int    tempYear = 0;
                double tempSpeed = 0;
                string tempName, tempColor;
                if (int.TryParse(Console.ReadLine(), out n))
                {
                    switch (n)
                    {
                    case 1:
                    {
                        Console.WriteLine("Enter the options(for adding):");
                        Console.Write("\tName:");
                        tempName = Console.ReadLine();
                        Console.Write("\tSpeed:");
                        double.TryParse(Console.ReadLine(), out tempSpeed);
                        Console.Write("\tColor:");
                        tempColor = Console.ReadLine();
                        Console.Write("\tYear:");
                        int.TryParse(Console.ReadLine(), out tempYear);
                        Car car = new Car(tempYear, tempName, tempColor, tempSpeed);
                        Garage.AddCar(car);
                        Console.WriteLine("Car was added");
                    }
                    break;

                    case 2:
                    {
                        Console.WriteLine("Enter the options(for removing):");
                        Console.Write("\tName:");
                        tempName = Console.ReadLine();
                        Console.Write("\tSpeed:");
                        double.TryParse(Console.ReadLine(), out tempSpeed);
                        Console.Write("\tColor:");
                        tempColor = Console.ReadLine();
                        Console.Write("\tYear:");
                        int.TryParse(Console.ReadLine(), out tempYear);
                        Car car = new Car(tempYear, tempName, tempColor, tempSpeed);
                        Garage.RemoveCar(car);
                        Console.WriteLine("Car was removed if had been found");
                    }
                    break;

                    case 3:
                    {
                        Console.WriteLine("Enter the options(for finding):");
                        Console.Write("\tName:");
                        tempName = Console.ReadLine();
                        Console.Write("\tSpeed:");
                        double.TryParse(Console.ReadLine(), out tempSpeed);
                        Console.Write("\tColor:");
                        tempColor = Console.ReadLine();
                        Console.Write("\tYear:");
                        int.TryParse(Console.ReadLine(), out tempYear);
                        Car car = new Car(tempYear, tempName, tempColor, tempSpeed);
                        Garage.ShowFindCars(Garage.SequenceCar(car));
                    }
                    break;

                    case 4:
                    {
                        Garage.ShowGarage();
                    }
                    break;

                    default: Console.WriteLine("Incorrect value"); break;
                    }
                }
                else
                {
                    continue;
                }
            }
        }