// GET: Store/Checkout
        public ActionResult Checkout()
        {
            // If the user has not logged in yet, make them log in
            if (Session[SessionKeys.UserId] == null)
            {
                return(RedirectToAction("Login", "User"));
            }

            //Get their active shopping cart
            ShoppingCart cart = GetActiveShoppingCart();

            //If they don't have any items, redirect them to ViewCart
            if (cart.Items.Count == 0)
            {
                return(RedirectToAction("ViewCart"));
            }

            //Create an Order Model that sums up the item totals and weights
            Order model = new Order()
            {
                SubTotal = cart.SubTotal,
                Weight   = cart.Items.Sum(m => m.Weight),
            };

            //Set the shipping rates to use for the model
            model.ShippingRates = ShippingCalculator.GetShippingRates(model.Weight);

            return(View("Checkout", model));
        }
        public void Returns_15_PriceBasedPriceRates()
        {
            Setup();
            //Price based rate returns 15.00m , Weight based rate returns 10.00m , API returns 0.00m
            var ExpectedValue = 15.00m;
            var orderline     = new SalesOrderLine();

            orderline.Price          = 300.00m;
            orderline.Quantity       = 2;
            orderline.Product        = new Product();
            orderline.Product.ID     = 11;
            orderline.Product.Name   = "Genuine Side Mirrors Honda";
            orderline.Product.Type   = ProductType.Physical;
            orderline.Product.Weight = 2.5m;
            //First Product added
            order.Lines.Add(orderline);

            orderline              = new SalesOrderLine();
            orderline.Price        = 410.00m;
            orderline.Quantity     = 1;
            orderline.Product      = new Product();
            orderline.Product.ID   = 12;
            orderline.Product.Name = "Side Mirror Installation";
            orderline.Product.Type = ProductType.Service;
            //orderline.Product.Weight = 5.5m;
            //Second Product added
            order.Lines.Add(orderline);


            ShippingCalculator shippingCalculator = new ShippingCalculator();
            var price = shippingCalculator.Calculate(order);

            Assert.AreEqual(ExpectedValue, price);
        }
예제 #3
0
        public void WithBasketTotalLessThanTwenty_CheckShippingCost()
        {
            var shippingCalculator = new ShippingCalculator(19);
            var shippingCost       = shippingCalculator.Calculate();

            Assert.AreEqual(7m, shippingCost);
        }
        public void Returns_20_WeightBasedPriceRates()
        {
            Setup();
            //Weight based rate returns 20.00m while Price based returns 0.00m. API returns null
            var ExpectedValue = 20.00m;
            var orderline     = new SalesOrderLine();

            orderline.Price          = 2.00m;
            orderline.Quantity       = 12;
            orderline.Product        = new Product();
            orderline.Product.ID     = 4;
            orderline.Product.Name   = "Charging Cable";
            orderline.Product.Type   = ProductType.Physical;
            orderline.Product.Weight = 0.50m;
            //First Product added
            order.Lines.Add(orderline);

            orderline                = new SalesOrderLine();
            orderline.Price          = 15.00m;
            orderline.Quantity       = 2;
            orderline.Product        = new Product();
            orderline.Product.ID     = 6;
            orderline.Product.Name   = "Charging Socket Replacement Part";
            orderline.Product.Type   = ProductType.Physical;
            orderline.Product.Weight = 24.50m;
            //Second Product added
            order.Lines.Add(orderline);

            ShippingCalculator shippingCalculator = new ShippingCalculator();
            var price = shippingCalculator.Calculate(order);

            Assert.AreEqual(ExpectedValue, price);
        }
