Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public interface IProductTax
- {
- bool IsApplicableTo(IProduct product);
- decimal Compute(IProduct product);
- }
- public class TaxProcessor : ITaxProcessor
- {
- List<IProductTax> SalesTaxes;
- public TaxProcessor()
- {
- SalesTaxes = new List<IProductTax>()
- {
- new SalesTax(),
- new ImportDuty()
- };
- }
- public decimal ComputeTotalTax(IProduct product)
- {
- decimal computedSalesTax = 0;
- foreach (var tax in SalesTaxes)
- {
- computedSalesTax += tax.Compute(product);
- }
- return computedSalesTax;
- }
- }
- public class SalesTax : IProductTax
- {
- public decimal Compute(IProduct product)
- {
- if (IsApplicableTo(product))
- return product.Price * 0.1m;
- return 0m;
- }
- public bool IsApplicableTo(IProduct product)
- {
- return !product.ItemType.HasFlag(ItemTypes.SalesTaxEmptedItemTypes);
- }
- }
- public class ImportDuty : IProductTax
- {
- public decimal Compute(IProduct product)
- {
- if (IsApplicableTo(product))
- return product.Price * 0.05m;
- return 0m;
- }
- public bool IsApplicableTo(IProduct product)
- {
- return product.IsImport;
- }
- }
- public class BillProcessor
- {
- public BillProcessor(ITaxProcessor salesTaxProcess)
- {
- SalesTaxProcessor = salesTaxProcess;
- }
- public Reciept ProcessCart(List<ShoppingItem> shoppingCart)
- {
- var billedShopppingItems = new List<BilledShopppingItem>();
- decimal totalTaxForCart = 0;
- decimal totalBilledAmount = 0;
- foreach (var shoppingItem in shoppingCart)
- {
- decimal individualTax = SalesTaxProcessor.ComputeTotalTax(shoppingItem.Product);
- decimal taxForAllProducts = individualTax * shoppingItem.Quantity;
- decimal totalPrice = shoppingItem.Product.Price * shoppingItem.Quantity;
- decimal totalPriceAfterTax = totalPrice + taxForAllProducts;
- billedShopppingItems.Add(new BilledShopppingItem(shoppingItem, taxForAllProducts,
- totalPriceAfterTax));
- totalTaxForCart += taxForAllProducts;
- totalBilledAmount += totalPriceAfterTax;
- }
- return new Reciept(billedShopppingItems, totalBilledAmount, totalTaxForCart);
- }
- public ITaxProcessor SalesTaxProcessor { get; }
- }
- public class ShoppingItem
- {
- public ShoppingItem(IProduct product, int quantity)
- {
- Product = product;
- Quantity = quantity;
- }
- public IProduct Product { get; }
- public int Quantity { get; }
- }
- public class Product : IProduct
- {
- public Product(string name, decimal price, bool isImport, ItemTypes itemType)
- {
- Name = name;
- Price = price;
- IsImport = isImport;
- ItemType = itemType;
- }
- public string Name { get; }
- public decimal Price { get; }
- public bool IsImport { get; }
- public ItemTypes ItemType { get; }
- }
- public class BilledShopppingItem
- {
- public BilledShopppingItem(ShoppingItem shoppingItem, decimal tax, decimal totalPrice)
- {
- ShoppingItem = shoppingItem;
- Tax = tax;
- TotalPrice = totalPrice;
- }
- public ShoppingItem ShoppingItem { get; }
- public decimal Tax { get; }
- public decimal TotalPrice { get; }
- }
- public class Reciept
- {
- public Reciept(List<BilledShopppingItem> processedShoppingCart, decimal totalBillAmount, decimal totalSalesTax)
- {
- ProcessedShoppingCart = processedShoppingCart;
- TotalBillAmount = totalBillAmount;
- TotalSalesTax = totalSalesTax;
- }
- public void PrintBill()
- {
- foreach (var processedItem in ProcessedShoppingCart)
- {
- Console.WriteLine($"{processedItem.ShoppingItem.Product.Name} { processedItem.TotalPrice }");
- }
- Console.WriteLine($"Sales Taxes {TotalSalesTax}");
- Console.WriteLine($"Total {TotalBillAmount}");
- }
- public List<BilledShopppingItem> ProcessedShoppingCart { get; }
- public decimal TotalBillAmount { get; }
- public decimal TotalSalesTax { get; }
- }
- [Flags]
- public enum ItemTypes
- {
- None = 0,
- Book = 1 << 0,
- Food = 1 << 1,
- Medical = 1 << 2,
- Others = 1 << 3,
- SalesTaxEmptedItemTypes = Book | Food | Medical
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement