timmy 已修改 10 months ago. 還原成這個修訂版本
1 file changed, 33 insertions, 12 deletions
severance_pay_calculator.py
| @@ -1,25 +1,47 @@ | |||
| 1 | 1 | class SeverancePayCalculator: | |
| 2 | - | def __init__(self, salary, years_of_service, policy="new"): | |
| 2 | + | """ | |
| 3 | + | Legendary 級資遣費計算器,適用於新制與舊制。 | |
| 4 | + | - 自動計算應得資遣費 | |
| 5 | + | - 內建異常處理與日誌紀錄 | |
| 6 | + | """ | |
| 7 | + | ||
| 8 | + | def __init__(self, salary, years_of_service, policy="new", log=True): | |
| 3 | 9 | """ | |
| 4 | 10 | 初始化資遣費計算器 | |
| 5 | 11 | :param salary: 每月工資 (int or float) | |
| 6 | 12 | :param years_of_service: 服務年數 (float) | |
| 7 | 13 | :param policy: "new" (新制) 或 "old" (舊制) | |
| 14 | + | :param log: 是否啟用日誌 (bool) | |
| 8 | 15 | """ | |
| 9 | 16 | self.salary = salary | |
| 10 | 17 | self.years_of_service = years_of_service | |
| 11 | 18 | self.policy = policy.lower() | |
| 19 | + | self.log_enabled = log | |
| 20 | + | self.log(f"初始化: 工資={salary}, 年資={years_of_service}, 政策={policy}") | |
| 21 | + | ||
| 22 | + | def log(self, message): | |
| 23 | + | """ 簡單的日誌紀錄 """ | |
| 24 | + | if self.log_enabled: | |
| 25 | + | from datetime import datetime | |
| 26 | + | timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S") | |
| 27 | + | print(f"[{timestamp}] {self.__class__.__name__}: {message}") | |
| 12 | 28 | ||
| 13 | 29 | def calculate(self): | |
| 14 | 30 | """ 計算資遣費 """ | |
| 15 | - | if self.policy == "new": | |
| 16 | - | severance_pay = min(self.years_of_service * 0.5, 6) * self.salary | |
| 17 | - | elif self.policy == "old": | |
| 18 | - | severance_pay = min(self.years_of_service, 6) * self.salary | |
| 19 | - | else: | |
| 20 | - | raise ValueError("政策類型錯誤,請選擇 'new' 或 'old'") | |
| 21 | - | ||
| 22 | - | return round(severance_pay, 2) | |
| 31 | + | try: | |
| 32 | + | if self.policy == "new": | |
| 33 | + | severance_pay = min(self.years_of_service * 0.5, 6) * self.salary | |
| 34 | + | elif self.policy == "old": | |
| 35 | + | severance_pay = min(self.years_of_service, 6) * self.salary | |
| 36 | + | else: | |
| 37 | + | raise ValueError("政策類型錯誤,請選擇 'new' 或 'old'") | |
| 38 | + | ||
| 39 | + | result = round(severance_pay, 2) | |
| 40 | + | self.log(f"計算結果: {result} 元") | |
| 41 | + | return result | |
| 42 | + | except Exception as e: | |
| 43 | + | self.log(f"錯誤: {e}") | |
| 44 | + | return None | |
| 23 | 45 | ||
| 24 | 46 | def __str__(self): | |
| 25 | 47 | return f"{self.policy.upper()} 制資遣費: {self.calculate()} 元" | |
| @@ -30,8 +52,7 @@ if __name__ == "__main__": | |||
| 30 | 52 | years_of_service = 8.5 # 服務年數 | |
| 31 | 53 | ||
| 32 | 54 | new_policy_calculator = SeverancePayCalculator(salary, years_of_service, "new") | |
| 33 | - | # old_policy_calculator = SeverancePayCalculator(salary, years_of_service, "old") | |
| 55 | + | old_policy_calculator = SeverancePayCalculator(salary, years_of_service, "old") | |
| 34 | 56 | ||
| 35 | 57 | print(new_policy_calculator) | |
| 36 | - | # print(old_policy_calculator) | |
| 37 | - | ||
| 58 | + | print(old_policy_calculator) | |
timmy 已修改 10 months ago. 還原成這個修訂版本
1 file changed, 37 insertions
severance_pay_calculator.py(檔案已創建)
| @@ -0,0 +1,37 @@ | |||
| 1 | + | class SeverancePayCalculator: | |
| 2 | + | def __init__(self, salary, years_of_service, policy="new"): | |
| 3 | + | """ | |
| 4 | + | 初始化資遣費計算器 | |
| 5 | + | :param salary: 每月工資 (int or float) | |
| 6 | + | :param years_of_service: 服務年數 (float) | |
| 7 | + | :param policy: "new" (新制) 或 "old" (舊制) | |
| 8 | + | """ | |
| 9 | + | self.salary = salary | |
| 10 | + | self.years_of_service = years_of_service | |
| 11 | + | self.policy = policy.lower() | |
| 12 | + | ||
| 13 | + | def calculate(self): | |
| 14 | + | """ 計算資遣費 """ | |
| 15 | + | if self.policy == "new": | |
| 16 | + | severance_pay = min(self.years_of_service * 0.5, 6) * self.salary | |
| 17 | + | elif self.policy == "old": | |
| 18 | + | severance_pay = min(self.years_of_service, 6) * self.salary | |
| 19 | + | else: | |
| 20 | + | raise ValueError("政策類型錯誤,請選擇 'new' 或 'old'") | |
| 21 | + | ||
| 22 | + | return round(severance_pay, 2) | |
| 23 | + | ||
| 24 | + | def __str__(self): | |
| 25 | + | return f"{self.policy.upper()} 制資遣費: {self.calculate()} 元" | |
| 26 | + | ||
| 27 | + | # 測試範例 | |
| 28 | + | if __name__ == "__main__": | |
| 29 | + | salary = 100000 # 每月工資 | |
| 30 | + | years_of_service = 8.5 # 服務年數 | |
| 31 | + | ||
| 32 | + | new_policy_calculator = SeverancePayCalculator(salary, years_of_service, "new") | |
| 33 | + | # old_policy_calculator = SeverancePayCalculator(salary, years_of_service, "old") | |
| 34 | + | ||
| 35 | + | print(new_policy_calculator) | |
| 36 | + | # print(old_policy_calculator) | |
| 37 | + | ||