Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.27 KB | None | 0 0
  1. public interface IProductTax
  2. {
  3. bool IsApplicableTo(IProduct product);
  4. decimal Compute(IProduct product);
  5. }
  6.  
  7. public class TaxProcessor : ITaxProcessor
  8. {
  9. List<IProductTax> SalesTaxes;
  10. public TaxProcessor()
  11. {
  12. SalesTaxes = new List<IProductTax>()
  13. {
  14. new SalesTax(),
  15. new ImportDuty()
  16. };
  17. }
  18. public decimal ComputeTotalTax(IProduct product)
  19. {
  20. decimal computedSalesTax = 0;
  21. foreach (var tax in SalesTaxes)
  22. {
  23. computedSalesTax += tax.Compute(product);
  24. }
  25.  
  26. return computedSalesTax;
  27. }
  28. }
  29.  
  30. public class SalesTax : IProductTax
  31. {
  32. public decimal Compute(IProduct product)
  33. {
  34. if (IsApplicableTo(product))
  35. return product.Price * 0.1m;
  36. return 0m;
  37. }
  38.  
  39. public bool IsApplicableTo(IProduct product)
  40. {
  41. return !product.ItemType.HasFlag(ItemTypes.SalesTaxEmptedItemTypes);
  42. }
  43. }
  44.  
  45. public class ImportDuty : IProductTax
  46. {
  47. public decimal Compute(IProduct product)
  48. {
  49. if (IsApplicableTo(product))
  50. return product.Price * 0.05m;
  51. return 0m;
  52. }
  53.  
  54. public bool IsApplicableTo(IProduct product)
  55. {
  56. return product.IsImport;
  57. }
  58. }
  59.  
  60. public class BillProcessor
  61. {
  62. public BillProcessor(ITaxProcessor salesTaxProcess)
  63. {
  64. SalesTaxProcessor = salesTaxProcess;
  65. }
  66.  
  67. public Reciept ProcessCart(List<ShoppingItem> shoppingCart)
  68. {
  69. var billedShopppingItems = new List<BilledShopppingItem>();
  70. decimal totalTaxForCart = 0;
  71. decimal totalBilledAmount = 0;
  72. foreach (var shoppingItem in shoppingCart)
  73. {
  74. decimal individualTax = SalesTaxProcessor.ComputeTotalTax(shoppingItem.Product);
  75. decimal taxForAllProducts = individualTax * shoppingItem.Quantity;
  76.  
  77. decimal totalPrice = shoppingItem.Product.Price * shoppingItem.Quantity;
  78. decimal totalPriceAfterTax = totalPrice + taxForAllProducts;
  79.  
  80. billedShopppingItems.Add(new BilledShopppingItem(shoppingItem, taxForAllProducts,
  81. totalPriceAfterTax));
  82.  
  83. totalTaxForCart += taxForAllProducts;
  84. totalBilledAmount += totalPriceAfterTax;
  85. }
  86. return new Reciept(billedShopppingItems, totalBilledAmount, totalTaxForCart);
  87. }
  88.  
  89. public ITaxProcessor SalesTaxProcessor { get; }
  90. }
  91.  
  92. public class ShoppingItem
  93. {
  94. public ShoppingItem(IProduct product, int quantity)
  95. {
  96. Product = product;
  97. Quantity = quantity;
  98. }
  99.  
  100. public IProduct Product { get; }
  101. public int Quantity { get; }
  102. }
  103.  
  104. public class Product : IProduct
  105. {
  106. public Product(string name, decimal price, bool isImport, ItemTypes itemType)
  107. {
  108. Name = name;
  109. Price = price;
  110. IsImport = isImport;
  111. ItemType = itemType;
  112. }
  113.  
  114. public string Name { get; }
  115. public decimal Price { get; }
  116. public bool IsImport { get; }
  117. public ItemTypes ItemType { get; }
  118. }
  119.  
  120. public class BilledShopppingItem
  121. {
  122. public BilledShopppingItem(ShoppingItem shoppingItem, decimal tax, decimal totalPrice)
  123. {
  124. ShoppingItem = shoppingItem;
  125. Tax = tax;
  126. TotalPrice = totalPrice;
  127. }
  128.  
  129. public ShoppingItem ShoppingItem { get; }
  130. public decimal Tax { get; }
  131. public decimal TotalPrice { get; }
  132. }
  133.  
  134. public class Reciept
  135. {
  136. public Reciept(List<BilledShopppingItem> processedShoppingCart, decimal totalBillAmount, decimal totalSalesTax)
  137. {
  138. ProcessedShoppingCart = processedShoppingCart;
  139. TotalBillAmount = totalBillAmount;
  140. TotalSalesTax = totalSalesTax;
  141. }
  142.  
  143. public void PrintBill()
  144. {
  145. foreach (var processedItem in ProcessedShoppingCart)
  146. {
  147. Console.WriteLine($"{processedItem.ShoppingItem.Product.Name} { processedItem.TotalPrice }");
  148. }
  149. Console.WriteLine($"Sales Taxes {TotalSalesTax}");
  150. Console.WriteLine($"Total {TotalBillAmount}");
  151. }
  152. public List<BilledShopppingItem> ProcessedShoppingCart { get; }
  153. public decimal TotalBillAmount { get; }
  154. public decimal TotalSalesTax { get; }
  155. }
  156.  
  157. [Flags]
  158. public enum ItemTypes
  159. {
  160. None = 0,
  161. Book = 1 << 0,
  162. Food = 1 << 1,
  163. Medical = 1 << 2,
  164. Others = 1 << 3,
  165. SalesTaxEmptedItemTypes = Book | Food | Medical
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement