2025年最新退休年齡計算 | C#程式碼 | .net core程式碼

钻葛格發表於2024-09-27
using System;
using RetireUtility;

namespace ConsoleApp1
{
    internal class Program
    {
        public static void Main(string[] args)
        {
            //參照 https://mp.weixin.qq.com/s/wqFAOhfNhfwMJKaQEE5lYQ 進行測試

            var r1 = Retire.CalcNewRetireDate("AAABBB19760503CC1C", employee_type.male_all);
            Console.WriteLine();

            Retire.CalcNewRetireDate("1976-05-03", gender.female, employee_type.female_manage);
            Console.WriteLine();

            Retire.CalcNewRetireDate(1976, 5, 3, gender.female, employee_type.female_operation);
            Console.ReadLine();
        }
    }
}

  

using System;

namespace RetireUtility
{
    public enum gender
    {
        male,
        female
    }
    public enum employee_type
    {
        male_all,
        female_manage,
        female_operation
    }
    public struct retire_result
    {
        public DateTime birthday { get; set; }
        public gender gender { get; set; }
        public employee_type employee_type { get; set; }
        public string retire_age { get; set; }
        public DateTime retire_date { get; set; }
        public int delay_months { get; set; }
        public string least_pay_months { get; set; }
    }
    public class Retire
    {
        public static retire_result CalcNewRetireDate(string birthday, gender gender, employee_type employee_type)
        {
            int gender_flag = gender == gender.male ? 1 : 0;
            string birthday_str = Convert.ToDateTime(birthday).ToString("yyyyMMdd");
            string sfz = $"AAABBB{birthday_str}CC{gender_flag}D";
            return CalcNewRetireDate(sfz, employee_type);
        }
        public static retire_result CalcNewRetireDate(int year, int month, int day, gender gender, employee_type employee_type)
        {
            int gender_flag = gender == gender.male ? 1 : 0;
            string birthday_str = new DateTime(year, month, day).ToString("yyyyMMdd");
            string sfz = $"AAABBB{birthday_str}CC{gender_flag}D";
            return CalcNewRetireDate(sfz, employee_type);
        }
        public static retire_result CalcNewRetireDate(string sfz, employee_type employee_type)
        {
            int year = int.Parse(sfz.Substring(6, 4));
            int month = int.Parse(sfz.Substring(10, 2));
            int day = int.Parse(sfz.Substring(12, 2));
            gender gender = int.Parse(sfz.Substring(16, 1)) % 2 == 0 ? gender.female : gender.male;
            DateTime birthday = new DateTime(year, month, day);

            int retire_delay_months = 0;
            int retire_original_months = 0;

            if (gender == gender.male) //男 60
            {
                retire_original_months = 12 * 60;
                if (year >= 1965)
                {
                    int over_months = 12 * (year - 1965) + (month - 0);
                    retire_delay_months = 1 * (((over_months - 1) / 4) + 1);
                    retire_delay_months = retire_delay_months > 36 ? 36 : retire_delay_months;
                }
            }
            else if (gender == gender.female && employee_type == employee_type.female_manage) //女 管理 55
            {
                retire_original_months = 12 * 55;
                if (year >= 1970)
                {
                    int over_months = 12 * (year - 1970) + (month - 0);
                    retire_delay_months = 1 * (((over_months - 1) / 4) + 1);
                    retire_delay_months = retire_delay_months > 36 ? 36 : retire_delay_months;
                }
            }
            else if (gender == gender.female && employee_type == employee_type.female_operation) //女 操作 50
            {
                retire_original_months = 12 * 50;
                if (year >= 1975)
                {
                    int over_months = 12 * (year - 1975) + (month - 0);
                    retire_delay_months = 1 * (((over_months - 1) / 2) + 1);
                    retire_delay_months = retire_delay_months > 60 ? 60 : retire_delay_months;
                }
            }

            int total_work_months = retire_original_months + retire_delay_months;
            DateTime retire_date = birthday.AddMonths(total_work_months);

            //計算“當年最低繳費年限”
            int least_pay_months = 12 * 15;
            int retire_year = retire_date.Year;
            if (retire_year >= 2030 && retire_year <= 2039)
            {
                least_pay_months += 6 * ((retire_year - 2030) + 1);
            }
            else if (retire_year > 2039)
            {
                least_pay_months = 12 * 20;
            }

            string retire_age = $"{total_work_months / 12}歲{total_work_months % 12}個月";
            string least_pay_months_str = $"{least_pay_months / 12}年{least_pay_months % 12}個月";

            Console.WriteLine($"性別\t\t\t{gender.ToString()}\r\n" +
                $"類別\t\t\t{employee_type.ToString()}\r\n" +
                $"出生時間\t\t{year}年{month}月\r\n" +
                $"改革後法定退休年齡\t{retire_age}\r\n" +
                $"改革後退休時間\t\t{retire_date.ToString("yyyy年M月")}\r\n" +
                $"延遲月數\t\t{retire_delay_months}\r\n" +
                $"當年最低繳費年限\t{least_pay_months_str}");

            var result = new retire_result
            {
                birthday = birthday,
                gender = gender,
                employee_type = employee_type,
                delay_months = retire_delay_months,
                retire_age = retire_age,
                retire_date = retire_date,
                least_pay_months = least_pay_months_str
            };
            return result;
        }
    }
}

  

相關文章