1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
| class EmployeeManagementSystem: """ 员工管理系统:使用面向对象方式管理 """ def __init__(self, data_file="employees.json"): self.data_file = data_file self.employees: List[Employee] = [] self.departments: Dict[str, Department] = {} self.load_data() self.init_departments() def init_departments(self): """初始化各部门""" self.departments["财务部"] = FinanceDepartment("财务部", "张经理") self.departments["销售部"] = SalesDepartment("销售部", "李经理") self.departments["技术部"] = TechDepartment("技术部", "王经理") def add_employee(self): """添加员工""" print("\n" + "=" * 40) print("添加新员工") print("=" * 40) name = input("姓名: ").strip() emp_id = input("工号: ").strip() dept = input("部门(财务部/销售部/技术部): ").strip() salary = float(input("基本工资: ").strip()) if dept == "销售部": commission = float(input("提成比例(如0.08表示8%): ").strip()) emp = SalesEmployee(name, emp_id, salary, commission) elif dept == "技术部": bonus = float(input("项目奖金: ").strip()) emp = TechEmployee(name, emp_id, salary, bonus) else: emp = Employee(name, emp_id, dept, salary) self.employees.append(emp) print(f"\n✅ 员工添加成功!") print(emp) def list_employees(self): """列出所有员工""" print("\n" + "=" * 50) print("员工列表") print("=" * 50) if not self.employees: print("📭 暂无员工数据") return for i, emp in enumerate(self.employees, 1): print(f"\n{i}. {emp}") def calculate_all_salaries(self): """计算所有员工本月薪资""" print("\n" + "=" * 50) print("薪资计算") print("=" * 50) if not self.employees: print("⚠️ 暂无员工数据") return total_salary = 0 for emp in self.employees: salary = emp.get_monthly_salary() total_salary += salary print(f" {emp.name}: ¥{salary:,.2f}") print(f"\n💰 本月工资总支出: ¥{total_salary:,.2f}") def reimbursement_request(self): """报销申请""" print("\n" + "=" * 40) print("报销申请") print("=" * 40) if not self.employees: print("⚠️ 暂无员工数据") return self.list_employees() idx = int(input("选择员工编号(数字): ").strip()) - 1 if idx < 0 or idx >= len(self.employees): print("❌ 编号错误") return emp = self.employees[idx] amount = float(input("报销金额: ").strip()) reason = input("报销事由: ").strip() if emp.department in self.departments: self.departments[emp.department].apply_reimbursement(emp, amount, reason) else: print("⚠️ 该部门暂无审批流程") def save_data(self): """保存数据到JSON""" data = { 'employees': [], 'update_time': datetime.now().strftime('%Y-%m-%d %H:%M:%S') } for emp in self.employees: emp_data = { 'name': emp.name, 'employee_id': emp.employee_id, 'department': emp.department, 'base_salary': emp.base_salary, 'type': type(emp).__name__ } if isinstance(emp, SalesEmployee): emp_data['commission_rate'] = emp.commission_rate emp_data['monthly_sales'] = emp.monthly_sales elif isinstance(emp, TechEmployee): emp_data['project_bonus'] = emp.project_bonus data['employees'].append(emp_data) with open(self.data_file, 'w', encoding='utf-8') as f: json.dump(data, f, ensure_ascii=False, indent=2) print(f"✅ 数据已保存到 {self.data_file}") def load_data(self): """从JSON加载数据""" if not os.path.exists(self.data_file): return try: with open(self.data_file, 'r', encoding='utf-8') as f: data = json.load(f) self.employees = [] for emp_data in data.get('employees', []): emp_type = emp_data.get('type', 'Employee') if emp_type == 'SalesEmployee': emp = SalesEmployee( emp_data['name'], emp_data['employee_id'], emp_data['base_salary'], emp_data.get('commission_rate', 0.05) ) emp.monthly_sales = emp_data.get('monthly_sales', 0) elif emp_type == 'TechEmployee': emp = TechEmployee( emp_data['name'], emp_data['employee_id'], emp_data['base_salary'], emp_data.get('project_bonus', 0) ) else: emp = Employee( emp_data['name'], emp_data['employee_id'], emp_data['department'], emp_data['base_salary'] ) self.employees.append(emp) print(f"📂 已加载 {len(self.employees)} 名员工数据") except Exception as e: print(f"❌ 加载数据失败: {e}")
def main(): """主菜单""" ems = EmployeeManagementSystem() while True: print("\n" + "=" * 55) print("员工管理系统") print("=" * 55) print("1. 添加员工") print("2. 查看员工列表") print("3. 计算全体薪资") print("4. 报销申请") print("5. 保存数据") print("6. 退出") print("=" * 55) choice = input("请选择功能: ").strip() if choice == "1": ems.add_employee() elif choice == "2": ems.list_employees() input("\n按回车键继续...") elif choice == "3": ems.calculate_all_salaries() input("\n按回车键继续...") elif choice == "4": ems.reimbursement_request() input("\n按回车键继续...") elif choice == "5": ems.save_data() input("\n按回车键继续...") elif choice == "6": print("👋 感谢使用,数据已自动保存!") ems.save_data() break else: print("请输入1-6之间的数字!")
if __name__ == "__main__": main()
|