Для виконання курсового проекту необхідно розробити щонайменше два класи: клас для окремих елементів і клас -колекцію (клас-контейнер).
У даному прикладі класи виконують такі функції:
1) Garage — клас-контейнер, що містить масив елементів (машин — екземплярів класу Car), його функції — додавати «машини» до внутрішнього масиву (carArray), видаляти машини з масиву, здійснювати пошук машин в масиві за певними ознаками тощо;
2) Car — клас окремих елементів (у даному випадку — машин), його функції — зберігати і забезпечувати коректність даних про певну машину (марка та швидкість — string marka; int speed).
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace ConsoleApplication1
{
public class Garage:IEnumerable
{
public string machina;
public int sp;
protected int size;
protected int index;
public Car[] carArray;
public Garage(int size)
{
carArray = new Car[size];
}
public void AddCar(int index, string machina, int sp)
{
carArray[index] = new Car(machina, sp);
}
#region Члены IEnumerable
public IEnumerator GetEnumerator()
{
return carArray.GetEnumerator();
}
#endregion
}
public class Car
{
protected string marka;
protected int speed;
public Car(string marka, int speed)
{
this.marka = marka;
this.speed = speed;
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Введите количество машин в гараже: ");
int n = Convert.ToInt32(Console.ReadLine());
Garage x1 = new Garage(n);
for (int i = 0; i < n; i++)
{
Console.Write("Марка машин: ");
string machina = Console.ReadLine();
Console.Write("Скорость машины: ");
int sp = Convert.ToInt32(Console.ReadLine());
x1.AddCar(i, machina, sp);
}
Console.WriteLine("\n");
Console.WriteLine("Просмотр машин в гараже");
foreach (Car с in x1)
{
Console.WriteLine("Марка {0} в гараже и ее скорость {1}", x1.machina, x1.sp);
}
}
}
}
Докладніше можна прочитати на форумі.