def shopping_or_atm(user_data): """ 给出提示信息,用于选择是进行shopping操作还是进行atm操作 :return: """ while 1: info = """ ********************shopping or 5.atm******************* welcome come to this system! 1.for shopping 2.for 5.atm 3.exit ********************shopping or 5.atm******************* """ choice_list = {"1": shopping_run, "2": atm_run, "3": "exit"} print_info(info) your_choice = input("please input your choice:").strip() if your_choice.isdigit() and your_choice in choice_list: if choice_list[your_choice] == "exit": exit() choice_list[your_choice](user_data) else: print_info("your input must be a number showed before!", "error") continue
def atm_run(user_data): """ atm程序的入口,可以进行一些转账,还款的操作 :param user_data: :return: """ while 1: choice = ''' ------- Bank --------- 1. 账户信息 2. 还款 3. 取款 4. 转账 5. 账单 6. 退出 ------- Bank --------- ''' choice_list = { "1": show_account_info, "2": repay, "3": withdraw, "4": transfer, "5": show_bill, "6": "exit" } print_info(choice) your_choice = input("please input your choice:").strip() if your_choice in choice_list: if choice_list[your_choice] == "exit": exit() choice_list[your_choice](user_data) else: print_info("your input is illegal!", "error")
def transaction(user_data, money, tran_type): """ 根据tran_type,对用户账户中的金额进行加减操作 :param user_data: :param money: :param tran_type: :return: """ if tran_type in settings.TRANSACTION_TYPE: money = float(money) action = settings.TRANSACTION_TYPE[tran_type]["action"] interest = money * settings.TRANSACTION_TYPE[tran_type]["interest"] if action == "plus": user_data[ "left_credit"] = user_data["left_credit"] + money - interest # 减钱操作 elif action == "minus": user_data[ "left_credit"] = user_data["left_credit"] - money - interest if user_data["left_credit"] < 0: print_info("the money you left is not enough!") return None print_info("transaction successful!") user_file = os.path.join( settings.DATABASE["path"], '%s/%s.json' % (settings.DATABASE["name"], user_data["user_name"])) save_db(user_file, user_data) logger = return_logger_obj("transaction_log") logger.info("account:%s action:%s amount:%s interest:%s" % (user_data["user_name"], tran_type, money, interest)) return True
def save_to_shop_history(user_data, goods_name, goods_price): """ 购物完成,保存到购物信息中 :param user_data: :param goods_name: :param goods_price: :return: """ user_name = user_data["user_name"] file = os.path.join( settings.GOODS_DATABASE["path"], '%s/shop_history_%s.json' % (settings.GOODS_DATABASE["name"], user_name)) if os.path.exists(file): shop_history_data = load_db(file) # shop_history_data = { # "手机": {"price": "998", "count": 1}, # "裙子": {"price": "398", "count": 2}, # } if goods_name in shop_history_data: shop_history_data[goods_name]["count"] += 1 else: shop_history_data[goods_name] = {} shop_history_data[goods_name]["price"] = goods_price shop_history_data[goods_name]["count"] = 1 else: shop_history_data = dict() shop_history_data[goods_name] = {} shop_history_data[goods_name]["price"] = goods_price shop_history_data[goods_name]["count"] = 1 save_db(file, shop_history_data) print_info("you have shopped %s" % goods_name)
def welcome(): """ 程序最初的入口函数,可以选择登录,添加账户,修改额度等操作 :return: """ while 1: menu = ''' 1.登录 2.添加新用户 3.修改用户额度 4.退出 ''' menu_list = { "1": user_login, "2": add_new_user, "3": modify_credit, "4": exit } print_info(menu) your_choice = input("please input one number for your choice:").strip() if your_choice in menu_list: user_data = menu_list[your_choice]() if user_data is not None: shopping_or_atm(user_data) else: break else: print_info("your input is illegal!", "error") continue
def shopping_run(user_data): """ 购物的入口程序 :param user_data: :return: """ while True: info = ''' ***************shopping choice**************** 1.shopping 2.show shopping history 3.exit ***************shopping choice**************** ''' print_info(info) choice_list = {"1": shopping, "2": show_shopping_history, "3": "exit"} your_choice = input("please input your choice:").strip() if your_choice in choice_list: if choice_list[your_choice] == "exit": exit() choice_list[your_choice](user_data) else: print_info("your input must be a number as showed before!", "error")
def log_out(self): """ 可用于老师登录,学生登录,管理员登录之后的退出登录,进入初始页面的操作。 :return: """ if self.user_data["is_authenticated"]: username = self.user_data["account_data"].username self.user_data = {"is_authenticated": False, "account_data": None} print_info("%s logout successful!" % username)
def interactive(menu, menu_list): exit_flag = True while exit_flag: print_info(menu) your_choice = input("input your choice:").strip() if your_choice in menu_list: eval(menu_list[your_choice]) else: print_info("your input is illegal!", "error")
def show_bill(user_data): """ 显示当前登录人的账单 :param user_data: :return: """ transaction_log_file = settings.LOG_TYPES["transaction_log"] f = open(file=transaction_log_file, mode="r", encoding="utf-8") for line in f: # 取出当前登录用户的账单 if line.split("account:")[1].split( "action")[0].strip() == user_data["user_name"]: print_info(line)
def withdraw(user_data): """ 取款 :param user_data: :return: """ withdraw_money = input("input money you want to withdraw:").strip() # 匹配数字,包含小数,因为"12.23".isdigit()是false,这里通过正则表达式来进行匹配 if re.match("^[0-9]+[.]?[0-9]*$", withdraw_money): # user_data["left_credit"] -= withdraw_money transaction(user_data, withdraw_money, "withdraw") else: print_info("the money your input is illegal!", "error")
def repay(user_data): """ 还款功能 :param user_data: :return: """ repay_money = input("input money you want to repay:").strip() # 匹配数字,包含小数,因为"12.23".isdigit()是false,这里通过正则表达式来进行匹配 if re.match("^[0-9]+[.]?[0-9]*$", repay_money): # user_data["left_credit"] += repay_money transaction(user_data, repay_money, "repay") else: print_info("your input is illegal!", "error")
def get_dir_size_count(dir): """ 获得文件夹中所有文件大小和文件个数 :param dir: :return: """ size = 0 count = 0 for root, dirs, files in os.walk(dir): size_li = [os.path.getsize(os.path.join(root, name)) for name in files] size += sum(size_li) count += len(size_li) print_info('目录{} 文件个数{}, 总共大小约{}'.format(dir, count, bytes2human(size))) return count, bytes2human(size)
def modify_credit(): """ 修改用户额度操作 :return: """ while 1: user_name = input( "please input user name you want to change credit:").strip() user_db_file = os.path.join( settings.DATABASE["path"], '%s/%s.json' % (settings.DATABASE["name"], user_name)) if os.path.exists(user_db_file): user_data = load_db(user_db_file) input_password = input("please input the password:"******"password"] if input_password == real_password: credit = input("please input your new credit:").strip() if credit.isdigit(): user_data = load_db(user_db_file) user_data["credit"] = credit save_db(user_db_file, user_data) print_info("update credit successful!") return None else: print_info("you can just input one number!", "error") continue else: print_info("your password is not correct!", "error") else: print_info("user is not exist!", "error") continue
def user_login(): """ 用户登录程序,允许用户登录三次,一个用户三次登录错误,会锁定账户 :return: """ login_user = [] lock_status = True login_times = 0 user_data = {} user_name = "" login_user_file = "" while login_times < 3: logger = return_logger_obj("login_log") user_name = input("please input your name:").strip() password = input("please input your password:"******"path"], '%s/%s.json' % (settings.DATABASE["name"], user_name)) if os.path.isfile(login_user_file): # 从文件中取出数据 user_data = load_db(login_user_file) if password == user_data["password"]: if user_data["lock_status"] == "no": logger.info("Account [%s] has 1.login in successful!" % user_name) # user_data = {"user_name": "vita", "password": "******", "1.login": "******","lock_status":"no"} user_data["1.login"] = "******" return user_data else: print_info("Account [%s] has been locked!" % user_name, "error") else: print_info("Password of [%s] does not correct!" % user_name, "error") logger.error("Password of [%s] does not correct!" % user_name) else: print_info("Account [%s] does not exist!" % user_name, "error") logger.error("Account [%s] does not exist!" % user_name) lock_or_not(user_name, login_user) login_times += 1 else: # len(user_data) > 0 只有用户存在时,user_data才不为空 # user_data 为空就不需要设置lock_status了 print_info("you have tried too many times!", "error") if len(user_data) > 0: # {'user_name': 'vita', 'password': '******', 'lock_status': 'yes'} user_data = set_lock_status(user_name, user_data, lock_status) # 一个用户,三次登录失败,就锁定,并写入文件 save_db(login_user_file, user_data) # 条件不符合,返回None return None
def show_classes(self): """ 列出所管理的班级有哪些 :return: """ exit_flag = True while exit_flag: teacher_obj = self.user_data["account_data"] class_info = "" for class_name in teacher_obj.classes: class_info += class_name class_info += "\n" print_info(class_info) exit_flag = False
def create_class(self): """ 通过学校创建班级, 班级关联课程、讲师 :return: """ exit_flag = True while exit_flag: class_name = input("please input class name:").strip() associate_school_name = input( "please input associate school name:").strip() associate_course_name = input( "please input associate course name:").strip() associate_teacher_name = input( "please input associate teacher name:").strip() school_path = "%s/%s" % (SCHOOL_PATH, associate_school_name) if os.path.exists(school_path) and associate_school_name: school_data = self.school_obj.getter(associate_school_name, None) # print_info(school_data) if associate_course_name not in school_data["course"]: print_info( "course %s is not exist! you can create it!" % associate_course_name, "error") exit_flag = False elif associate_teacher_name not in school_data["teacher"]: print_info( "teacher %s is not exist! you can create it!" % associate_teacher_name, "error") exit_flag = False else: self.class_obj = self.class_obj.setter( class_name, associate_course_name, associate_school_name, associate_teacher_name) school_data["class"][class_name] = self.class_obj # 课程对象也与班级联系起来,用于展示该教该课程的班级有哪些,可展示出来供用户选择 school_data["course"][ associate_course_name].associate_class_names.append( class_name) # 创建班级的时候,分配了老师,这里为了后面展示该老师管理哪些班级,把班级信息加入到老师对象中 teacher_obj = school_data["teacher"][ associate_teacher_name] teacher_obj.classes.append(class_name) db_handler_obj = DbHandler(teacher_obj.username, "teacher", teacher_obj) db_handler_obj.save_to_db() school_db_handler_obj = SchoolDbHandler( associate_school_name, school_data) school_db_handler_obj.save_to_db() print_info( "you have add this class to the school file successful!" ) exit_flag = False else: print_info("this school is not exist!you can create it!", "error") exit_flag = False
def show_school_info(self): """ 展示学校信息 :return: """ exit_flag = True while exit_flag: school_info = "" school_name = input( "please input school name you want to show:").strip() school_path = "%s/%s" % (SCHOOL_PATH, school_name) if os.path.exists(school_path) and school_name: school_data = self.school_obj.getter(school_name, None) school_info += "class:\n" for class_name in school_data["class"]: school_info += "class_name:%s" % class_name school_info += " associate_course_name:%s" % \ school_data["class"][class_name].associate_course_name school_info += " associate_teacher_name:%s" % \ school_data["class"][class_name].associate_teacher_name school_info += "\n" school_info += "course:\n" for course_name in school_data["course"]: school_info += "course_name:%s" % course_name school_info += " period:%s" % school_data["course"][ course_name].period school_info += " rice:%s" % school_data["course"][ course_name].price school_info += " associate_class_names:%s" % \ str(school_data["course"][course_name].associate_class_names) school_info += "\n" school_info += "teacher:\n" for teacher_name in school_data["teacher"]: school_info += "teacher_name:%s" % teacher_name school_info += " classes managed: %s" % str( school_data["teacher"][teacher_name].classes) school_info += "\n" school_info += "student:\n" for student_name in school_data["student"]: school_info += "student_name:%s" % student_name school_info += "\n" print_info(school_info) exit_flag = False else: print_info("this school is not exist!you can create it!", "error") exit_flag = False
def create_school(self): """ 新建学校,保存到文件中 :return: """ exit_flag = True while exit_flag: school_name = input("please input school name:").strip() school_country = input("please input school country:").strip() school_city = input("please input school city:").strip() if school_name and school_country and school_city: school_path = "%s/%s" % (SCHOOL_PATH, school_name) if os.path.exists(school_path) and school_name: print_info("this school has already exist!", "error") else: school_obj = self.school_obj.setter( school_name, school_country, school_city) self.school_data["school"] = school_obj school_db_handler_obj = SchoolDbHandler( school_name, self.school_data) school_db_handler_obj.save_to_db() print_info( "you have add this school to the file successful!") exit_flag = False else: print_info("your input is illegal", "error") exit_flag = False
def login(self, user_type): """ 登录函数 :param user_type: :return: """ exit_flag = True while exit_flag: if not self.user_data["is_authenticated"]: username = input("input your name:").strip() password = input("input password:"******"1.login success!") # 保存登录用户的对象到user_data中 self.user_data["is_authenticated"] = True self.user_data["account_data"] = account_data return True else: print_info("username or password is not correct!", "error") exit_flag = False else: print_info( "there is something wrong with your username or user_type", "error") exit_flag = False else: return True
def create_course(self): """ 课程包含,周期,价格,通过学校创建课程 :return: """ exit_flag = True while exit_flag: course_name = input("please input course name:").strip() period = input("please input period[month]:").strip() price = input("please input price:").strip() associate_school_name = input( "please input associate school name:").strip() school_path = "%s/%s" % (SCHOOL_PATH, associate_school_name) if os.path.exists(school_path) and associate_school_name: if period.isdigit() and price.isdigit(): school_data = self.school_obj.getter( associate_school_name, None) self.course_obj = self.course_obj.setter( course_name, period, price, associate_school_name) school_data["course"][course_name] = self.course_obj school_db_handler_obj = SchoolDbHandler( associate_school_name, school_data) school_db_handler_obj.save_to_db() print_info( "you have add this course to the school file successful!" ) exit_flag = False else: print_info("price and period must be a number!", "error") exit_flag = False else: print_info("this school is not exist!you can create it!", "error") exit_flag = False
def set_student_record(self): """ 设置学生成绩 :return: """ exit_flag = True while exit_flag: student_name = input("please input student name:").strip() student_record = input("please set record:").strip() if student_name not in self.school_data["student"]: print_info("the student not exist!", "error") exit_flag = False elif not student_record.isdigit(): print_info("record can just be a number!", "error") exit_flag = False else: student_obj = self.school_data["student"][student_name] student_obj.record = student_record db_handler_obj = DbHandler(student_obj.username, "student", student_obj) db_handler_obj.save_to_db() school_db_handler_obj = SchoolDbHandler( student_obj.associate_school_name, self.school_data) school_db_handler_obj.save_to_db() print_info("set record successful!") exit_flag = False
def choose_class(self): """ 选择班级 :return: """ exit_flag = True while exit_flag: class_choice = input( "please choose one class you want to manage:").strip() if class_choice not in self.user_data["account_data"].classes: print_info("your input class not exist!", "error") exit_flag = False else: self.class_choice = class_choice print_info("you have choose %s class successful!" % class_choice) teacher_obj = self.user_data["account_data"] associate_school_name = teacher_obj.associate_school_name school_data = self.school_obj.getter(associate_school_name, None) self.school_data = school_data exit_flag = False
def show_student_info(self): """ 展示学生信息 :return: """ exit_flag = True while exit_flag: student_obj = self.user_data["account_data"] student_info = "" student_info += "username:%s \n" % student_obj.username if student_obj.associate_school_name: student_info += "associate_school_name:%s \n" % student_obj.associate_school_name if student_obj.associate_course_name: student_info += "associate_course_name:%s \n" % student_obj.associate_course_name if student_obj.associate_course_price: student_info += "associate_course_price:%s \n" % student_obj.associate_course_price if student_obj.associate_class_name: student_info += "associate_class_name:%s \n" % student_obj.associate_class_name if student_obj.record: student_info += "record:%s\n" % student_obj.record print_info(student_info) exit_flag = False
def transfer(user_data): """ 转账 :param user_data: :return: """ transfer_user_name = input( "input the user name you want to transfer:").strip() transfer_money = input("input the money you want to transfer:").strip() transfer_user_file = os.path.join( settings.DATABASE["path"], '%s/%s.json' % (settings.DATABASE["name"], transfer_user_name)) if os.path.isfile(transfer_user_file): if re.match("^[0-9]+[.]?[0-9]*$", transfer_money): # 当前用户减去传输的金额 transaction(user_data, transfer_money, "transfer") # 另一个用户加上传输的金额 transfer_user_data = load_db(transfer_user_file) transaction(transfer_user_data, transfer_money, "repay") else: print_info("the money your input is illegal!", "error") else: print_info("transfer user is not exist!", "error")
def show_shopping_history(user_data): """展示购物历史信息""" user_name = user_data["user_name"] file = os.path.join( settings.GOODS_DATABASE["path"], '%s/shop_history_%s.json' % (settings.GOODS_DATABASE["name"], user_name)) if os.path.exists(file): shopping_history_data = load_db(file) print_info("************3.shop history****************") for goods_name in shopping_history_data: goods_price = shopping_history_data[goods_name]["price"] goods_count = shopping_history_data[goods_name]["count"] print_info("%s,%s,%s" % (goods_name, goods_price, goods_count)) print_info("************3.shop history****************") else: print_info("this user has never shopped something!", "error")
def show_account_info(user_data): """ 显示用户的账户信息 :param user_data: :return: """ print_info("*************account info***************") for key, value in user_data.items(): print_info("%15s:%s" % (key, value)) print_info("*************account info***************")
def add_new_user(): """ 添加新用户,输入用户名和密码,其余信息是添加的时候就默认的 :return: """ # 这些值在申请信用卡的时候已经是默认的了 new_user = { "lock_status": "no", "credit": 15000, "left_credit": 15000, "repay_day": 22 } your_choice = input( "do you want to add a new uer[YES/NO]?:").strip().lower() if your_choice == 'yes': while 1: user_name = input("please input new user name:").strip() new_user_db_file = os.path.join( settings.DATABASE["path"], '%s/%s.json' % (settings.DATABASE["name"], user_name)) if os.path.exists(new_user_db_file): print_info("this user has already exist!", "error") continue password = input("please input password:"******"username or password can not be null!", "error") continue enroll_date = time.strftime("%Y-%m-%d", time.gmtime()) # 2024-04-27 14:11:20.520427转换为字符串后,在进行切割,得到年月日 # 假设有效期是5年 expire_date = str(datetime.datetime.now() + datetime.timedelta(5 * 365)).split()[0] new_user["user_name"] = user_name new_user["password"] = password new_user["enroll_date"] = enroll_date new_user["expire_date"] = expire_date save_db(new_user_db_file, new_user) print_info("add new user success!") return None
def create_teacher(self): """ 创建讲师角色时要关联学校 :return: """ exit_flag = True while exit_flag: teacher_name = input("please input teacher name:").strip() teacher_password = input("please input teacher password:"******"please input associate school name:").strip() school_path = "%s/%s" % (SCHOOL_PATH, associate_school_name) if os.path.exists(school_path) and associate_school_name: school_data = self.school_obj.getter(associate_school_name, None) self.teacher_obj = self.teacher_obj.teacher_setter( teacher_name, teacher_password, "teacher", associate_school_name) if self.teacher_obj: school_data["teacher"][teacher_name] = self.teacher_obj school_db_handler_obj = SchoolDbHandler( associate_school_name, school_data) school_db_handler_obj.save_to_db() db_handler_obj = DbHandler(teacher_name, "teacher", self.teacher_obj) db_handler_obj.save_to_db() print_info( "you have create this teacher and add this teacher to the school file successful!" ) exit_flag = False else: # 为FALSE,说明老师已经存在了 print_info("teacher has already exist!", "error") exit_flag = False else: print_info("this school is not exist!you can create it!", "error") exit_flag = False
def list_student(self): """ 列出选择的班级中的学生 :return: """ exit_flag = True while exit_flag: if self.class_choice: student_info = "" if len(self.school_data["class"][ self.class_choice].students) == 0: print_info("there is not student in this class now") exit_flag = False else: for student_name in self.school_data["class"][ self.class_choice].students: student_info += student_name student_info += "\n" print_info(student_info) exit_flag = False else: print_info("you must choose one class first!", "error") exit_flag = False
def shopping(user_data): """ 购物程序,调用transaction函数进行结账 :param user_data: :return: """ file = os.path.join( settings.GOODS_DATABASE["path"], '%s/%s.json' % (settings.GOODS_DATABASE["name"], "goods")) goods_data = load_db(file) print_info("***************goods info**************** ") for goods in goods_data: print_info("%s,%s,%s" % (goods_data.index(goods), goods["name"], goods["price"])) your_choice = input("you can input one number for shopping: ").strip() if your_choice.isdigit() and int(your_choice) < len(goods_data): your_choice = int(your_choice) goods_price = goods_data[your_choice]["price"] if transaction(user_data, goods_price, "consume"): goods_name = goods_data[your_choice]["name"] save_to_shop_history(user_data, goods_name, goods_price) else: print_info("your input is illegal!", "error")