Exemplo n.º 1
0
        static void Main(string[] args)
        {
            var fruits     = new Category("fruits");
            var vegetables = new Category("vegetables");

            var saltyFruits = new Category("saltyFruits", fruits);



            var apple  = new Product("apple", 5, fruits);
            var tomato = new Product("tomato", 3, saltyFruits);

            var cucumber = new Product("cucumber", 4, vegetables);

            var cart = new ShoppingCart();

            cart.AddItem(apple, 10);
            cart.AddItem(tomato, 5);
            cart.AddItem(cucumber, 2);

            var campaign1 = new CategoryProductCountCampaign(fruits, 20, 3, DiscountTypeEnum.Rate);
            var campaign2 = new CategoryProductCountCampaign(saltyFruits, 5, 2, DiscountTypeEnum.Rate);
            var campaign3 = new CategoryProductCountCampaign(vegetables, 10, 5, DiscountTypeEnum.Amount);

            var coupon = new CartMinValueCoupon(10, 100, DiscountTypeEnum.Amount);

            cart.ApplyDiscounts(campaign1, campaign2, campaign3);

            cart.ApplyCoupon(coupon);

            CartDeliveryConfig config = new CartDeliveryConfig(10, 2, 2.99);



            cart.ApplyDeliveryCost(config.CalculateFor(cart));

            var cartOperator = new ShoppingCartOperator(cart);

            var totalAmount = cartOperator.GetTotalAmount();
            var totalAmountAfterDiscounts = cartOperator.GetTotalAmountAfterDiscount();
            var couponDiscounts           = cartOperator.GetCouponDiscounts();
            var campaignDiscount          = cartOperator.GetCampaignDiscount();
            var deliveryCost = cartOperator.GetDeliveryCost();

            var productsByCategory = cartOperator.GetProductsByCategory(fruits);
        }
Exemplo n.º 2
0
        public void Can_Add_New_Lines()
        {
            var p1 = new Product {
                ProductID = 1, Name = "P1"
            };
            var p2 = new Product {
                ProductID = 2, Name = "P2"
            };

            var cart = new ShoppingCart();

            cart.AddItem(p1, 1);
            cart.AddItem(p2, 3);

            Assert.AreEqual(2, cart.Lines.Count());
            Assert.AreEqual("P2", cart.Lines.ElementAt(1).Product.Name);
        }
Exemplo n.º 3
0
        public void Nothing_Should_Be_Added_When_0_Passed_as_Quantity_To_AddItem()
        {
            ShoppingCart cart = new ShoppingCart("TEST");
            Product      p    = new Product("SKU");

            cart.AddItem(p, 0);
            Assert.Equal(0, cart.Items.Count);
        }
Exemplo n.º 4
0
        static void Main(string[] args)
        {
            Category cat1 = new Category("Giysi");
            Category cat2 = new Category("Teknoloji");

            Product urun1 = new Product("Ayakkabı", 150, cat1);
            Product urun2 = new Product("Gömlek", 50, cat1);
            Product urun3 = new Product("T-shirt", 20, cat1);

            Product urun4 = new Product("Klavye", 50, cat2);
            Product urun5 = new Product("Mouse", 50, cat2);
            Product urun6 = new Product("Laptop", 1220, cat2);

            Campaign cmp1 = new Campaign(cat1, 10, 2, DiscountType.Rate);
            Campaign cmp2 = new Campaign(cat1, 20, 3, DiscountType.Rate);
            Campaign cmp3 = new Campaign(cat1, 10, 1, DiscountType.Amount);


            ShoppingCart _shoppingCart;

            _shoppingCart = new ShoppingCart();

            _shoppingCart.AddItem(urun1, 1);
            _shoppingCart.AddItem(urun2, 3);
            _shoppingCart.AddItem(urun3, 3);
            _shoppingCart.AddItem(urun4, 1);

            List <Campaign> listCampaing = new List <Campaign>()
            {
                cmp1, cmp2, cmp3
            };

            _shoppingCart.ApplyDiscount(listCampaing);

            Coupon kupon1 = new Coupon(500, 30, DiscountType.Rate);

            _shoppingCart.ApplyCoupon(kupon1);

            _shoppingCart.Print();


            Console.ReadKey();
            Console.ReadKey();
            Console.ReadKey();
        }
