1
0
oop-1-labs/Lab3/Savarankiskas4/StudentsContainer.cs

98 lines
2.3 KiB
C#

using System;
using System.Collections.Generic;
using System.Text;
namespace Savarankiskas4
{
class StudentsContainer
{
private int Capacity;
public int Count { get; private set; } = 0;
private Student[] students;
public StudentsContainer(int capacity = 16)
{
Capacity = capacity;
students = new Student[capacity];
}
public void Add(Student student)
{
if (Count == Capacity)
{
EnsureCapacity(Capacity * 2);
}
students[Count] = student;
Count++;
}
public void Put(int index, Student student)
{
students[index] = student;
}
public Student Get(int index)
{
return students[index];
}
public bool Contains(Student student)
{
for (int i = 0; i < Count; i++)
{
if (students[i].Equals(student))
{
return true;
}
}
return false;
}
public void Insert(int index, Student student)
{
for (int i = Count + 1; i > index; i--)
{
students[i] = students[i - 1];
}
students[index] = student;
Count++;
}
public void RemoveAt(int index)
{
for (int i = index; i < Count; i++)
{
students[i] = students[i + 1];
}
Count--;
}
public void Remove(Student student)
{
for (int i = 0; i < Count; i++)
{
if (students[i].Equals(student))
{
RemoveAt(i);
return;
}
}
}
private void EnsureCapacity(int minimumCapacity)
{
if (minimumCapacity > Capacity)
{
Student[] dogsClone = new Student[minimumCapacity];
for (int i = 0; i < Count; i++)
{
dogsClone[i] = students[i];
}
Capacity = minimumCapacity;
students = dogsClone;
}
}
}
}