Beispiel #1
0
        public void CheckoutGetTotal_SingleItem_Test()
        {
            // arrange
            string           skuA     = "A";
            int              expected = priceA;
            StockKeepingUnit itemA    = new StockKeepingUnit
            {
                Name             = "A",
                UnitPrice        = priceA,
                HasMultibuyOffer = false
            };

            mockStockControl.Setup(s => s.GetStockControlUnit(skuA)).Returns(itemA);

            ICheckout checkout = new Checkout(mockStockControl.Object);

            checkout.Scan(skuA);

            // act
            var actual = checkout.GetTotal();

            // assert
            Assert.AreEqual(expected, actual);

            repository.VerifyAll();
        }
Beispiel #2
0
        public void Get_Discount_Price_Chooses_The_Best_Discount_For_Basket()
        {
            var itemA = new StockKeepingUnit("A", 50);
            var itemB = new StockKeepingUnit("B", 30);

            // two A for 75
            DiscountRule rule1 = new DiscountRuleBuilder().WithUnits(itemA, 2).WithPrice(75).Build();

            // two A and one B for 70
            DiscountRule rule2 = new DiscountRuleBuilder().WithUnits(itemA, 2).WithUnits(itemB, 1).WithPrice(100).Build();

            _mockRuleRepository.MockRules = new List <DiscountRule> {
                rule1, rule2
            };

            List <ItemPile> basket = new List <ItemPile>
            {
                new ItemPile(itemA, 2),
                new ItemPile(itemB, 1)
            };

            //the rule which renders the smallest price is rule2
            decimal result = _pricing.GetDiscountedPrice(basket);

            result.Should().Be(175);
        }
        public void FindSKUTest_VerifyByName()
        {
            StockKeepingUnits target = new StockKeepingUnits(); // TODO: Initialize to an appropriate value

            StockKeepingUnit wheel = new StockKeepingUnit("Wheel", 88.50);

            target.Add(wheel);
            target.Add(new StockKeepingUnit(wheel)); //because, we don't just use the same instance
            target.Add(new StockKeepingUnit("Tyre", 50.00));
            target.Add(new StockKeepingUnit(wheel)); //because, we don't just use the same instance
            target.Add(new StockKeepingUnit("Tyrehorn", 11.50));
            target.Add(wheel);


            string skuname = wheel.Name; // TODO: Initialize to an appropriate value

            //as we are not always returning the SAME instance, testing that would be pointless.
            StockKeepingUnit expected = new StockKeepingUnit(wheel); // TODO: Initialize to an appropriate value

            StockKeepingUnit actual;

            actual = target.FindSKU(skuname);

            Assert.AreEqual <string>(expected.Name, actual.Name);
        }
        public void FindSKUTest_VerifyByPrice()
        {
            StockKeepingUnits target = new StockKeepingUnits(); // TODO: Initialize to an appropriate value

            StockKeepingUnit wheel = new StockKeepingUnit("Wheel", 88.50);

            target.Add(wheel);
            target.Add(new StockKeepingUnit(wheel)); //because, we don't just use the same instance
            target.Add(new StockKeepingUnit("Tyre", 50.00));
            target.Add(new StockKeepingUnit(wheel)); //because, we don't just use the same instance
            target.Add(new StockKeepingUnit("Tyrehorn", 11.50));
            target.Add(wheel);

            string skuname = wheel.Name; // TODO: Initialize to an appropriate value

            //as we are not always returning the SAME instance, testing that would be pointless.
            StockKeepingUnit expected = new StockKeepingUnit(wheel); // TODO: Initialize to an appropriate value

            //we have enfourced a limit that the FindSKU will return null when there is 0 or > 1 SKU
            //in the list.
            StockKeepingUnit actual;

            actual = target.FindSKU(skuname);

            Assert.IsNotNull(actual, "There was no 'actual' item instance found");
            Assert.AreEqual <double>(expected.Price, actual.Price, string.Format("L: {0} - R: {1}", expected.Price, actual.Price));
        }
        /// <summary>
        ///
        /// </summary>
        public Receipt CalculateReceipt(string[] items)
        {
            Receipt result = new Receipt(); //zero the result

            StockKeepingUnits itemSKUs = new StockKeepingUnits();

            foreach (var s in items)
            {
                StockKeepingUnit tempRef = fSKUs.FindSingletonSKU(s);
                if (tempRef != null)
                {
                    itemSKUs.Add(new StockKeepingUnit(tempRef));
                }
                else
                {
                    result.AddRemark(String.Format("\"{0}\" was not recognised and skipped", s));
                }
            }

            result.AddRemark(itemSKUs.ToString());

            //now that we know what we are buting, we should apply the discounts
            result.Subtotal = itemSKUs.Price();

            DiscountResults discountResults = fDiscounts.ApplyDiscounts(itemSKUs);

            result.Total = result.Subtotal - discountResults.Sum();
            result.AddDiscount(discountResults.ToString());

            return(result);
        }
 public StockKeepingUnitVM(StockKeepingUnit sku)
 {
     this.sku          = sku.sku;
     this.name         = sku.name;
     this.inventory    = new InventoryVM(sku.inventory);
     this.isMarketable = inventory.quantity > 0;
 }
Beispiel #7
0
        public void Get_Discount_Price_Handles_Different_Rules_For_Same_Unit_When_Sufficient_Quantity_In_Basket()
        {
            var itemA = new StockKeepingUnit("A", 50);
            var itemB = new StockKeepingUnit("B", 30);

            // two A and one B for 70
            DiscountRule rule1 = new DiscountRuleBuilder().WithUnits(itemA, 2).WithUnits(itemB, 1).WithPrice(70).Build();

            // two A for 75
            DiscountRule rule2 = new DiscountRuleBuilder().WithUnits(itemA, 2).WithPrice(75).Build();

            _mockRuleRepository.MockRules = new List <DiscountRule> {
                rule1, rule2
            };

            List <ItemPile> basket = new List <ItemPile>
            {
                new ItemPile(itemA, 4),
                new ItemPile(itemB, 2)
            };

            //both rules should be applied
            decimal result = _pricing.GetDiscountedPrice(basket);

            result.Should().Be(175);
        }
Beispiel #8
0
 public ProductInfo(
     string name,
     Money price,
     StockKeepingUnit?productSKU = default)
 {
     Name       = name;
     Price      = price;
     ProductSKU = productSKU ?? StockKeepingUnit.NewSKU();
 }
Beispiel #9
0
        /// <summary>
        /// Test product properties are properly initialised with right values
        /// </summary>
        public void TestProductIsInitialisedWithGivenPrice()
        {
            var product = new StockKeepingUnit("A", 5);

            Assert.AreEqual(5, product.Price);
            Assert.IsFalse(product.IsPromoProduct);
            Assert.IsTrue(product.Name.Equals("A"));
            Assert.IsNull(product.CurrentPromoPricingRule);
        }
        public void StockKeepingUnitConstructorTest3()
        {
            string           name   = "Crunchy"; // TODO: Initialize to an appropriate value
            double           price  = 345.67F;   // TODO: Initialize to an appropriate value
            StockKeepingUnit target = new StockKeepingUnit(name, price);

            Assert.AreEqual <string>(name, target.Name, "Names did not match");
            Assert.AreEqual <double>(price, target.Price, "Price did not match");
            Assert.AreEqual <string>(target.Name, target.Description, "Description did not match name for 2 param constructor.");
        }
Beispiel #11
0
 public ActionResult Post([FromBody] StockKeepingUnit sku)
 {
     try
     {
         return(Ok(repository.Add(sku)));
     }
     catch (Exception ex)
     {
         return(BadRequest(ex.Message));
     }
 }
        public void ApplyDiscountTest2()
        {
            StockKeepingUnit temp = new StockKeepingUnit("Test1", 1.5);
            BuyItemsGetOneFreeDiscountUnit target = new BuyItemsGetOneFreeDiscountUnit("Test 1 discount", temp, 2); // TODO: Initialize to an appropriate value
            StockKeepingUnits items = new StockKeepingUnits(new StockKeepingUnit[] { temp, temp, temp });           // TODO: Initialize to an appropriate value

            double         expected = 1.5F;                                                                         // TODO: Initialize to an appropriate value
            DiscountResult actual   = target.ApplyDiscount(items);

            Assert.AreEqual <double>(expected, actual.Discount);
        }
