Advertisement
wingman007

C#_OOP_2b_Manager.cs

Mar 23rd, 2014
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.11 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace OOP9
  7. {
  8.     class Manager<T>
  9.     {
  10.         private const int LIMIT = 5;
  11.         T[] entities;
  12.         private int counter;
  13.  
  14.         public Manager()
  15.             : this(LIMIT)
  16.         {}
  17.  
  18.         public Manager(int limit)
  19.         {
  20.             entities = new T[limit];
  21.             counter = 0;
  22.         }
  23.  
  24.         public void Add(T entity)
  25.         {
  26.             if (counter >= entities.Length)
  27.             {
  28.                 throw new InvalidOperationException("The limit has been reached");
  29.             }
  30.             entities[counter] = entity;
  31.             counter++;
  32.         }
  33.  
  34.         public T Remove()
  35.         {
  36.             if (counter == 0)
  37.             {
  38.                 throw new InvalidOperationException("No entities");
  39.             }
  40.             T temp = entities[counter - 1];
  41.             entities[counter - 1] = default(T);
  42.             counter--;
  43.             return temp;
  44.         }
  45.  
  46.         public int GetNumberOfEntities()
  47.         {
  48.             return counter;
  49.         }
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement