项目说明:
用python实现一个能在控制台输出的简单系统,数据是储存在电脑磁盘上的txt文件中。这里有两个文件,一个账号储存文本,一个交易数据储存文本。
系统实现功能说明:
1、用户可以登陆自己的账户
2、用户可以存取款,如果金额单笔交易大于2W,则禁止交易,小于100禁止交易。
3、用户可以给其他账户转账。
4、用户可以查询到转账记录(转出、转入)。
5、有管理员用户,管理员用户可以查看所有用户的账户信息和金额。
6、管理员用户可管理所有人的交易信息。
7、记录用户登陆信息
8、记录存取款信息
项目实现代码:
使用了两个模块:
import re
import time
1、用户定义:
class Person():
def __init__(self):
self.name = ""
self.password = ""
2、用户账户:
class User():
def login(self):
# global name
# global password
print("------------------------------")
self.name = input(" 姓名:")
self.password = input(" 密码:")
print("------------------------------")
fail = open(r"D:\学习\python\python阶段性任务\账号.txt", "r", encoding="utf-8")
string = fail.read()
a = '用户名:"(.*?)"'
b = '密码:"(.*?)"'
l1 = re.findall(a, string)
l2 = re.findall(b, string)
while True:
for i in range(len(l1)):
if self.name == l1[i] and self.password == l2[i]:
print("用户名密码正确!正在进入用户界面。")
now_time = time.strftime("%Y年%m月%d日 %H:%M:%S:")
fail = open(r"D:\学习\python\python阶段性任务\记录.txt", "a", encoding="utf-8")
fail.write(f"登录时间:{self.name}在{now_time}登录银行记账系统。")
fail.write("\n")
fail.close()
self.interface()
return True
else:
print("用户名密码有误,重新操作(返回主界面输入1,重新输入用户名输入2)")
k = input("")
if k == "1":
Main()
return
else:
self.login()
return
def interface(self):
print("---------------------------------")
print("用户您好! ")
print(" 存款(1) ")
print(" 取款(2) ")
print(" 转账(3) ")
print(" 查询余额(4) ")
print(" 转账记录(5) ")
print(" 退出(6) ")
print(" ")
print("---------------------------------")
a = input("输入操作:")
if a == "1":
self.Deposit()
elif a == "2":
self.Withdraw()
elif a == "3":
self.Transfer()
elif a == "4":
self.Inquire()
elif a == "5":
self.Transfer_recording()
elif a == "6":
print(" 再 见 ! ")
else:
print("输入无效!请重新输入")
self.interface()
return
def Deposit(self):
fail = open(r"D:\学习\python\python阶段性任务\记录.txt", "r", encoding="utf-8")
string = fail.read()
l = re.findall(r"{}.*余额:(.*\d)".format(self.name), string)
fail.close()
q = l[len(l) - 1]
a = float(input("您要存钱的金额:"))
if a < 100:
print("金额过低禁止交易.")
k = input("返回(1) 重新输入金额(2):")
if k == "1":
self.interface()
else:
return self.Deposit()
elif a > 20000:
print("金额过高请到银行柜台进行交易")
k = input("返回(1) 重新输入金额(2):")
if k == "1":
self.interface()
else:
return self.Deposit()
else:
q = float(q)
q = q + a
print(f"存入{a}元成功!")
fail = open(r"D:\学习\python\python阶段性任务\记录.txt", "a", encoding="utf-8")
fail.write(f"用户名:{self.name} 存入{a}元 余额:{q}元")
fail.write("\n")
fail.close()
while True:
k = input("返回主界面(1) 退出(2):")
if k == "1":
self.interface()
return
elif k == "2":
print(" 再 见 ! ")
break
else:
print("输入有误")
continue
def Withdraw(self):
fail = open(r"D:\学习\python\python阶段性任务\记录.txt", "r", encoding="utf-8")
string = fail.read()
l = re.findall(r"{}.*余额:(.*\d)".format(self.name), string)
fail.close()
q = l[len(l) - 1]
a = float(input("您要取钱的金额:"))
q = float(q)
q = q - a
if a < 100:
print("金额过低禁止交易.")
k = input("返回(1) 重新输入金额(2):")
if k == "1":
self.interface()
else:
return self.Withdraw()
elif a > 20000:
print("金额过高请到银行柜台进行交易")
k = input("返回(1) 重新输入金额(2):")
if k == "1":
self.interface()
else:
return self.Withdraw()
elif q < 0:
print(f"您的存款不够取{a}元!")
k = input("返回(1) 重新输入金额(2):")
if k == "1":
self.interface()
else:
return self.Withdraw()
else:
print(f"取走{a}元成功!")
fail = open(r"D:\学习\python\python阶段性任务\记录.txt", "a", encoding="utf-8")
fail.write(f"用户名:{self.name} 取走{a}元 余额:{q}元 ")
fail.write("\n")
fail.close()
while True:
k = input("返回主界面(1) 退出(2):")
if k == "1":
return self.interface()
elif k == "2":
print(" 再 见 ! ")
break
else:
print("输入有误")
continue
def Transfer(self):
fail = open(r"D:\学习\python\python阶段性任务\记录.txt", "r", encoding="utf-8")
string = fail.read()
l = re.findall(r"{}.*余额:(.*\d)".format(self.name), string)
fail.close()
q = l[len(l) - 1]
name1 = input("您要向谁转账:")
fail1 = open(r"D:\学习\python\python阶段性任务\账号.txt", "r", encoding="utf-8")
string1 = fail1.read()
fail1.close()
a = '用户名:"(.*?)"'
R = re.findall(a, string1)
while True:
for i in range(len(R)):
if name1 == R[i]:
print("用户存在请稍等")
a = float(input("您要转账的金额:"))
q = float(q)
q = q - a
if a < 100:
print("金额过低禁止交易.")
k = input("返回(1) 重新输入金额(2):")
if k == "1":
return self.interface()
else:
return self.Transfer()
elif a > 20000:
print("金额过高请到银行柜台进行交易")
k = input("返回(1) 重新输入金额(2):")
if k == "1":
return self.interface()
else:
return self.Transfer()
elif q < 0:
print(f"您的余额不足{a}元")
k = input("返回(1) 重新输入金额(2):")
if k == "1":
return self.interface()
else:
return self.Transfer()
else:
print(f"转账{a}元成功!")
fail = open(r"D:\学习\python\python阶段性任务\记录.txt", "a", encoding="utf-8")
l1 = re.findall(r"{}.*余额:(.*\d)".format(name1), string)
q1 = l1[len(l1) - 1]
q1 = float(q1)
q1 = q1 + a
fail.write(f"{self.name} 向 {name1} 转账 {a}元 ")
fail.write("\n")
fail.write(f"用户名:{self.name} 转出{a}元 余额:{q}元")
fail.write("\n")
fail.write(f"用户名:{name1} 收入{a}元 余额:{q1}元")
fail.write("\n")
fail.close()
while True:
k = input("返回主界面(1) 退出(2):")
if k == "1":
return self.interface()
elif k == "2":
print(" 再 见 ! ")
break
else:
print("输入有误")
continue
else:
print("您要转账的用户不存在,请重新输入!")
return self.Transfer()
def Inquire(self):
fail = open(r"D:\学习\python\python阶段性任务\记录.txt", "r", encoding="utf-8")
string = fail.read()
l = re.findall(r"{}.*余额:(.*\d)".format(self.name), string)
fail.close()
q = l[len(l) - 1]
print(f"您的余额为{q}元")
while True:
k = input("返回主界面(1) 退出(2):")
if k == "1":
return self.interface()
elif k == "2":
print(" 再 见 ! ")
break
else:
print("输入有误")
continue
def Transfer_recording(self):
fail = open(r"D:\学习\python\python阶段性任务\记录.txt", "r", encoding="utf-8")
string = fail.read()
l = re.findall(r"{}.*向.*".format(self.name), string)
fail.close()
for i in range(len(l)):
print(l[i])
while True:
k = input("返回主界面(1) 退出(2):")
if k == "1":
return self.interface()
elif k == "2":
print(" 再 见 ! ")
break
else:
print("输入有误")
continue
3、系统管理员
class System():
def __init__(self):
self.Admin_name = "admin"
self.Admin_password = "admin"
def s_login(self):
n = input("请输入管理员用户名:")
p = input("请输入管理员密码:")
if n == self.Admin_name and p == self.Admin_password:
print("输入正确!正在进入管理员界面。")
return self.Admin()
else:
while True:
k = input("返回主界面(1) 重新输入(2):")
if k == "1":
return Main()
elif k == "2":
return self.s_login()
else:
print("输入有误")
continue
def Admin(self):
print("---------------------------------")
print("您好,系统管理员! ")
print(" ")
print(" 查看用户(1) ")
print(" ")
print(" 交易信息(2) ")
print(" ")
print(" 返回主页(3) ")
print(" ")
print(" 退出系统(4) ")
print("---------------------------------")
a = input("输入操作:")
if a == "1":
self.Select_User()
elif a == "2":
self.Trading_Information()
elif a == "3":
Main()
elif a == "4":
print(" 再 见 ! ")
else:
print("输入无效!请重新输入")
self.Admin()
return
def Select_User(self):
fail = open(r"D:\学习\python\python阶段性任务\账号.txt", "r", encoding="utf-8")
string = fail.read()
a = r"用户名:.*"
l = re.findall(a, string)
for i in range(len(l)):
print(l[i])
while True:
k = input("返回主界面(1) 查看账号余额(2) 退出(3):")
if k == "1":
return self.Admin()
elif k == "2":
return self.Balance()
elif k == "3":
print(" 再 见 ! ")
break
else:
print("输入有误")
continue
def Balance(self):
fail = open(r"D:\学习\python\python阶段性任务\记录.txt", "r", encoding="utf-8")
string = fail.read()
user_name = input("查询的用户名:")
l = re.findall(r"{}.*余额:(.*\d)".format(user_name), string)
fail.close()
q = l[len(l) - 1]
print(f"{user_name}的余额为{q}元")
while True:
k = input("返回主界面(1) 返回上一级(2) 退出(3):")
if k == "1":
return self.Admin()
elif k == "2":
return self.Select_User()
elif k == "3":
print(" 再 见 ! ")
break
else:
print("输入有误")
continue
def Trading_Information(self):
fail0 = open(r"D:\学习\python\python阶段性任务\账号.txt", "r", encoding="utf-8")
string = fail0.read()
fail0.close()
a = r'(用户名:.*" )'
l0 = re.findall(a, string)
for i in range(len(l0)):
print(l0[i])
fail = open(r"D:\学习\python\python阶段性任务\记录.txt", "r", encoding="utf-8")
string = fail.read()
user_name = input("查询的用户名:")
l = re.findall(r"{}.*".format(user_name), string)
fail.close()
for i in range(len(l)):
print(l[i])
while True:
k = input("返回主界面(1) 退出(2):")
if k == "1":
return self.Admin()
elif k == "2":
print(" 再 见 ! ")
break
else:
print("输入有误")
continue
4、用户注册
class Registered():
def __init__(self):
self.User_name = ""
self.User_password = ""
def registered(self):
fail0 = open(r"D:\学习\python\python阶段性任务\账号.txt", "r", encoding="utf-8")
string = fail0.read()
l1 = re.findall(r'用户名:"(.*?)"', string)
fail0.close()
self.User_name = input("输入用户名:")
self.User_password = input("输入密码:")
l2 = []
for i in range(len(l1)):
if l1[i] == self.User_name:
l2.append(l1[i])
if l2 != []:
k = input("该用户名重复,请重新输入(1)或者返回主页面(2):")
if k == "1":
return self.registered()
else:
return Main()
else:
fail = open(r"D:\学习\python\python阶段性任务\账号.txt", "a", encoding="utf-8")
fail.write(f'用户名:"{self.User_name}" 密码:"{self.User_password}"')
fail.write("\n")
fail.close()
fail1 = open(r"D:\学习\python\python阶段性任务\记录.txt", "a", encoding="utf-8")
fail1.write(f'用户名:{self.User_name} 余额:0元"')
fail1.write("\n")
fail1.close()
print("注册成功!")
while True:
k = input("返回主界面(1)退出(2):")
if k == "1":
return Main()
elif k == "2":
print(" 再 见 ! ")
break
else:
print("输入有误")
continue
Main()函数
def Main():
print("------------------------------------------------")
print(" 用户登录(1) ")
print(" ")
print(" 管理员登录(账号密码都是admin)(2) ")
print(" ")
print(" 注册(3) ")
print(" ")
print(" 退出系统(4) ")
print("------------------------------------------------")
a = input("输入操作:")
if a == "1":
s = User()
s.login()
elif a == "2":
s = System()
s.s_login()
elif a == "3":
s = Registered()
s.registered()
elif a == "4":
print(" 再 见 ! ")
else:
print("输入无效!")
Main()
return
Main()