Beispiel #13
0
        public StockKeepingUnit Add(StockKeepingUnit sku)
        {
            if (dados.Any(x => x.sku == sku.sku))
            {
                throw new Exception("Dois produtos são considerados iguais se os seus skus forem iguais");
            }

            dados.Add(sku);

            return(sku);
        }
Beispiel #14
0
        public void ApplyDiscountTest()
        {
            StockKeepingUnit temp = new StockKeepingUnit("Test1", 1.5);
            BuyItemsLessPercentageDiscountUnit target = new BuyItemsLessPercentageDiscountUnit("Test 1 discount", temp, 1, 10.0); // TODO: Initialize to an appropriate value
            StockKeepingUnits items = new StockKeepingUnits(new StockKeepingUnit[] { temp, temp });                               // TODO: Initialize to an appropriate value

            double         expected = 0.3F;                                                                                       // TODO: Initialize to an appropriate value
            DiscountResult actual   = target.ApplyDiscount(items);

            Assert.IsTrue(System.Math.Round(expected, 2) == System.Math.Round(actual.Discount, 2));
        }
        public void StockKeepingUnitConstructorTest()
        {
            string           name        = "Crunchy";                                // TODO: Initialize to an appropriate value
            string           description = "Case of Cadbury Crunchy chocolate bars"; // TODO: Initialize to an appropriate value
            double           price       = 345.67F;                                  // TODO: Initialize to an appropriate value
            StockKeepingUnit target      = new StockKeepingUnit(name, description, price);

            Assert.AreEqual <string>(name, target.Name, "Names did not match");
            Assert.AreEqual <double>(price, target.Price, "Price did not match");
            Assert.AreEqual <string>(description, target.Description, "Description did not match");
        }
Beispiel #16
0
 public ActionResult Put(StockKeepingUnit sku)
 {
     try
     {
         return(Ok(repository.Update(sku)));
     }
     catch (Exception ex)
     {
         return(BadRequest(ex.Message));
     }
 }
Beispiel #17
0
        /// <summary>
        /// Test that when the AppyPromotionRule called, the <c>Product</c> properties are updated appropriately
        /// with the given <c>IPromotionPricingRule</c> properties
        /// is null
        /// </summary>
        public void TestAppyPromotionRuleUpdatesProductWithRuleProperties( )
        {
            var product   = new StockKeepingUnit("A", 5);
            var promoRule = new SpecialPricingRule("Test", 20, 0, 0);

            product.AppyPromotionRule(promoRule);

            Assert.AreEqual(20, product.Price);
            Assert.IsTrue(product.IsPromoProduct);
            Assert.IsTrue(product.Name.Equals("A"));
            Assert.IsNotNull(product.CurrentPromoPricingRule);
        }
Beispiel #18
0
        public void ApplyDiscountTest2()
        {
            StockKeepingUnit temp = new StockKeepingUnit("Test1", 1.5);
            BuyItemsLessPercentageDiscountUnit target = new BuyItemsLessPercentageDiscountUnit("Test 1 discount", temp, 2, 20.00F); // TODO: Initialize to an appropriate value
            StockKeepingUnits items = new StockKeepingUnits(new StockKeepingUnit[] { temp, temp, temp });                           // TODO: Initialize to an appropriate value

            double         expected = 0.3;                                                                                          // TODO: Initialize to an appropriate value
            DiscountResult actual   = target.ApplyDiscount(items);

            //Even though these were actually equal, precision screwed up the test.... *sigh*
            Assert.IsTrue(System.Math.Round(expected, 2) == System.Math.Round(actual.Discount, 2));
        }
Beispiel #19
0
        /// <summary>
        /// Shuffled/scrambled the the list of <c>StockKeepingUnit</c> by using the Fisher-Yates shuffle algorithm
        /// </summary>
        public static void ShuffleStockKeepingUnitList(ref List <StockKeepingUnit> list)
        {
            Random random = new Random( );

            for (int index = 0; index < list.Count; index++)
            {
                int randomNumber      = index + random.Next(list.Count - index);
                StockKeepingUnit unit = list[randomNumber];
                list[randomNumber] = list[index];
                list[index]        = unit;
            }
        }
Beispiel #20
0
        public void ApplyDiscountTest()
        {
            StockKeepingUnit temp  = new StockKeepingUnit("Test1", 1.5F);
            StockKeepingUnit temp2 = new StockKeepingUnit("Test2", 5.0F);
            BuyItemsGetPercentageFromItemDiscountUnit target = new BuyItemsGetPercentageFromItemDiscountUnit("Test 1 discount", temp, temp2, 2, 50.0); // TODO: Initialize to an appropriate value
            StockKeepingUnits items = new StockKeepingUnits(new StockKeepingUnit[] { temp, temp, temp2 });                                             // TODO: Initialize to an appropriate value

            double         expected = 2.5F;                                                                                                            // TODO: Initialize to an appropriate value
            DiscountResult actual   = target.ApplyDiscount(items);

            Assert.IsTrue(System.Math.Round(expected, 2) == System.Math.Round(actual.Discount, 2));
        }
Beispiel #21
0
 public void CheckOrderStock(int OrderID)
 {
     using (var stock = new StockKeepingUnit())
     {
         var amount = 0;
         foreach (var item in db.Items.Where(i => i.IsEnable.Value && i.OrderID.Value.Equals(OrderID)))
         {
             stock.SetItemData(item.ID);
             amount = stock.CheckInventory();
         }
     }
 }
Beispiel #22
0
        public void CheckoutGetTotal_MultipleItemsWithDiscounts3_Test()
        {
            int expected = 130 + priceA + 45 + priceC * 2;

            StockKeepingUnit itemA = new StockKeepingUnit
            {
                Name                  = "A",
                UnitPrice             = priceA,
                HasMultibuyOffer      = true,
                MultibuyPrice         = 130,
                MultibuyUnitsRequired = 3
            };

            StockKeepingUnit itemB = new StockKeepingUnit
            {
                Name                  = "B",
                UnitPrice             = priceB,
                HasMultibuyOffer      = true,
                MultibuyPrice         = 45,
                MultibuyUnitsRequired = 2
            };

            StockKeepingUnit itemC = new StockKeepingUnit
            {
                Name             = "C",
                UnitPrice        = priceC,
                HasMultibuyOffer = false
            };

            mockStockControl.Setup(s => s.GetStockControlUnit("A")).Returns(itemA);
            mockStockControl.Setup(s => s.GetStockControlUnit("B")).Returns(itemB);
            mockStockControl.Setup(s => s.GetStockControlUnit("C")).Returns(itemC);

            ICheckout checkout = new Checkout(mockStockControl.Object);

            checkout.Scan("A");
            checkout.Scan("C");
            checkout.Scan("B");
            checkout.Scan("B");
            checkout.Scan("A");
            checkout.Scan("C");
            checkout.Scan("A");
            checkout.Scan("A");

            // act
            var actual = checkout.GetTotal();

            // assert
            Assert.AreEqual(expected, actual);

            repository.VerifyAll();
        }
        public void ApplyDiscountTest3()
        {
            StockKeepingUnit temp = new StockKeepingUnit("Test1", 1.5);
            BuyItemsGetOneFreeDiscountUnit target = new BuyItemsGetOneFreeDiscountUnit("Test 1 discount", temp, 2); // TODO: Initialize to an appropriate value

            //this should match 2 offers, should ignore the extra item
            StockKeepingUnits items = new StockKeepingUnits(new StockKeepingUnit[] { temp, temp, temp, temp, temp, temp, temp }); // TODO: Initialize to an appropriate value

            double         expected = 3.0F;                                                                                       // TODO: Initialize to an appropriate value
            DiscountResult actual   = target.ApplyDiscount(items);

            Assert.AreEqual <double>(expected, actual.Discount);
        }
Beispiel #24
0
        public void ApplyDiscountTest2()
        {
            StockKeepingUnit temp  = new StockKeepingUnit("Test1", 1.5F);
            StockKeepingUnit temp2 = new StockKeepingUnit("Test2", 5.0F);
            BuyItemsGetPercentageFromItemDiscountUnit target = new BuyItemsGetPercentageFromItemDiscountUnit("Test 1 discount", temp, temp2, 2, 50.0); // TODO: Initialize to an appropriate value
            StockKeepingUnits items = new StockKeepingUnits(new StockKeepingUnit[] { temp, temp, temp2, temp, temp, temp2, temp2 });                   // TODO: Initialize to an appropriate value

            //there should be 2 discounts on Test 2 (50% or 5, so 2 x 2.5) and the extra Test2 should be ignored.
            double         expected = 5.0F; // TODO: Initialize to an appropriate value
            DiscountResult actual   = target.ApplyDiscount(items);

            Assert.IsTrue(System.Math.Round(expected, 2) == System.Math.Round(actual.Discount, 2), string.Format("L: {0} R: {1}", expected, actual.Discount));
        }
