Beispiel #1
0
        public ShoppingBasket CreateBasketFromInput(string[] args, ProductsDL productsDL)
        {
            ShoppingBasket shoppingBasket = new ShoppingBasket();

            // check data is valid first
            if (productsDL == null)
            {
                throw new NoProductToBuyException("no products to purchase");
            }
            if (args == null || args.Length == 0)
            {
                return(shoppingBasket);
            }

            // read arguments
            foreach (var arg in args)
            {
                string  argToLower = arg.Trim().ToLower(CultureInfo.InvariantCulture);
                Product product    = productsDL.GetProductByName(name: argToLower);
                if (product != null)
                {
                    shoppingBasket.AddItem(product);
                }
                else
                {
                    throw new InvalidInputException($"{argToLower} is not a valid product");
                }
            }
            return(shoppingBasket);
        }
Beispiel #2
0
 internal int Delete()
 {
     try
     {
         using (ProductsDL _productsdlDL = new ProductsDL())
         {
             return(_productsdlDL.Delete(this));
         }
     }
     catch (Exception)
     {
         throw;
     }
 }
Beispiel #3
0
 internal int InsertAndGetId()
 {
     try
     {
         using (ProductsDL _productsdlDL = new ProductsDL())
         {
             return(_productsdlDL.InsertAndGetId(this));
         }
     }
     catch (Exception)
     {
         throw;
     }
 }
Beispiel #4
0
        public static SpecialOffersDL GetAppleBeansBreadSpecialOffers(ProductsDL productsDL)
        {
            // 10 % off apples
            SpecialOfferRule apples10percentOff = new SpecialOfferRule(new SpecialOfferMatchRule(productsDL.GetProductByName("apple"), 1),
                                                                       new SpecialOfferDiscountRule(productsDL.GetProductByName("apple"), 10));

            // buy 2 cans of beans, get 1 loaf of bread 50% off
            SpecialOfferRule twoCans1BreadLoaf = new SpecialOfferRule(new SpecialOfferMatchRule(productsDL.GetProductByName("beans"), 2),
                                                                      new SpecialOfferDiscountRule(productsDL.GetProductByName("bread"), 50, 1));
            SpecialOffersDL specialOffers = new SpecialOffersDL(new List <SpecialOfferRule>()
            {
                apples10percentOff, twoCans1BreadLoaf
            });

            return(specialOffers);
        }
Beispiel #5
0
        public static SpecialOffersDL GetSpecialOffers3oranges1applefree(ProductsDL products)
        {
            // buy 90 % off strawberries
            SpecialOfferRule apples10percentOff = new SpecialOfferRule(
                new SpecialOfferMatchRule(products.GetProductByName("Strawberries"), 1),
                new SpecialOfferDiscountRule(products.GetProductByName("Strawberries"), 90));

            // buy 3 oranges, get 1 apple for free
            SpecialOfferRule threeOrangesOneAppleFree = new SpecialOfferRule(
                new SpecialOfferMatchRule(products.GetProductByName("oranges"), 3),
                new SpecialOfferDiscountRule(products.GetProductByName("apple"), 100, 1));

            SpecialOffersDL specialOffers = new SpecialOffersDL(new List <SpecialOfferRule>()
            {
                apples10percentOff, threeOrangesOneAppleFree
            });

            return(specialOffers);
        }
Beispiel #6
0
        static void Main(string[] args)
        {
            try
            {
                // Create Product List for example in paper
                ProductsDL productsDL = new ProductsDL(new List <Product>()
                {
                    new Product(1, "Beans", 0.65m),
                    new Product(2, "Bread", 0.80m),
                    new Product(3, "Milk", 1.30m),
                    new Product(4, "Apple", "Apples", 1.00m)
                });

                // create special offers
                // Offer 1: 10% off apples
                SpecialOfferRule apples10percentOff = new SpecialOfferRule(
                    new SpecialOfferMatchRule(productsDL.GetProductByName("apple"), 1),
                    new SpecialOfferDiscountRule(productsDL.GetProductByName("apple"), 10));

                // Offer 2: Buy 2 cans of beans get 1 loaf of bread 50 percent off
                SpecialOfferRule twoCans1BreadLoaf = new SpecialOfferRule(
                    new SpecialOfferMatchRule(productsDL.GetProductByName("beans"), 2),
                    new SpecialOfferDiscountRule(productsDL.GetProductByName("bread"), 50, 1));
                SpecialOffersDL specialOffersDL = new SpecialOffersDL(new List <SpecialOfferRule>()
                {
                    apples10percentOff, twoCans1BreadLoaf
                });

                // process user imput with products and special offers
                IAppMain appMain = new AppMain(productsDL, specialOffersDL);
                string   output  = appMain.Process(args);
                Console.WriteLine(output);
            }
            catch (Exception ex)
            {
                // log out error not just to console.
                Console.WriteLine("Fatal Error occured: ", ex);
            }
        }
Beispiel #7
0
 public AppMain(ProductsDL productsDL,
                SpecialOffersDL specialOffersDL)
 {
     _productsDL      = productsDL;
     _specialOffersDL = specialOffersDL;
 }