wingman007

2015_OOP_Features_Manager

May 27th, 2015
578
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.29 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace World1
  8. {
  9.     class Manager<T>
  10.     {
  11.         private const int CAPACITY = 5;
  12.  
  13.         private T[] entities;
  14.  
  15.         private int counter = 0;
  16.  
  17.         public T this[int i]
  18.         {
  19.             get { return entities[i]; }
  20.             set { entities[i] = value; }
  21.         }
  22.  
  23.         public Manager()
  24.             : this(CAPACITY)
  25.         { }
  26.         public Manager(int capacity)
  27.         {
  28.             entities = new T[capacity];
  29.         }
  30.  
  31.         public void AddEntity(T entity) // Push
  32.         {
  33.             if (counter >= entities.Length)
  34.             {
  35.                 throw new InvalidOperationException("The stack is full!");
  36.             }
  37.             this.entities[counter] = entity;
  38.             counter++;
  39.         }
  40.  
  41.         public T RemoveEntity() // <K>
  42.         {
  43.             if (counter == 0) {
  44.                 throw new InvalidOperationException("The stack is empty!");
  45.             }
  46.             T temp = entities[counter - 1];
  47.             entities[counter - 1] = default(T);
  48.             counter--;
  49.             return temp;
  50.         }
  51.  
  52.         public int GetNumberOfEntities()
  53.         {
  54.             return counter;
  55.         }
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment