星期一, 2月 23, 2015

[C#] Delegate 練習

遠距教學提到可以透過 delegate 把 Method 當成參數傳進 Method 中,故意拿勞保費計算來練習 delegate

畫面控件
  • 勞保投保薪資:25200、33300、43900
  • 減免條件:正常、身心輕度、身心中度、極重或重

[C#] Delegate 練習-1

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
  
    }

    delegate decimal demo(decimal LSalary , decimal discount);

    class Employ
    {
        public bool Hirer { get; set; }
        
        // 計算勞保薪資
        public decimal LaborFee(decimal LSalary , decimal discount,demo func)            
        {                 
            decimal retValue = 0m;                 

            if (this.Hirer == true)                 
            {                      
                retValue = func(LSalary , discount);                 
            }                 
            else                 
            {                      
                retValue = func(LSalary, discount);                 
            }                 
            return retValue;            
        }              
      
        // 勞保計算公式 - 雇主
        public decimal LaborForHirer(decimal LSalary, decimal discount)
        {

            decimal retValue = Math.Round(LSalary * 0.09m * 0.2m, 0, MidpointRounding.AwayFromZero) -
                               Math.Round
                                   (
                                       (
                                           Math.Round(LSalary * 0.09m * 0.2m, 0, MidpointRounding.AwayFromZero) +
                                           Math.Round(LSalary * 0.2m, 0, MidpointRounding.AwayFromZero)
                                       ) * discount
                                       , 0, MidpointRounding.AwayFromZero
                                   );

            if (retValue < 0) retValue = 0;
            return retValue;
        }

        // 勞保計算公式 - 員工
        public decimal LaborForEmploy(decimal LSalary, decimal discount)
        {
            decimal retValue = Math.Round(LSalary * 0.09m * 0.2m, 0, MidpointRounding.AwayFromZero) +
                               Math.Round(LSalary * 0.01m * 0.2m, 0, MidpointRounding.AwayFromZero) -
                               Math.Round
                                   (
                                       (
                                           Math.Round(LSalary * 0.09m * 0.2m, 0, MidpointRounding.AwayFromZero) +
                                           Math.Round(LSalary * 0.01m * 0.2m, 0, MidpointRounding.AwayFromZero)
                                       ) * discount
                                       , 0, MidpointRounding.AwayFromZero
                                   );

            if (retValue < 0) retValue = 0;
            return retValue;
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        Employ emp = new Employ();
        emp.Hirer = chkHirer.Checked;
        decimal LSalary = Convert.ToDecimal(ddlLSalary.SelectedValue);

        decimal discount = 0m;
        string discountType = ddldiscountType.SelectedValue;
        switch (discountType)
        {
            case "正常":
                discount = 0m;
                break ;
            case "身心輕度":
                discount = 0.25m;
                break ;
            case "身心中度":
                discount = 0.5m;
                break ;
            case "極重或重":
                discount = 1m;
                break;
            default:
                discount = 1m;
                break;
        }

        demo func;
        if (emp.Hirer == true)
        {
            func = new demo(emp.LaborForHirer);
        }
        else
        {
            func = new demo(emp.LaborForEmploy);
        }

        lblResult.Text = string.Format("勞保費為:{0}", emp.LaborFee(LSalary, discount, func));
    }
}

顯示結果
[C#] Delegate 練習-2

沒有留言:

張貼留言