예제 #5
0
        static void Main(string[] args)
        {
            // Observer Example
            var customer  = new Customer("Eliot Anderson");
            var customer2 = new Customer("Andrei Keks");
            var shop      = new OnlineShop();
            var mobile    = new Product("Xiaomi MI 9", 600);

            shop.Subscribe(customer);
            shop.Subscribe(customer2);
            shop.AddProduct(mobile);

            // Template pattern example
            var mkvVideo = new MkvVideoPlayer();
            var mp4Video = new MP4VideoPlayer();

            mkvVideo.PlayVideo("funny-cats.mkv");
            mp4Video.PlayVideo("funny-dogs.mp4");


            // Strategy pattern
            var order           = new Order(126);
            var calculator      = new ShippingCalculator();
            var shippingCostDHL = calculator.CalculateShipping(new DHLShippingStrategy(), order);
            var shippingCostUPS = calculator.CalculateShipping(new UpsShippingStrategy(), order);

            Console.WriteLine($"Shipping prices:\n  DHL price: {shippingCostDHL}$\n  UPS Price: {shippingCostUPS}$");
            Console.ReadLine();
        }
예제 #6
0
        public void WithBasketTotalMoreThanFourty_CheckShippingCost()
        {
            var shippingCalculator = new ShippingCalculator(40);
            var shippingCost       = shippingCalculator.Calculate();

            Assert.AreEqual(0m, shippingCost);
        }
        public void ReturnsEqualPriceRates()
        {
            Setup();
            //Price based rate returns 10.00m and Weight based  rate returns 10.00m
            var ExpectedValue = 10.00m;
            var orderline     = new SalesOrderLine();

            orderline.Price          = 240.00m;
            orderline.Quantity       = 1;
            orderline.Product        = new Product();
            orderline.Product.ID     = 1;
            orderline.Product.Name   = "GPS Navigation System";
            orderline.Product.Type   = ProductType.Physical;
            orderline.Product.Weight = 4.30m;
            //First Product added
            order.Lines.Add(orderline);
            orderline                = new SalesOrderLine();
            orderline.Price          = 120.00m;
            orderline.Quantity       = 1;
            orderline.Product        = new Product();
            orderline.Product.ID     = 2;
            orderline.Product.Name   = "Nav Installation";
            orderline.Product.Type   = ProductType.Service;
            orderline.Product.Weight = 0.00m;
            //Second Product added
            order.Lines.Add(orderline);
            ShippingCalculator shippingCalculator = new ShippingCalculator();
            var price = shippingCalculator.Calculate(order);

            Assert.AreEqual(ExpectedValue, price);
        }
        public void Returns_0_ZeroProducts()
        {
            Setup();
            ShippingCalculator shippingCalculator = new ShippingCalculator();
            var price = shippingCalculator.Calculate(order);

            Assert.AreEqual(0.00m, price);
        }
예제 #9
0
        public void Calculates_Shipping_Cost_For_International(Cart cart, double low, double high)
        {
            var sut = new ShippingCalculator();

            var result = sut.CalculateShippingCost(cart);

            result.Should().BeInRange(low, high);
        }
예제 #10
0
        public void When_Shipping_Via_FedEx_The_Cost_Is_5()
        {
            // Arrange
            var order = new Order(ShippingOptions.FedEx);

            // Act
            var cost = new ShippingCalculator().CalculateCost(order);

            Assert.AreEqual(5.00d, cost);
        }
예제 #11
0
        public void When_Shipping_Heavier_Product_Via_FedEx_The_Cost_Is_55()
        {
            // Arrange
            var order = new Order(ShippingOptions.FedEx, 310);

            // Act
            var cost = new ShippingCalculator().CalculateCost(order);

            Assert.AreEqual(5.5d, cost);
        }
예제 #12
0
        public void When_Shipping_Heavier_Product_Via_UPS_The_Cost_Is_4675()
        {
            // Arrange
            var order = new Order(ShippingOptions.UPS, 410);

            // Act
            var cost = new ShippingCalculator().CalculateCost(order);

            Assert.AreEqual(4.675d, Math.Round(cost, 3));
        }
예제 #13
0
        public void When_Shipping_Via_USPS_The_Cost_Is_300()
        {
            // Arrange
            var order = new Order(ShippingOptions.USPS);

            // Act
            var cost = new ShippingCalculator().CalculateCost(order);

            Assert.AreEqual(3.00d, cost);
        }