Exemplo n.º 5
0
        public void CalculateForTest()
        {
            Category category = new Category("fruit");

            Product apple  = new Product("Apple", 100, category);
            Product almond = new Product("Almond", 150, category);

            ShoppingCart cart = new ShoppingCart();

            cart.AddItem(apple, 3);
            cart.AddItem(almond, 1);

            DeliveryCostCalculater deliveryCostCalculater = new DeliveryCostCalculater(2, 3, Consts.DeliveryFixedCost);

            deliveryCostCalculater.CalculateFor(cart);

            Assert.AreEqual(8.99, cart.DeliveryCost);
        }
Exemplo n.º 6
0
        public void ApplyCouponTest()
        {
            Category category = new Category("fruit");

            Product apple  = new Product("Apple", 100, category);
            Product almond = new Product("Almond", 150, category);

            ShoppingCart cart = new ShoppingCart();

            cart.AddItem(apple, 3);
            cart.AddItem(almond, 1);

            Coupon coupon = new Coupon(100, 10, DiscountType.Amount);

            cart.ApplyCoupon(coupon);

            Assert.AreEqual(440, cart.TotalPrice);
        }
Exemplo n.º 7
0
        public void AddItemTest_GivenOneItemIsAddedTwice_ReturnOneAddedItem()
        {
            //Arrange
            var searchResult = new SearchResult("homepages/r/DawkinsRichard", "Richard Dawkins", 0,
                                                SearchResultSourceType.Person, "");

            var target = new ShoppingCart();

            //Act

            Assert.IsTrue(!target.SearchResults.Any());
            target.AddItem(searchResult);
            target.AddItem(searchResult);

            //Assert
            Assert.IsTrue(target.SearchResults.Any(s => s.Equals(searchResult)));
            Assert.IsTrue(target.SearchResults.Count() == 1);
        }
Exemplo n.º 8
0
        private object DummyEcommerceMethod2()
        {
            SKUInfo          sku             = null;
            IOrderRepository orderRepository = null;
            int orderId = 0;

            //DocSection:DisplayCatalogDiscounts
            // Initializes the needed services
            ShoppingService shoppingService = new ShoppingService();
            PricingService  pricingService  = new PricingService();

            // Gets the current shopping cart
            ShoppingCart shoppingCart = shoppingService.GetCurrentShoppingCart();

            // Calculates prices for the specified product
            ProductPrice price = pricingService.CalculatePrice(sku, shoppingCart, true, false);

            // Gets the catalog discount
            decimal catalogDiscount = price.Discount;
            //EndDocSection:DisplayCatalogDiscounts

            //DocSection:DisplayOrderList
            // Gets the current customer
            Customer currentCustomer = shoppingService.GetCurrentCustomer();

            // If the customer does not exist, returns error 404
            if (currentCustomer == null)
            {
                return(HttpNotFound());
            }

            // Creates a view model representing a collection of the customer's orders
            OrdersViewModel model = new OrdersViewModel()
            {
                Orders = orderRepository.GetByCustomerId(currentCustomer.ID)
            };
            //EndDocSection:DisplayOrderList

            //DocSection:ReorderExistingOrder
            // Gets the order based on its ID
            Order order = orderRepository.GetById(orderId);

            // Gets the current visitor's shopping cart
            ShoppingCart cart = shoppingService.GetCurrentShoppingCart();

            // Loops through the items in the order and adds them to the shopping cart
            foreach (OrderItem item in order.OrderItems)
            {
                cart.AddItem(item.SKUID, item.Units);
            }

            // Saves the shopping cart
            cart.Save();
            //EndDocSection:ReorderExistingOrder

            return(null);
        }
