TQC+ C# 認證教學手冊

402、綜合所得稅

☑易 ☐中 ☐難

專案結構

  • ITax.cs - 介面(Interface)
  • Program.cs - 主程式
  • TaxCalculator.cs - 綜合所得稅計算器

介面的實作類別

public interface ITax
{
    // 是否符合本課稅等級
    bool IsQualified(int Amount);
    // 取得稅額
    int GetTax(int Amount);
    // 取得目前的課稅等級
    int GetLevel();
}

從題目說明的這一段:

「將表格中的五種級距計算方式單獨寫成 Class,套上 ITax 介面實作。」

方法一、實作 5 個新類別。

  • TaxLv1.cs
  • TaxLv2.cs
  • TaxLv3.cs
  • TaxLv4.cs
  • TaxLv5.cs

方法二、只用一個新類別。

  • ImplTax.cs

Impl. = Implementation

實作 ImplTax 類別

class ImplTax : ITax 
{

}

檔名必須是 ImplTax.cs

「ImplTax : ITax」的意思是「ImplTax 類別實現 ITax 介面」

TaxCalculator 的初始化

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.2, 124400));
    _taxStrategics.Add(new ImplTax(4, 2260001, 4230000, 0.3, 351400));
    _taxStrategics.Add(new ImplTax(5, 4230001, int.MaxValue, 0.4, 774400));
}

實作 ImplTax 的建構式

建構式(constructor)就是在建立新物件時執行的函示,可以用來接收參數。

class ImplTax : ITax 
{
    int level;
    int minAmount, maxAmount;
    double rate;
    int adjustment;

    public ImplTax(int level, int minAmount, int maxAmount, double rate, int adjustment)
    {
        this.level = level;
        this.minAmount = minAmount;
        this.maxAmount = maxAmount;
        this.rate = rate;
        this.adjustment = adjustment;
    }
}

建構式中將接收到的參數保存起來很重要。

讓 ImplTax 具體實現 ITax 介面方法

bool ITax.IsQualified(int Amount)
{
    if (Amount >= minAmount && Amount <= maxAmount)
    {
        return true;
    }
    return false;
}

int ITax.GetTax(int Amount)
{
    return (int)(Amount * rate) - adjustment;
}

int ITax.GetLevel()
{
    return level;
}

主程式

使用 TaxCalculator 的靜態方法來計算所得稅「等級」與「應納稅額」。

Console.WriteLine("Amount: {0}, Lv: {1}, Tax: ${2}",
    amount1,
    TaxCalculator.GetTaxLevel(amount1),
    TaxCalculator.GetTaxAmount(amount1));

標準數值格式字串.aspx)

參考程式碼

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;

            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();
        }
    }
}

TaxCalculator.cs

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()
        {

            _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.2, 124400));
            _taxStrategics.Add(new ImplTax(4, 2260001, 4230000, 0.3, 351400));
            _taxStrategics.Add(new ImplTax(5, 4230001, int.MaxValue, 0.4, 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;
        }
    }
}

ITax.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CSD04
{
    public interface ITax
    {
        // 是否符合本課稅等級
        bool IsQualified(int Amount);
        // 取得稅額
        int GetTax(int Amount);
        // 取得目前的課稅等級
        int GetLevel();
    }
}

ImplTax.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CSD04
{
    class ImplTax : ITax 
    {
        int level;
        int minAmount, maxAmount;
        double rate;
        int adjustment;

        public ImplTax(int level, int minAmount, int maxAmount, double rate, int adjustment)
        {
            this.level = level;
            this.minAmount = minAmount;
            this.maxAmount = maxAmount;
            this.rate = rate;
            this.adjustment = adjustment;
        }

        bool ITax.IsQualified(int Amount)
        {
            if (Amount >= minAmount && Amount <= maxAmount)
            {
                return true;
            }
            return false;
        }

        int ITax.GetTax(int Amount)
        {
            return (int)(Amount * rate) - adjustment;
        }

        int ITax.GetLevel()
        {
            return level;
        }
    }
}