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 choose_class(self): # 选择班级 if not self.have_pay: print('请先缴费') return if self.classes: print('您选择好班级') return class_list = [(index, i.split('.')[0]) for index, i in enumerate(os.listdir(setting.CLASSES_DB)) ] while True: for i in class_list: print(i[0], i[1]) choose = common.must_digit() if choose >= len(class_list): print('没有该班级') continue print(class_list) print(class_list[choose][1]) classes = db_handler.load_db('classes', class_list[choose][1]) self.classes = classes classes.students.append(self) self.dump_db() classes.dump_db() self.__save_teachers() break
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 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 login(kind, name, password): obj = db_handler.load_db(kind, name) if not obj: print('没有该账号') return False if password == obj.password: print('登录成功') return obj print('密码错误')
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 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 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")
def choose_school(school_name): school = db_handler.load_db('school', school_name) return school