Exemplo n.º 9
0
        public void Cart_Item_Quantity_Should_Adjust_To_10()
        {
            ShoppingCart cart = new ShoppingCart("TEST");
            Product      p    = new Product("SKU");

            cart.AddItem(p);
            cart.AdjustQuantity(p, 10);
            Assert.Equal(10, cart.TotalItems);
        }
Exemplo n.º 10
0
        public void Items_Count_Should_Be_0_When_10_Items_Adjusted_To_Negative_10()
        {
            ShoppingCart cart = new ShoppingCart("TEST");
            Product      p    = new Product("SKU");

            cart.AddItem(p, 10);
            cart.AdjustQuantity(p, -10);
            Assert.Equal(0, cart.Items.Count);
        }
Exemplo n.º 11
0
        public void When_Items_added_to_cart_are_grouped()
        {
            //arrange
            IShoppingCart shoppingCart = new ShoppingCart(_pricelist);

            shoppingCart.AddItem("apple", 2);
            shoppingCart.AddItem("apple", 1);
            shoppingCart.AddItem("orange", 2);
            shoppingCart.AddItem("orange", 2);

            //act
            var grouped = shoppingCart.GroupShoppingItems();

            //assert
            Assert.IsTrue(grouped.Count == 2);
            Assert.IsTrue(grouped.First(i => String.Compare(i.Name, "apple", StringComparison.OrdinalIgnoreCase) == 0).Quantity == 3);
            Assert.IsTrue(grouped.First(i => String.Compare(i.Name, "orange", StringComparison.OrdinalIgnoreCase) == 0).Quantity == 4);
        }
Exemplo n.º 12
0
        public void ReturnNumberOfDeliveriesIsEqual1()
        {
            var sut     = new ShoppingCart(new DeliveryCostCalculator(1, 5));
            var product = new Product("Nike Sneaker", 299.559, new Category("Sneaker"));

            sut.AddItem(product, 1);

            Assert.AreEqual(sut.NumberOfDeliveries, 1);
        }
Exemplo n.º 13
0
        public async Task <IActionResult> Index()
        {
            //var people = _repo.GetPeople();
            //var products = await _repo.GetProducts();
            //IndexViewModel vm = new IndexViewModel();
            //vm.People = people;
            //vm.Products = products;
            //return View(vm);
            HttpContext.Response.Cookies.Append("uid", "1245");
            var          cookie = HttpContext.Request.Cookies["uid"];
            ShoppingCart cart   = new ShoppingCart();

            cart.AddItem("item1");
            cart.AddItem("item2");
            Utils.StoreInSession(cart, HttpContext.Session);

            return(View(await _repo.GetProducts()));
        }
        public void ShouldExecuteScenarioC()
        {
            ShoppingCart sc = new ShoppingCart();
            Item         a  = new Item("A", 50.00);
            Item         b  = new Item("B", 30.00);
            Item         c  = new Item("C", 20.00);
            Item         d  = new Item("D", 15.00);

            sc.AddItem(a, 3);
            sc.AddItem(b, 5);
            sc.AddItem(c);
            sc.AddItem(d);
            List <IPromotion>      promoList   = this.GetAvailablePromotions(a, b, c, d);
            Dictionary <Item, int> cartDetails = sc.GetCartDetails();

            Assert.NotNull(cartDetails);
            Assert.Equal(280, sc.GetCartTotalWithPromotion(promoList));
        }
Exemplo n.º 15
0
        public void TestScenario3()
        {
            var cart = new ShoppingCart(_taxAssessor, _roundingStrategy);

            cart.AddItem(getProduct("imported bottle of perfume", 27.99m), 1);
            cart.AddItem(getProduct("bottle of perfume"), 1);
            cart.AddItem(getProduct("packet of headache pills"), 1);
            cart.AddItem(getProduct("box of imported chocolates"), 2);
            var items = cart.GetLineItems().ToList();

            Assert.AreEqual(4, items.Count());
            Assert.AreEqual("imported bottle of perfume: 32.19", items[0].PrintableMessage);
            Assert.AreEqual("bottle of perfume: 20.89", items[1].PrintableMessage);
            Assert.AreEqual("packet of headache pills: 9.75", items[2].PrintableMessage);
            Assert.AreEqual("box of imported chocolates: 23.70 (2 @ 11.85)", items[3].PrintableMessage);
            Assert.AreEqual(7.30m, cart.GetTotalSalesTax());
            Assert.AreEqual(86.53m, cart.GetGrandTotal());
        }