예제 #14
0
        public void When_Shipping_Book_Via_USPS_The_Cost_is_27()
        {
            // Arrange
            var order = new Order(ShippingOptions.USPS, 0d, ProductType.Book);

            // Act
            var cost = new ShippingCalculator().CalculateCost(order);

            Assert.AreEqual(2.7d, cost);
        }
 public void CanExtendComponentsRegisteredInFacilities()
 {
     using (IWindsorContainer container = new RhinoContainer())
     {
         string path = Path.GetFullPath(@"Binsor\PreRegistrationWithExtensions.boo");
         BooReader.Read(container, path);
         ShippingCalculator calculator = container.Resolve <ShippingCalculator>();
         Assert.AreEqual(120m, calculator.ShippingCost);
     }
 }
예제 #16
0
        public void When_Shipping_Via_UPS_The_Shipping_Cost_Is_425()
        {
            // Arrange
            var order = new Order(ShippingOptions.UPS);

            // Act
            var cost = new ShippingCalculator(new UpsStrategy()).CalculateCost(order);

            Assert.AreEqual(4.25d, cost);
        }
        public void CalculateShippingCost_NoItems_Return0(Address source, Address destination)
        {
            var target = new ShippingCalculator(source);

            var cart = new CartBuilder()
                       .WithShippingAddress(destination)
                       .Build();

            var result = target.CalculateShippingCost(cart);

            Assert.Equal(0, result);
        }
        public void BasketShippingTotalTest()
        {
            var perRegionShippingOption = new PerRegionShipping()
            {
                PerRegionCosts = new[]
                {
                    new RegionShippingCost()
                    {
                        DestinationRegion =
                            RegionShippingCost.Regions.UK,
                        Amount = .75m
                    },
                    new RegionShippingCost()
                    {
                        DestinationRegion =
                            RegionShippingCost.Regions.Europe,
                        Amount = 1.5m
                    }
                },
            };

            var flatRateShippingOption = new FlatRateShipping {
                FlatRate = 1.1m
            };

            var basket = new Basket()
            {
                LineItems = new List <LineItem>
                {
                    new LineItem()
                    {
                        DeliveryRegion = RegionShippingCost.Regions.UK,
                        Shipping       = perRegionShippingOption
                    },
                    new LineItem()
                    {
                        DeliveryRegion = RegionShippingCost.Regions.Europe,
                        Shipping       = perRegionShippingOption
                    },
                    new LineItem()
                    {
                        Shipping = flatRateShippingOption
                    },
                }
            };

            var calculator = new ShippingCalculator();

            decimal basketShipping = calculator.CalculateShipping(basket);

            Assert.That(basketShipping, Is.EqualTo(3.35m));
        }
        public void CalculateShippingCost_StandardShippingOneItemsQuantity1_Return1TimesRate(
            Address source, Address destination, double rate)
        {
            var target = new ShippingCalculator(source);

            var cart = new Cart
            {
                CustomerType   = CustomerType.Standard,
                ShippingMethod = ShippingMethod.Standard,
                Items          = new List <Item>
                {
                    new() { Quantity = 1 }
                },
        public void CalculateShippingCost_NoItems_Return0(Address source, Address destination)
        {
            // We could have used auto mocking container here as well, however at the moment this initialization is simple as it is
            var target = new ShippingCalculator(source);

            var cart = new CartBuilder()
                       .WithShippingAddress(destination)
                       .Build();

            var result = target.CalculateShippingCost(cart);

            Assert.Equal(0, result);
        }
예제 #21
0
        public void DiscountShippingOptionTest()
        {
            var discountShippingOption = new DiscountShipping()
            {
                PerRegionCosts = new[]
                {
                    new RegionShippingCost()
                    {
                        DestinationRegion =
                            RegionShippingCost.Regions.UK,
                        Amount = .75m
                    },
                    new RegionShippingCost()
                    {
                        DestinationRegion =
                            RegionShippingCost.Regions.Europe,
                        Amount = 1.5m
                    }
                },
            };

            var basket = new Basket()
            {
                LineItems = new List <LineItem>
                {
                    new LineItem()
                    {
                        DeliveryRegion = RegionShippingCost.Regions.UK,
                        Shipping       = discountShippingOption,
                        SupplierId     = 99
                    },
                    new LineItem()
                    {
                        DeliveryRegion = RegionShippingCost.Regions.UK,
                        Shipping       = discountShippingOption,
                        SupplierId     = 99
                    },
                }
            };

            var calculator = new ShippingCalculator();

            decimal basketShipping = calculator.CalculateShipping(basket);

            var shippingAmount = discountShippingOption.GetAmount(basket.LineItems[0], basket);

            Assert.That(shippingAmount, Is.EqualTo(.25m));

            shippingAmount = discountShippingOption.GetAmount(basket.LineItems[1], basket);
            Assert.That(shippingAmount, Is.EqualTo(.25m));
        }
        public ActionResult Checkout(Order order)
        {
            //Get their active shopping cart
            ShoppingCart cart = GetActiveShoppingCart();

            order.SubTotal = cart.SubTotal;
            order.Weight   = cart.Items.Sum(i => i.Weight);

            if (!ModelState.IsValid)
            {
                order.ShippingRates = ShippingCalculator.GetShippingRates(order.Weight);
                return(View("Checkout", order));
            }

            //Update the order with fields from the customer's checkout
            order.Tax      = cart.SubTotal * TaxCalculator.GetTaxRate(order.BillingPostalCode);
            order.Shipping = ShippingCalculator.GetShippingRates(order.Weight)[order.ShippingType];
            order.UserId   = (int)Session[SessionKeys.UserId];

            //Create a transaction to save these in the database
            //We want to make sure that the order is saved, along with each order item
            using (TransactionScope tran = new TransactionScope())
            {
                //Save the Order
                orderDal.SaveOrder(order);

                //Convert each "ShopingCartItem" to an "OrderItem" and give it the new order id
                List <OrderItem> orderItems = cart.Items.Select(c => new OrderItem
                {
                    Price     = c.Price,
                    ProductId = c.Product.Id,
                    Quantity  = c.Quantity,
                    OrderId   = order.Id
                }).ToList();

                //Save the Order Items
                orderItemDal.SaveOrderItems(orderItems);

                //Empty the shopping cart
                Session.Remove(SessionKeys.Cart);

                //Save the transaction
                tran.Complete();
            }

            return(RedirectToAction("Confirm", new { orderId = order.Id }));
        }
        public void Returns_ZeroforNegativeValues()
        {
            Setup();

            var ExpectedValue = 0.00m;
            var orderline     = new SalesOrderLine();

            orderline.Price        = -1.00m;
            orderline.Quantity     = 4;
            orderline.Product      = new Product();
            orderline.Product.ID   = 15;
            orderline.Product.Name = "Affiliates Credit";
            orderline.Product.Type = ProductType.NonPhysical;
            //First Product added
            order.Lines.Add(orderline);
            ShippingCalculator shippingCalculator = new ShippingCalculator();
            var price = shippingCalculator.Calculate(order);

            Assert.AreEqual(ExpectedValue, price);
        }
        public ShoppingCartControllerTest()
        {
            MongoSetup.Start();
            ShippingCalculator shippingCalculator = new ShippingCalculator();

            _mongoUtility = new MongoUtility();
            var settings = _mongoUtility.RetrieveDatabaseSettings();

            _mongoUtility.CreateDatabase("ShoppingCartDatabaseSettings");
            _databaseName = settings.DatabaseName;

            var config = _mongoUtility.InitMapper();
            ShoppingCartRepository shoppingCartRepository = new ShoppingCartRepository(settings);

            _shoppingCartManager =
                new ShoppingCartManager(
                    shoppingCartRepository,
                    new AddressValidator(),
                    config.CreateMapper(),
                    new CheckOutEngine(shippingCalculator, config.CreateMapper()));
        }
        public void Returns_5_5_PriceBasedPriceRates()
        {
            Setup();
            //Price based rate returns 5.50m , Weight based rate returns 0.00m , API returns 0.00m
            var ExpectedValue = 5.50m;
            var orderline     = new SalesOrderLine();

            orderline.Price          = 50.00m;
            orderline.Quantity       = 1;
            orderline.Product        = new Product();
            orderline.Product.ID     = 7;
            orderline.Product.Name   = "Cables & wires";
            orderline.Product.Type   = ProductType.Physical;
            orderline.Product.Weight = 0.5m;
            //First Product added
            order.Lines.Add(orderline);
            ShippingCalculator shippingCalculator = new ShippingCalculator();
            var price = shippingCalculator.Calculate(order);

            Assert.AreEqual(ExpectedValue, price);
        }
        public void Returns_0_PriceBasedPriceRates()
        {
            Setup();
            //Price based rate returns 0.00m , Weight based rate returns 0.00m , API returns 0.00m
            var ExpectedValue = 0.00m;
            var orderline     = new SalesOrderLine();

            orderline.Price        = 55.99m;
            orderline.Quantity     = 1;
            orderline.Product      = new Product();
            orderline.Product.ID   = 8;
            orderline.Product.Name = "Installation";
            orderline.Product.Type = ProductType.Service;
            //orderline.Product.Weight = 0.5m;
            //First Product added
            order.Lines.Add(orderline);
            ShippingCalculator shippingCalculator = new ShippingCalculator();
            var price = shippingCalculator.Calculate(order);

            Assert.AreEqual(ExpectedValue, price);
        }
        public void Returns_10_WeightBasedPriceRates()
        {
            Setup();
            //Weight based rate returns 10.00m while Price based returns 0.00m. API returns null
            var ExpectedValue = 10.00m;
            var orderline     = new SalesOrderLine();

            orderline.Price          = 51.00m;
            orderline.Quantity       = 1;
            orderline.Product        = new Product();
            orderline.Product.ID     = 3;
            orderline.Product.Name   = "Portable GPS";
            orderline.Product.Type   = ProductType.Physical;
            orderline.Product.Weight = 2.50m;
            //First Product added
            order.Lines.Add(orderline);
            ShippingCalculator shippingCalculator = new ShippingCalculator();
            var price = shippingCalculator.Calculate(order);

            Assert.AreEqual(ExpectedValue, price);
        }
        public void Returns_39_56_APIBasedPriceRates()
        {
            Setup();
            //Price based rate returns 15.00m , Weight based rate returns 20.00m , API returns 39.56m
            var ExpectedValue = 39.56m;
            var orderline     = new SalesOrderLine();

            orderline.Price          = 300.00m;
            orderline.Quantity       = 4;
            orderline.Product        = new Product();
            orderline.Product.ID     = 14;
            orderline.Product.Name   = "Car Tyres";
            orderline.Product.Type   = ProductType.Physical;
            orderline.Product.Weight = 8;
            //First Product added
            order.Lines.Add(orderline);
            ShippingCalculator shippingCalculator = new ShippingCalculator();
            var price = shippingCalculator.Calculate(order);

            Assert.AreEqual(ExpectedValue, price);
        }
        public void Returns_23_750_APIBasedPriceRates()
        {
            Setup();
            //Price based rate returns 10.00m , Weight based rate returns 20.00m , API returns 23.75m
            var ExpectedValue = 23.750m;
            var orderline     = new SalesOrderLine();

            orderline.Price          = 250.00m;
            orderline.Quantity       = 1;
            orderline.Product        = new Product();
            orderline.Product.ID     = 13;
            orderline.Product.Name   = "Car Front and Back Mirror";
            orderline.Product.Type   = ProductType.Physical;
            orderline.Product.Weight = 15;
            //First Product added
            order.Lines.Add(orderline);
            ShippingCalculator shippingCalculator = new ShippingCalculator();
            var price = shippingCalculator.Calculate(order);

            Assert.AreEqual(ExpectedValue, price);
        }
예제 #30
0
 public OrderProcessor()
 {
     _shippingcalculator = new ShippingCalculator();
 }