Beispiel #25
0
        public void BuyItemsLessPercentageDiscountUnitConstructorTest()
        {
            string           name    = "Test";                             // TODO: Initialize to an appropriate value
            StockKeepingUnit target1 = new StockKeepingUnit("Test1", 1.5); // TODO: Initialize to an appropriate value
            int    targetCount       = 1;                                  // TODO: Initialize to an appropriate value
            double percentage        = 1.0F;                               // TODO: Initialize to an appropriate value
            BuyItemsLessPercentageDiscountUnit target = new BuyItemsLessPercentageDiscountUnit(name, target1, targetCount, percentage);

            Assert.IsNotNull(target);
            Assert.AreEqual(name, target.Name);
            Assert.AreEqual(name, target.Name);
            Assert.AreEqual(targetCount, target.TargetLevel);
            Assert.AreEqual(percentage, target.Discount);
        }
Beispiel #26
0
        public StockKeepingUnit Update(StockKeepingUnit sku)
        {
            var item = dados.FirstOrDefault(x => x.sku == sku.sku);

            if (item == null)
            {
                throw new Exception($"SKU {sku.sku} não encontrado.");
            }

            dados.Remove(item);
            dados.Add(sku);

            return(sku);
        }
Beispiel #27
0
 public void SkuData_Test(int OrderID)
 {
     try
     {
         using (StockKeepingUnit stock = new StockKeepingUnit())
         {
             var IDs      = db.Items.Where(i => i.OrderID.Value.Equals(OrderID)).Select(i => i.ProductID).Distinct().ToArray();
             var response = stock.GetSkuData(IDs);
         }
     }
     catch (Exception e)
     {
         string errorMsg = string.Format("傳送出貨資料至PO系統失敗,請通知處理人員:{0}", e.InnerException != null ? e.InnerException.Message.Trim() : e.Message.Trim());
     }
 }
        public DiscountRuleBuilder WithUnits(StockKeepingUnit unit, int quantity)
        {
            ItemPile existinPile = _itemPiles.FirstOrDefault(i => i.Unit == unit);

            if (existinPile != null)
            {
                existinPile.AddToPile(quantity);
            }
            else
            {
                _itemPiles.Add(new ItemPile(unit, quantity));
            }

            return(this);
        }
Beispiel #29
0
        /// <summary>
        /// Test that when the EndPromo called, the <c>Product</c> properties are re-initialised with the given price
        /// with the given <c>IPromotionPricingRule</c> properties
        /// is null
        /// </summary>
        public void TestEndPromoReinitialisedProductWithGivenPrice( )
        {
            var product   = new StockKeepingUnit("A", 5);
            var promoRule = new SpecialPricingRule("Test", 40, 0, 0);

            product.AppyPromotionRule(promoRule);

            // Now end the promo
            product.EndPromo(6);

            Assert.AreEqual(6, product.Price);
            Assert.IsFalse(product.IsPromoProduct);
            Assert.IsTrue(product.Name.Equals("A"));
            Assert.IsNull(product.CurrentPromoPricingRule);
        }
Beispiel #30
0
        public static Cart GetCart(Dictionary <string, int> skuDictionary, Dictionary <string, int> skuPriceDictionary)
        {
            Cart cart = new Cart();

            foreach (KeyValuePair <string, int> keyValuePair in skuDictionary)
            {
                StockKeepingUnit stockKeepingUnit = new StockKeepingUnit();
                stockKeepingUnit.ID        = keyValuePair.Key;
                stockKeepingUnit.UnitPrice = skuPriceDictionary[keyValuePair.Key];
                stockKeepingUnit.Units     = keyValuePair.Value;

                cart.StockKeepingUnits.Add(stockKeepingUnit);
            }

            return(cart);
        }