Exemplo n.º 16
0
        public void ShouldReturnOkAndSaveWhenSaveOrderWithInventoryChecking()
        {
            // Arrange
            var targetShoppingCartSaved = false;
            var controller = new OrdersController(
                null,
                new StubIShoppingCartRepository
            {
                GetShoppingCartString = id =>
                {
                    var cart = new ShoppingCart(id);
                    cart.AddItem(new ShoppingCartItem {
                        ProductName = "name", ProductPrice = 1m, Quantity = 1
                    });
                    return(cart);
                },
                SaveShoppingCartShoppingCart = cart =>
                {
                    targetShoppingCartSaved = true;
                    return(cart);
                }
            },
                new StubIPersonRepository
            {
                GetPersonGuid = id =>
                {
                    var person = new Person();
                    person.AddAddress(new Address {
                        Id = 13, AddressLine1 = "street", City = "city", PostalCode = "pcode"
                    });
                    person.AddCreditCard(new CreditCard {
                        Id = 29, CardNumber = "1234567890123456", CardType = "type", ExpMonth = 11, ExpYear = 2019
                    });
                    return(person);
                }
            },
                new StubIProductRepository
            {
                GetProductInt32 = id => new Product()
            },
                null,
                new StubIInventoryService
            {
                InventoryAndPriceCheckShoppingCart = cart => true
            });

            SetupControllerForTests(controller);

            // Act
            var response = controller.Post(new OrderInfo {
                CartId = Guid.NewGuid().ToString(), ShippingAddressId = 13, BillingAddressId = 13, CreditCardId = 29
            });

            // Assert
            Assert.IsTrue(targetShoppingCartSaved);
            Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
        }
Exemplo n.º 17
0
        public void Can_Add_Existing_Product()
        {
            var p1 = new Product {
                ProductID = 1, Name = "P1"
            };
            var p2 = new Product {
                ProductID = 2, Name = "P2"
            };

            var cart = new ShoppingCart();

            cart.AddItem(p1, 1);
            cart.AddItem(p2, 3);
            cart.AddItem(p1, 6);

            Assert.AreEqual(2, cart.Lines.Count());
            Assert.AreEqual(7, cart.Lines.ElementAt(0).Quantity);
        }
Exemplo n.º 18
0
        public void ShoppingCartShouldReturnDeliveryCost()
        {
            Category category  = new Category("Food");
            Category category2 = new Category("Toys");
            Category category3 = new Category("Drink");

            Product apple = new Product("Apple", 10.0, category);
            Product rcCar = new Product("Rc Car", 1.0, category2);
            Product milk  = new Product("Milk", 1.5, category3);

            ShoppingCart shoppingCart = new ShoppingCart();

            shoppingCart.AddItem(apple, 10);
            shoppingCart.AddItem(rcCar, 3);
            shoppingCart.AddItem(milk, 5);

            Assert.Equal(122.99, shoppingCart.GetDeliveryCost());
        }
Exemplo n.º 19
0
        public void CampaignAndCouponShouldBeApplied()
        {
            ShoppingCart cart     = new ShoppingCart(1, 1);
            Category     category = new Category("Nike Ayakkabýlar");

            cart.AddItem(new Product("Nike Ayakkabý - 1", 100, category), 1);
            cart.AddItem(new Product("Nike Ayakkabý - 2", 200, category), 2);
            cart.AddItem(new Product("Nike Ayakkabý - 3", 300, category), 3);

            Campaign campaign = new Campaign(category, 10, 3, Shopping_Cart.Domains.Discount.Enums.DiscountType.Rate);
            Coupon   coupon   = new Coupon(600, Shopping_Cart.Domains.Discount.Enums.DiscountType.Rate, 10);

            cart.ApplyDiscounts(campaign);
            cart.ApplyCoupon(coupon);

            cart.AppliedCampaigns.Count.Should().Be(1);
            cart.AppliedCoupons.Count.Should().Be(1);
        }
Exemplo n.º 20
0
        public void AddTocart_WillAddItemsToShoppingCart(string itemName, string category, decimal price)
        {
            ShoppingCart cart = new ShoppingCart();

            cart.AddItem(new Product(itemName, category, price), 1);

            int cSize = cart.Cart.Count;

            Assert.Equal(1, cSize);
        }
Exemplo n.º 21
0
        public void ShoppingCartShouldNotAppylDiscountWhenDiscountTypeIsNone()
        {
            Category category  = new Category("Food");
            Category category2 = new Category("Toys");

            Product apple = new Product("Apple", 10.0, category);
            Product rcCar = new Product("Rc Car", 1.0, category2);

            ShoppingCart shoppingCart = new ShoppingCart();

            shoppingCart.AddItem(apple, 5);
            shoppingCart.AddItem(rcCar, 3);

            Coupon coupon = new Coupon(50, 10, DiscountType.None);

            shoppingCart.ApplyCoupon(coupon);

            Assert.Equal(0, shoppingCart.GetCouponDiscount());
        }
Exemplo n.º 22
0
        public void ShoppingCartShouldNotAddNullProducts()
        {
            Product product = null;

            ShoppingCart shoppingCart = new ShoppingCart();

            shoppingCart.AddItem(product, 5);

            Assert.Empty(shoppingCart.Items);
        }
Exemplo n.º 23
0
        public void ShoppingCartShouldNotAppylDiscountForCouponWhenTotalAmountLessThanMinimumAmount()
        {
            Category category  = new Category("Food");
            Category category2 = new Category("Toys");

            Product apple = new Product("Apple", 10.0, category);
            Product rcCar = new Product("Rc Car", 1.0, category2);

            ShoppingCart shoppingCart = new ShoppingCart();

            shoppingCart.AddItem(apple, 5);
            shoppingCart.AddItem(rcCar, 3);

            Coupon coupon = new Coupon(100, 10, DiscountType.Amount);

            shoppingCart.ApplyCoupon(coupon);

            Assert.Equal(0, shoppingCart.GetCouponDiscount());
        }
Exemplo n.º 24
0
        public void AddItem_ShouldReturnsOneCartItem_WhenAddingToNewShoppingCart()
        {
            var shoppingCart = new ShoppingCart(Guid.NewGuid());
            var product      = new Product(Guid.NewGuid(), "Test", new Price("TRY", 55.55));
            var cartItem     = new CartItem(Guid.NewGuid(), product, 5);

            shoppingCart.AddItem(cartItem, 10);

            Assert.Single(shoppingCart.CartItems);
        }
Exemplo n.º 25
0
        public void AddItem_ShouldAddAnItemToTheCartItemsList()
        {
            //Arrange
            var shoppingCart = new ShoppingCart();

            //Act
            shoppingCart.AddItem(StubCartItem(Tests.FAKE_JEWELRY_REPOSITORY_FIRST_ITEM_ID, 1234, CartItemType.Jewelry));
            //Assert
            shoppingCart.Items.Should().HaveCount(1);
        }
        public RedirectToActionResult AddToCart(int productId, string returnUrl)
        {
            var album = _myContext.Albums.FirstOrDefault(a => a.AlbumId == productId);

            if (album != null)
            {
                _cartService.AddItem(album, 1);
            }
            return(RedirectToAction("Index", new { returnUrl }));
        }
