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 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 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 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 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