diff --git a/src/user_manager.py b/src/user_manager.py new file mode 100644 index 0000000..7aede43 --- /dev/null +++ b/src/user_manager.py @@ -0,0 +1,64 @@ +# user_manager.py +from enum import Enum, auto + + +class AccountType(Enum): + """账户类型枚举类,对应小学、初中、高中三个级别""" + PRIMARY_SCHOOL = auto() + MIDDLE_SCHOOL = auto() + HIGH_SCHOOL = auto() + INVALID = auto() + + +class UserManager: + def __init__(self): + # 初始化预设账户:key为用户名,value为(密码, 账户类型) + self.user_database = { + # 小学账户 + "张三 1": ("123", AccountType.PRIMARY_SCHOOL), + "张三 2": ("123", AccountType.PRIMARY_SCHOOL), + "张三 3": ("123", AccountType.PRIMARY_SCHOOL), + # 初中账户 + "李四 1": ("123", AccountType.MIDDLE_SCHOOL), + "李四 2": ("123", AccountType.MIDDLE_SCHOOL), + "李四 3": ("123", AccountType.MIDDLE_SCHOOL), + # 高中账户 + "王五 1": ("123", AccountType.HIGH_SCHOOL), + "王五 2": ("123", AccountType.HIGH_SCHOOL), + "王五 3": ("123", AccountType.HIGH_SCHOOL), + } + + def verify_user(self, username: str, password: str) -> AccountType: + """ + 验证用户名和密码是否正确 + :param username: 输入的用户名 + :param password: 输入的密码 + :return: 账户类型,无效账户返回INVALID + """ + user_info = self.user_database.get(username) + if user_info and user_info[0] == password: + return user_info[1] + return AccountType.INVALID + + def type_to_str(self, account_type: AccountType) -> str: + """将账户类型转换为中文描述""" + type_map = { + AccountType.PRIMARY_SCHOOL: "小学", + AccountType.MIDDLE_SCHOOL: "初中", + AccountType.HIGH_SCHOOL: "高中", + AccountType.INVALID: "未知" + } + return type_map[account_type] + + def parse_type_choice(self, choice: int) -> AccountType: + """ + 根据输入的序号解析对应的账户类型 + :param choice: 输入的序号(1代表小学,2代表初中,3代表高中) + :return: 对应的账户类型,无效输入返回INVALID + """ + choice_map = { + 1: AccountType.PRIMARY_SCHOOL, + 2: AccountType.MIDDLE_SCHOOL, + 3: AccountType.HIGH_SCHOOL + } + return choice_map.get(choice, AccountType.INVALID) \ No newline at end of file