參考答案:
1.先實作ImplTax這個Class,目的是製作所得稅公式的框架和實作ITax這個interface
=================ImplTax=================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CSD04
{
class ImplTax:ITax
{
int level;
int minInc;
int maxInc;
double rate;
int adjust;
public ImplTax(int l,int min,int max,double r,int ad)
{
level = l;
minInc = min;
maxInc = max;
rate = r;
adjust = ad;
}
bool ITax.IsQualified(int Amount)
{
if (Amount >= minInc && Amount <= maxInc)
return true;
else
return false;
}
int ITax.GetTax(int Amount)
{
return (int)Math.Round(Amount * rate - adjust);
}
int ITax.GetLevel()
{
return level;
}
}
}
=======================================
再來修改TaxCalculator,利用ImplTax製作的框架輸入所得稅計算的規則與數值
==================TaxCalculator============
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CSD04
{
public class TaxCalculator
{
private static List<ITax> _taxStrategics = new List<ITax>();
static TaxCalculator()
{
//TODO
_taxStrategics.Add(new ImplTax(1, 0, 500000, 0.05, 0));
_taxStrategics.Add(new ImplTax(2, 500001, 1130000, 0.12, 35000));
_taxStrategics.Add(new ImplTax(3, 1130001, 2260000, 0.20, 124400));
_taxStrategics.Add(new ImplTax(4, 2260001, 4230000, 0.30, 351400));
_taxStrategics.Add(new ImplTax(5, 4230001, int.MaxValue, 0.40, 774400));
}
public static int GetTaxLevel(int Amount)
{
foreach (ITax taxStrategic in _taxStrategics)
{
if (taxStrategic.IsQualified(Amount))
return taxStrategic.GetLevel();
}
return 0;
}
public static int GetTaxAmount(int Amount)
{
foreach (ITax taxStrategic in _taxStrategics)
{
if (taxStrategic.IsQualified(Amount))
return taxStrategic.GetTax(Amount);
}
return 0;
}
}
}
=======================================
最後主程式做呼叫和顯示畫面(主要利用TaxCalculator提供的方法)
==================Program.cs==============
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CSD04
{
class Program
{
static void Main(string[] args)
{
int amount1 = 827391;
int amount2 = 334839;
int amount3 = 1839203;
int amount4 = 4827812;
int amount5 = 3210292;
// TODO: calculate the tax with given amount.
Console.WriteLine("Amount: ${0:N0}, Lv: {1}, Tax: ${2:N0}", amount1, TaxCalculator.GetTaxLevel(amount1), TaxCalculator.GetTaxAmount(amount1));
Console.WriteLine("Amount: ${0:N0}, Lv: {1}, Tax: ${2:N0}", amount2, TaxCalculator.GetTaxLevel(amount2), TaxCalculator.GetTaxAmount(amount2));
Console.WriteLine("Amount: ${0:N0}, Lv: {1}, Tax: ${2:N0}", amount3, TaxCalculator.GetTaxLevel(amount3), TaxCalculator.GetTaxAmount(amount3));
Console.WriteLine("Amount: ${0:N0}, Lv: {1}, Tax: ${2:N0}", amount4, TaxCalculator.GetTaxLevel(amount4), TaxCalculator.GetTaxAmount(amount4));
Console.WriteLine("Amount: ${0:N0}, Lv: {1}, Tax: ${2:N0}", amount5, TaxCalculator.GetTaxLevel(amount5), TaxCalculator.GetTaxAmount(amount5));
Console.ReadLine();
}
}
}
=======================================
執行結果:
留言列表