Exemplo n.º 27
0
        public void CouponShouldBeApplied()
        {
            ShoppingCart cart = new ShoppingCart(1, 1);

            cart.AddItem(new Product("Nike Ayakkabý", 300, new Category("Nike Ayakkabýlar")), 2);
            Coupon coupon = new Coupon(150, Shopping_Cart.Domains.Discount.Enums.DiscountType.Rate, 10);

            cart.ApplyCoupon(coupon);
            cart.AppliedCoupons.Count.Should().Be(1);
        }
        public void ShouldAddMultipleItemsToCart()
        {
            ShoppingCart sc = new ShoppingCart();

            sc.AddItem(new Item("A", 50.00), 3);
            Dictionary <Item, int> cartDetails = sc.GetCartDetails();

            Assert.NotNull(cartDetails);
            Assert.Equal(3, cartDetails[new Item("A", 50.00)]);
        }
Exemplo n.º 29
0
        public IActionResult Add(int id, decimal price, int number, ShoppingCart shoppingCart)
        {
            Items items = _itemsRepository.GetByItemsId(id);

            if (items != null)
            {
                shoppingCart.AddItem(items, number, price);
            }
            return(RedirectToAction(nameof(Index)));
        }
Exemplo n.º 30
0
        public RedirectToRouteResult AddToCart(ShoppingCart cart, int productId, string returnUrl)
        {
            var product = repository.Products.FirstOrDefault(p => p.ProductID == productId);

            if (product != null)
            {
                cart.AddItem(product, 1);
            }
            return(RedirectToAction("Index", new { returnUrl }));
        }
Exemplo n.º 31
0
        public void AssignPricingToShoppingCartItemsTest_pricingAssignedToLineItem() {
            string[] pricings = new string[] { "Apple, 1.00" };
            string[] promotions = new string[0];
            
            Item item = new Item("Apple");
            ShoppingCart cart = new ShoppingCart();
            cart.AddItem(item);
            LineItem lineItem = cart.GetLineItemForItem(item);

            Assert.AreEqual(0, lineItem.PricePerUnit);

            PricingController pricingController = new PricingController(pricings, promotions);
            pricingController.AssignPricingToShoppingCartItems(cart);

            Assert.AreEqual(1.00, lineItem.PricePerUnit);
        }
Exemplo n.º 32
0
        public void AssignPromotionsToShoppingCartItemsTest_promotionNotFoundForItem_noPromotionAssigned() {
            string[] pricings = new string[] { "Apple, 1.00" };
            string[] promotions = new string[] { "Bananas, QtyPromoPrice, [email protected]" };

            Item item = new Item("Apple");
            ShoppingCart cart = new ShoppingCart();
            cart.AddItem(item);
            LineItem lineItem = cart.GetLineItemForItem(item);

            Assert.IsNull(lineItem.Promotion);

            PricingController pricingController = new PricingController(pricings, promotions);
            pricingController.AssignPromotionsToShoppingCartItems(cart);

            Assert.IsNull(lineItem.Promotion);            
        }
 public void DoesPromotionApply_cartContainsItem_greaterThanQuantityRequired_returnsTrue() {
     ShoppingCart cart = new ShoppingCart();
     cart.AddItem(GetTestItem(), 3);
     BuyGetFreePromotion promotion = GetTestBuyGetFreePromotion();
     Assert.IsTrue(promotion.DoesPromotionApply(cart));
 }
 public void DoesPromotionApply_cartContainsItem_notEnoughQuantityRequired_returnsFalse() {
     ShoppingCart cart = new ShoppingCart();
     cart.AddItem(GetTestItem(), 1);
     BuyGetFreePromotion promotion = GetTestBuyGetFreePromotion();
     Assert.IsFalse(promotion.DoesPromotionApply(cart));
 }
 public void DoesPromotionApply_cartContainsItem_exactQuantityRequired_returnsTrue() {
     ShoppingCart cart = new ShoppingCart();
     cart.AddItem(GetTestItem(), 2);
     BuyGetDiscountPromotion promotion = GetTestBuyGetDiscountPromotion();
     Assert.IsTrue(promotion.DoesPromotionApply(cart));
 }