コード例 #1
0
        public static string ListActiveOffers(string userInput)
        {
            var    allOffers        = new SpecialOfferRepository().GetAllOffers();
            var    userInputAsArray = userInput.ToUpper().Split(' ');
            double discount         = 0;
            string message          = null;

            for (int i = 1; i < userInputAsArray.Length; i++)
            {
                if (allItems.Find(x => x.ItemName == userInputAsArray[i]) != null)
                {
                    if (allOffers.Find(x => x.ItemOnSpecialOffer == userInputAsArray[i]) != null)
                    {
                        var currentOffer = allOffers.Find(x => x.ItemOnSpecialOffer == userInputAsArray[i]);
                        if (currentOffer.DependentItem == null)
                        {
                            discount = currentOffer.SpecialOfferDiscount;
                            TextInfo myTI = new CultureInfo("en-GB", false).TextInfo;
                            message += myTI.ToTitleCase(currentOffer.ItemOnSpecialOffer.ToLower()) +
                                       " " + (discount * 100) + "% off: -" + (discount * 100) + "p";
                        }
                    }
                }
            }
            return(message ?? "(No offers available)");
        }
コード例 #2
0
        public List <Product> RetrieveAllProducts()
        {
            List <Product>         listOfProducts = new List <Product>();
            SpecialOfferRepository offerRepo      = new SpecialOfferRepository(new SpecialOfferMssqlContext());

            try
            {
                SqlConnection cnn    = ReturnSQLConnection();
                string        query  = "SELECT * FROM dbo.[Product]";
                SqlCommand    newCmd = CreateSQLCommandText(query, cnn);
                SqlDataReader dr     = newCmd.ExecuteReader();
                while (dr.Read())
                {
                    int          productId   = Convert.ToInt32(dr["ID"]);
                    string       productName = dr["name"].ToString();
                    SpecialOffer offer       = offerRepo.RetrieveOfferByProductID(productId);

                    if (offer.RetrieveOfferId() != -1)
                    {
                        SpecialOffer product = new SpecialOffer(offer.RetrieveOfferId(), offer.RetrieveOfferPrice(), productId, productName);
                        product.ProductAmount   = Convert.ToInt32(dr["Amount"]);
                        product.ProductDesc     = dr["Description"].ToString();
                        product.ProductDiscount = Convert.ToDecimal(dr["Discount_Price"]);
                        product.ProductPrice    = Convert.ToDecimal(dr["Price"]);
                        product.ProductStatus   = dr["Status"].ToString();
                        product.ImageUrl        = dr["ImageURL"].ToString();
                        product.BtwPercentage   = Convert.ToInt32(dr["BTWPercentage"]);
                        product.ProductInStock  = Convert.ToInt32(dr["Instock"]);
                        product.StartTime       = offer.StartTime;
                        product.EndTime         = offer.EndTime;

                        listOfProducts.Add(product);
                    }
                    else
                    {
                        Product product = new Product(productId, productName);
                        product.ProductAmount   = Convert.ToInt32(dr["Amount"]);
                        product.ProductDesc     = dr["Description"].ToString();
                        product.ProductDiscount = Convert.ToDecimal(dr["Discount_Price"]);
                        product.ProductPrice    = Convert.ToDecimal(dr["Price"]);
                        product.ProductStatus   = dr["Status"].ToString();
                        product.ImageUrl        = dr["ImageURL"].ToString();
                        product.BtwPercentage   = Convert.ToInt32(dr["BTWPercentage"]);
                        product.ProductInStock  = Convert.ToInt32(dr["Instock"]);

                        listOfProducts.Add(product);
                    }
                }
                cnn.Close();
            }
            catch (NullReferenceException e)
            {
                Console.WriteLine(e.Message);
            }
            return(listOfProducts);
        }
コード例 #3
0
        public static double CalculateTotal(string userInput)
        {
            var    allOffers = new SpecialOfferRepository().GetAllOffers();
            var    userInputAsArray = userInput.ToUpper().Split(' ');
            double itemCost, discount, subTotal = 0;
            int    maxNumberOfTimesToAppplyDiscount = 0;
            int    discountApplied = 0;

            for (int i = 1; i < userInputAsArray.Length; i++)
            {
                var currentItem = allItems.Find(x => x.ItemName == userInputAsArray[i]);
                if (currentItem != null)
                {
                    itemCost = currentItem.ItemPrice;

                    if (allOffers.Find(x => x.ItemOnSpecialOffer == userInputAsArray[i]) != null)
                    {
                        var currentOffer = allOffers.Find(x => x.ItemOnSpecialOffer == userInputAsArray[i]);

                        if (currentOffer.DependentItem == null)
                        {
                            discount  = currentOffer.SpecialOfferDiscount;
                            subTotal += itemCost - (itemCost * discount);
                        }
                        else
                        {
                            var      depItem = currentOffer.DependentItem;
                            string[] source  = userInput.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                            var itemCount = (from word in source
                                             where word.ToLowerInvariant() == depItem.ToLowerInvariant()
                                             select word).Count();
                            maxNumberOfTimesToAppplyDiscount = itemCount / currentOffer.DependentItemQuantity;

                            if (itemCount >= currentOffer.DependentItemQuantity && discountApplied < maxNumberOfTimesToAppplyDiscount)
                            {
                                discount         = currentOffer.SpecialOfferDiscount;
                                subTotal        += itemCost - (itemCost * discount);
                                discountApplied += 1;
                            }
                            else
                            {
                                subTotal += itemCost;
                            }
                        }
                    }
                    else
                    {
                        subTotal += itemCost;
                    }
                }
            }
            return(subTotal);
        }
コード例 #4
0
        public void Setup()
        {
            var specialOfferRepository = new SpecialOfferRepository();
            var discountRepository     = new DiscountRepository();

            _discountService     = new DiscountService(discountRepository);
            _specialOfferService = new SpecialOfferService(specialOfferRepository);

            _shelfItemRepository = new ShelfItemRepository();

            _basketService = new BasketService(_discountService, _specialOfferService);
        }
コード例 #5
0
        private static void Main()
        {
            try
            {
                var shelfItemRepository = new ShelfItemRepository();
                var shelfItems          = shelfItemRepository.GetShelfItems().ToArray();

                var discountRepository = new DiscountRepository();
                var discounts          = discountRepository.GetActiveDiscounts().ToArray();

                var specialOfferRepository = new SpecialOfferRepository();
                var specialOffers          = specialOfferRepository.GetActiveSpecialOffers().ToArray();


                DisplayShelfItems(shelfItems, discounts, specialOffers);


                IDiscountService     discountService     = new DiscountService(discountRepository);
                ISpecialOfferService specialOfferService = new SpecialOfferService(specialOfferRepository);
                using (var basketService = new BasketService(discountService, specialOfferService))
                {
                    BasketHandler(shelfItems, basketService);

                    PrintCustomerReceipt(specialOffers, discounts, basketService);
                }

                ReadLine();
            }
            catch (Exception ex)
            {
                WriteLine("\n\n");

                ILogger logger = new ConsoleLogger();
                logger.LogException(ex);

                ReadLine();
            }
        }