public static async Task Main(string[] args)
        {
            var printer    = new ConsoleReceiptPrinter();
            var calculator = new ReceiptCalculator(new InMemoryItemsRepository(), new TaxCalculator(0.10m, 0.05m));

            foreach (var cart in CreateSampleShoppingCarts())
            {
                var receipt = await calculator.CalculateAsync(cart);

                await printer.PrintAsync(receipt);
            }
        }
Example #2
0
        public void Checkout()
        {
            try
            {
                IRepository <Product> productRepository = new FileRepository <Product>(Path.Combine("..\\..\\Files", "product.json"));

                IRepository <OnSalePromotion> onSalePromotionRepository =
                    new FileRepository <OnSalePromotion>(Path.Combine("..\\..\\Files", "onsale.json"));

                IRepository <GroupSalePromotion> groupSalePromotionRepository =
                    new FileRepository <GroupSalePromotion>(Path.Combine("..\\..\\Files", "groupsale.json"));

                IRepository <AdditionalSalePromotion> additionalSalePromotionRepository =
                    new FileRepository <AdditionalSalePromotion>(Path.Combine("..\\..\\Files", "additionalsale.json"));


                // Simple promotion data validation
                IPromotionValidator promotionValidator;


                promotionValidator = new OnSalePromotionValidator(onSalePromotionRepository.GetAll());
                promotionValidator.Validate();

                promotionValidator = new GroupSalePromotionValidator(groupSalePromotionRepository.GetAll());
                promotionValidator.Validate();

                promotionValidator = new AdditionalSalePromotionValidator(additionalSalePromotionRepository.GetAll());
                promotionValidator.Validate();



                // Read basket data
                List <Product> productsInBasket = JsonConvert.DeserializeObject <List <Product> >(File.ReadAllText(Path.Combine("..\\..\\Files", "basket.json")));

                // Service Layer Facade
                ISaleService saleService = new SaleServiceImpl(productRepository, onSalePromotionRepository, groupSalePromotionRepository, additionalSalePromotionRepository);


                foreach (var item in productsInBasket)
                {
                    saleService.AddProduct(item.Id);
                }

                Receipt receipt = saleService.GetReceipt();

                ReceiptPrinter receiptPrinter = new ConsoleReceiptPrinter(receipt);
                receiptPrinter.Print();
            }
            catch (ProductException pdex)
            {
                Console.WriteLine("=== " + pdex.Message + " ===");
            }
            catch (PromotionException pmex)
            {
                Console.WriteLine("=== " + pmex.Message + " ===");
                return;
            }
            catch (Exception ex)
            {
                Console.WriteLine("=== Woops, there's something wrong..." + ex.ToString() + " ===");
            }
        }