Ejemplo n.º 1
0
 /// <summary>
 /// Default implementation for adding product. 
 /// Adds new product to the products collection
 /// </summary>
 /// <param name="product">Product to be added</param>
 public virtual void AddProduct(Product product)
 {
     if (_products.Count < _productCapacity)
     {
         var backedProducts = _products;
         try
         {
             /// since product list can be be updated in ANY TIME (according to task)
             /// need to ensure it is only changed by one code in a time
             /// This will help avoid ordering a product being changed or deleted
             lock (_lockObject)
             {
                 _products.Add(product);
             }
         }
         catch (Exception ex)
         {
             // revert any changes to products before sending ex further
             _products = backedProducts;
             throw ex;
         }
     }
     else
     {
         throw new IndexOutOfRangeException("Product capacity limited");
     }
 }
        public void TakeOne_NotAvaliable_ExceptionThrown()
        {
            var info = new Product(Guid.NewGuid(), "Cookie", 10);
            var amount = 0;
            var instance = new ProductCollection(info, amount);

            var taken = instance.TakeOne();
        }
        public void IsAvaliableProperty_ZeroAmount_FalseReturned()
        {
            var info = new Product(Guid.NewGuid(), "Cookie", 10);
            var amount = 0;

            var instance = new ProductCollection(info, amount);

            Assert.IsFalse(instance.IsAvaliable);
        }
        public void Constructor_ValidArguments_InstanceCreated()
        {
            var info = new Product(Guid.NewGuid(), "Cookie", 10);
            var amount = 10;

            var instance = new ProductCollection(info, amount);

            Assert.AreEqual(instance.ProductInfo, info);
            Assert.AreEqual(instance.Amount, amount);
        }
        public void TakeOne_IsAvaliable_ProductReturned()
        {
            var info = new Product(Guid.NewGuid(), "Cookie", 10);
            var amount = 10;
            var instance = new ProductCollection(info, amount);

            var taken = instance.TakeOne();

            Assert.AreEqual(info.Barcode, taken.Barcode);
            Assert.AreEqual(info.Name, taken.Name);
            Assert.AreEqual(info.Price, taken.Price);
        }
Ejemplo n.º 6
0
        public void Constructor_ValidArguments_InstanceCreated()
        {
            Guid barcode = Guid.NewGuid();
            string name = "Cookie";
            int price = 10;

            var instance = new Product(barcode, name, price);

            Assert.AreEqual(instance.Barcode, barcode);
            Assert.AreEqual(instance.Name, name);
            Assert.AreEqual(instance.Price, price);
        }
Ejemplo n.º 7
0
        public void Clone_Cloned()
        {
            Guid barcode = Guid.NewGuid();
            string name = "Cookie";
            int price = 10;

            var instance = new Product(barcode, name, price);
            var cloned = instance.Clone();

            Assert.AreEqual(instance.Barcode, cloned.Barcode);
            Assert.AreEqual(instance.Name, cloned.Name);
            Assert.AreEqual(instance.Price, cloned.Price);
        }
        public ProductCollection(Product info, int amount)
        {
            if (info == null)
            {
                throw new ArgumentNullException(nameof(info));
            }

            if (amount < 0)
            {
                throw new ArgumentException(nameof(amount));
            }

            ProductInfo = info;
            Amount = amount;
        }
        public void Constructor_LessThanZeroAmount_ExceptionThrown()
        {
            var info = new Product(Guid.NewGuid(), "Cookie", 10);

            new ProductCollection(info, -10);
        }