Oldboy Day4 HW by 徐杰炀
模拟实现一个ATM + 购物商城程序
额度 15000或自定义
可以提现,手续费5%
支持多账户登录
支持账户间转账
提供还款接口
ATM记录操作日志
功能实现代码,造零件阶段,之后加入文件系统
import os os.chdir(os.path.dirname(__file__)) # 查看指定用户信息 def check(username): """ 查看指定用户金额 :param username: 指定用户名 :return: [username, pwd, credit, event] """ with open('users.txt', 'rt', encoding='utf-8') as f1: for line in f1: check_name = line.split('|', 1)[0] if check_name == username: account_info = line.split('|') return account_info # 添加ATM记录操作 def add_event(event, username): """ :param event: 记录事件 :param username: 用户名 :return:None """ with open('users.txt', 'rt', encoding='utf-8') as f1, \ open('.users.txt.swp', 'wt', encoding='utf-8') as f2: for line in f1: check_name = line.split('|', 1)[0] if check_name == username: f2.write(line.strip('\n') + event + ',\n') else: f2.write(line) os.remove('users.txt') os.rename('.users.txt.swp', 'users.txt') # 注册新用户,初始额度15000或者自定义 def register(username, pwd, credit=15000): """ :param username: 用户名 :param pwd: 密码 :param credit: 额度,金额预设15000 :return: True->成功 False->失败 """ succeeded_or_not = False with open('users.txt', 'rt', encoding='utf-8') as f1: for line in f1: check_name = line.split('|', 1)[0] if check_name == username: break else: with open('users.txt', 'at', encoding='utf-8') as f2: f2.write('{}|{}|{}|\n'.format(username, pwd, credit)) succeeded_or_not = True return succeeded_or_not # 更新用户金额 def update_credit(username, new_credit): """ 更新用户金额 :param username: 用户名 :param new_credit: 新金额 :return:None """ with open('users.txt', 'rt', encoding='utf-8') as f1, \ open('.users.txt.swp', 'wt', encoding='utf-8') as f2: for line in f1: check_name = line.split('|', 1)[0] if check_name == username: account_info = line.split('|') account_info[2] = str(new_credit) new_line = '|'.join(account_info) f2.write(new_line) else: f2.write(line) os.remove('users.txt') os.rename('.users.txt.swp', 'users.txt') # 提现,手续费5% def withdraw(username, amount): """ :param username: 用户名 :param amount: 提现金额 :return: True->提现成功,False->提现失败 """ succeeded_or_not = False amount = (1 + 0.05) * amount user_credit = check(username)[2] if amount <= float(user_credit): new_credit = float(user_credit) - amount update_credit(username, new_credit) succeeded_or_not = True return succeeded_or_not # 账户间转账 def transfer(from_username, to_username, amount): """ :param from_username: 转出用户名 :param to_username: 目标用户名 :param amount: 转账数额 :return: True->提现成功,False->提现失败 """ succeeded_or_not = False from_user_credit = check(from_username)[2] to_user_credit = check(to_username)[2] if amount <= float(from_user_credit): from_user_new_credit = float(from_user_credit) - amount to_user_new_credit = float(to_user_credit) + amount update_credit(from_username, from_user_new_credit) update_credit(to_username, to_user_new_credit) succeeded_or_not = True return succeeded_or_not if __name__ == '__main__': pass
之后写入主程序,主程序包括主运行逻辑以及用户界面,将零件装上去就可
浙公网安备 33010602011771号