Skip to content
Snippets Groups Projects
Select Git revision
  • a8d442c96101a69f034baa48a651c9149da05315
  • main default protected
  • ghebremm-main-patch-40518
3 results

Buffer.cs

Blame
  • Michael Ghebremussie's avatar
    Michael Ghebremussie authored
    # Conflicts:
    #   Aufgabe3/ConsoleApplication1/Buffer.cs
    eea55fe8
    History
    Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    Buffer.cs 1.63 KiB
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Threading;
    
    namespace ConsoleApplication1
    {
        /// <summary>
        /// Creating a Class Buffer that contains a Queue that represent the "Parking lot".
        /// The Queue is limited by the size that can manually be setted
        /// </summary>
        
        public class Buffer
        {
            private Queue<Car> _carQueue;
            private int _size = 10;
            private Mutex _mutex;
    
            // Constructor initialized with a Mutex and a Queue.
            public Buffer()
            {
                _mutex = new Mutex();
                _carQueue = new Queue<Car>();
            }
    
            // This function adds a Car Object into the Queue.
            public void Push(Car c)
            {
                // Check whether the Queue is full and throws an Exception if so.
                if (_carQueue.Count == _size)
                {
                    throw new Exception("Buffer full");
                }  
                
                _carQueue.Enqueue(c);
            }
    
            // Removes a Car Object from the Queue and returns the Queue.
            public Car Pop()
            {
                if (_carQueue.Count == 0)
                {
                    throw new Exception("Buffer empty");
                }
    
                return _carQueue.Dequeue();
            }
    
            // Check if the Queue is full.
            public bool Full()
            {
                return _carQueue.Count == _size;
            }
    
            // Check if the Queue is empty.
            public bool Empty()
            {
                return _carQueue.Count == 0;
            }
    
            // returns the Mutex. 
            public Mutex GetMutex()
            {
                return this._mutex;
            }
        }
    }