示例#1
0
    static void Main()
    {
        Motorbike.ShowAmountOfWheels();

        Motorbike m1 = new Motorbike();

        m1.SetBrand("Honda");
        m1.Show();

        Motorbike m2 = new Motorbike();

        m2.SetBrand("Yamaha");
        m2.SetWheels(3);  // !!!!!
        m2.Show();

        m1.Show();
    }
    public static void Main()
    {
        const int SIZE = 100;

        Vehicle[] vehicles = new Vehicle[SIZE];
        char      option;
        int       amount = 0;

        do
        {
            Console.WriteLine();
            Console.WriteLine("1. Add a car");
            Console.WriteLine("2. Add a truck");
            Console.WriteLine("3. Add a motorbike");
            Console.WriteLine("4. Show all data");
            Console.WriteLine("5. Exit");
            option = Convert.ToChar(Console.ReadLine());
            string brand, model;

            switch (option)
            {
            case '1':
                // Note: this repetitive fragment should be improved
                Console.Write("Enter the brand: ");
                brand = Console.ReadLine();
                Console.WriteLine("Enter the model: ");
                model = Console.ReadLine();
                Car c = new Car();
                c.SetBrand(brand);
                c.SetModel(model);
                vehicles[amount] = c;
                amount++;
                break;

            case '2':
                // Note: this repetitive fragment should be improved
                Console.Write("Enter the brand: ");
                brand = Console.ReadLine();
                Console.WriteLine("Enter the model: ");
                model = Console.ReadLine();
                Truck t = new Truck();
                t.SetBrand(brand);
                t.SetModel(model);
                vehicles[amount] = t;
                amount++;
                break;

            case '3':
                // Note: this repetitive fragment should be improved
                Console.Write("Enter the brand: ");
                brand = Console.ReadLine();
                Console.WriteLine("Enter the model: ");
                model = Console.ReadLine();
                Motorbike m = new Motorbike();
                m.SetBrand(brand);
                m.SetModel(model);
                vehicles[amount] = m;
                amount++;
                break;

            case '4':
                if (amount > 0)
                {
                    for (int i = 0; i < amount; i++)
                    {
                        vehicles[i].ShowData();
                        Console.WriteLine();
                    }
                }
                else
                {
                    Console.WriteLine("No vehicles registered");
                }

                break;

            case '5':
                break;

            default:
                Console.WriteLine("Invalid option");
                break;
            }
        }while (option != '5');
        Console.WriteLine("Bye!");
    }