def credit_huankuan(): show_current_user() current_credit = int(current_user_info_dic["current_credit"]) max_credit = int(current_user_info_dic["max_credit"]) balance = int(current_user_info_dic["balance"]) need_to_pay = max_credit - current_credit # 用储蓄账户换信用账户的逻辑 if need_to_pay == 0: # 不需要还款的情况 print("you do not need to 还款") elif need_to_pay <= balance and need_to_pay > 0: # 储蓄卡余额是足够的 # 额度恢复 current_user_info_dic["current_credit"] = current_credit + need_to_pay # 储蓄卡现金减少 current_user_info_dic["balance"] = balance - need_to_pay # 写进文件 all_user_info_dic[ current_user_info_dic["name"]] = current_user_info_dic common.write_to_file_with_json(all_user_info_dic, setting.ALL_USER_INFO_FILE_PATH) common.show_my_log("还款成功") # 显示当前用户信息 show_current_user() else: # 储蓄卡的余额不够 s = "you not have enough money, balance: {}, need_to_pay: {}".format( balance, need_to_pay) print(s)
def store_or_draw_money(action): while True: count = str(input("how much:")).strip() if count.isnumeric(): # 输入的是数字, 开始执行存钱或者取钱 逻辑 if action == "store" or action == 1: # 存钱 current_user_info_dic["balance"] += int(count) print("存钱成功") elif action == "draw" or action == 2: # 取钱 if current_user_info_dic["balance"] > int(count): current_user_info_dic["balance"] -= int(count) print("取钱成功") else: print("you do not have enough money") break elif action == "yxyk" or action == 3: # 使用信用卡 current_credit = int(current_user_info_dic["current_credit"]) if int(count) <= current_credit: # 信用卡的额度足够 make_bill_record( int(count)) # 该函数构造其他记录信息, update本次消费记录的数据到文件 current_user_info_dic[ "current_credit"] = current_credit - int(count) print("刷卡成功") else: # 信用卡的额度不够 print("you credit is not enough") return # 把当前用户信息写入到文件,如果前面操作成功,如果操作失败,会直接 return all_user_info_dic[ current_user_info_dic["name"]] = current_user_info_dic common.write_to_file_with_json(all_user_info_dic, setting.ALL_USER_INFO_FILE_PATH) common.show_my_log("action is success") # print(current_user_info_dic) show_current_user() break else: print("numeric is valid") continue
def make_bill_record(count): current_user_name = current_user_info_dic["name"] # 所有用户的账单记录 bill_record_with_user_dic = common.read_from_file_with_json( setting.ALL_USER_BILL_RECORD_FILE_PATH) # 当前用户的账单记录 bill_record_dic = bill_record_with_user_dic.setdefault( current_user_name, {}) # 构造本次的交易时间记录 time_stamp = time.time() count = int(count) project = input("购买了什么东西:") bill_record_dic[time_stamp] = {"project": project, "count": count} bill_record_with_user_dic[current_user_info_dic["name"]] = bill_record_dic common.write_to_file_with_json(bill_record_with_user_dic, setting.ALL_USER_BILL_RECORD_FILE_PATH) print("make bill record is success") return count
def add_user(): # 设计思路, 把所有的用户读出来,修改,然后再覆盖 all_user_info = fetch_all_user() current_user_info = input_user_info() # 密码使用md5算法加密,在存储 password_md5_after = common.hashlib_md5(current_user_info["password"]) current_user_info["password"] = password_md5_after # update all_user_info all_user_info[current_user_info["name"]] = current_user_info # override db # user_info_file_path = os.path.join(setting.APP_DIR, "db", "user_info.json") # with open(user_info_file_path, "w", encoding="utf-8") as user_info_file: # json.dump(all_user_info, user_info_file) # 利用自己在common module 写的函数,把当前用户信息直接写到文件 # 目录只是使用在setting中设置的 common.write_to_file_with_json(all_user_info, setting.ALL_USER_INFO_FILE_PATH) print(current_user_info) # 打印当前用户信息 print("add user success")
def transfer_accounts(): all_user_names = all_user_info_dic.keys() while True: dist_name = str(input("to:")) if dist_name not in all_user_names: # user is not exist print("user is not in system, please contact admin") continue else: # user is exist break while True: money = str(input("money:")) if not money.isnumeric(): print("just numeric is valid") continue else: money = int(money) break # 修改相应账户的数据 current_user_balance = current_user_info_dic["balance"] dist_user_info_dic = all_user_info_dic[dist_name] if current_user_balance < money: # 没有足够的钱转出 s = ''' you do not have enough money in you balance current balance: {balance} want to transfer: {money} '''.format(balance=current_user_balance, money=money) print(s) else: # 有足够的钱转出 current_user_info_dic["balance"] = current_user_balance - money dist_user_info_dic["balance"] += money all_user_info_dic[dist_name] = dist_user_info_dic common.write_to_file_with_json(all_user_info_dic, setting.ALL_USER_INFO_FILE_PATH) print("transfer account is success")