Advertisement
Guest User

Untitled

a guest
Jun 27th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace TaxLibrarySample001
  6. {
  7. public class TaxHelper
  8. {
  9. public static decimal GetTaxResult(decimal income)
  10. {
  11. decimal result = 0;
  12.  
  13. foreach (var Item in GetTaxTable().OrderBy(r => r.Tax))
  14. {
  15. if (Item.ParentTax == null)
  16. {
  17. if (income > 0 && income <= Item.To)
  18. result += (income - Item.from) * Item.Tax;
  19. else
  20. result += (Item.To - Item.from) * Item.Tax;
  21. continue;
  22. }
  23.  
  24. if (income > Item.ParentTax.To && income <= Item.To)
  25. result += (income - Item.ParentTax.To) * Item.Tax;
  26.  
  27. if (income > Item.To)
  28. result += (Item.To - Item.ParentTax.To) * Item.Tax;
  29. }
  30.  
  31. return result;
  32. }
  33.  
  34. internal static IEnumerable<TaxTable> GetTaxTable(Func<TaxTable, bool> filter = null)
  35. {
  36. List<TaxTable> taxes = new List<TaxTable>();
  37.  
  38. taxes.Add(new TaxTable { from = 0, To = 540000, Tax = 0.05m, ParentTax = null });
  39. taxes.Add(new TaxTable { from = 540001, To = 1210000, Tax = 0.12m, ParentTax = new TaxTable { from = 0, To = 540000, Tax = 0.05m } });
  40. taxes.Add(new TaxTable { from = 1210001, To = 2420000, Tax = 0.2m, ParentTax = new TaxTable { from = 540001, To = 1210000, Tax = 0.12m } });
  41. taxes.Add(new TaxTable { from = 2420001, To = 4530000, Tax = 0.3m, ParentTax = new TaxTable { from = 1210001, To = 2420000, Tax = 0.2m } });
  42. taxes.Add(new TaxTable { from = 4530001, To = 10310000, Tax = 0.4m, ParentTax = new TaxTable { from = 2420001, To = 4530000, Tax = 0.3m } });
  43. taxes.Add(new TaxTable { from = 10310001, To = 999999999999999, Tax = 0.5m, ParentTax = new TaxTable { from = 4530001, To = 10310000, Tax = 0.4m } });
  44. if (filter == null)
  45. return taxes;
  46. return taxes.Where(filter);
  47. }
  48. }
  49.  
  50. internal class TaxTable
  51. {
  52. public decimal from { get; set; }
  53. public decimal To { get; set; }
  54. public decimal Tax { get; set; }
  55. public TaxTable ParentTax { get; set; }
  56. }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement