#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# ----------------------------------------------------------#
# Date : 2020-09-17 #
# Author : Created by zhouwanchun. #
# Wechat : WGzhouwanchu #
# Function: This scripts function is ... #
# Version : 4.0 #
# ----------------------------------------------------------#
# MySQL建库建表。
# create database atm charset=utf8mb4;
# CREATE TABLE `user` (
# `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '自增ID',
# `username` varchar(64) NOT NULL COMMENT '账户',
# `password` varchar(64) NOT NULL COMMENT '密码',
# `money` varchar(10) NOT NULL COMMENT '余额',
# `tel` char(11) NOT NULL COMMENT '手机号',
# `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '账户创建时间',
# `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '数据最后修改时间',
# PRIMARY KEY (`id`)
# ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='账户表';
import mysql.connector
# import myloginpath
import os
import time
# 只要用户已经登录进来,就给他一个临时身份字典,这样就避免了每次都去读用户表信息;同时用于用户在线校验(建议写装饰器)。
atm_user = {'username': 'None', 'password': '', 'status': 'offline'}
# conn_user = "zhouwanchun"
# mylogin = myloginpath.parse(conn_user)
# print(mylogin, type(mylogin))
# conn = mysql.connector.connect(**mylogin)
conn = mysql.connector.connect(
host='10.0.0.11',
port=3306,
user='zhouwanchun',
password='123',
charset='utf8'
)
sql_cmd = conn.cursor()
# 校验账户是否存在。
def check_username(username):
sql1 = "select username from atm.user;"
sql_cmd.execute(sql1)
result = sql_cmd.fetchall()
# print(result, type(result))
for i in result:
if username == i[0]:
return True
else:
return False
# 校验密码是否正确。
def check_password(username, password):
sql1 = "select username,password from atm.user where username=%s;"
values = (username,)
sql_cmd.execute(sql1, values)
result = sql_cmd.fetchall()
# print(result, type(result))
for i in result:
if password == i[1]:
atm_user['username'] = username
atm_user['password'] = password
atm_user['status'] = 'online'
return True
else:
return False
# insert_atm_user
def insert_atm_user(username, password, money, tel):
sql1 = "insert into atm.user(username,password,money,tel) values(%s,%s,%s,%s);"
values = [(username, password, money, tel), ]
sql_cmd.executemany(sql1, values)
conn.commit()
print("[", sql_cmd.rowcount, "]", "\033[1;32m数据插入成功!\033[0m")
# 1.登录
def login():
print("\033[1;32m-------------------------欢迎进入用户登录页面-------------------------\033[0m")
while True:
username = input("请输入你的账户名: ").strip()
if check_username(username):
password = input("请输入你的密码: ").strip()
if check_password(username, password):
print("\033[1;32m登录成功!\033[0m")
return True
else:
print("\033[1;31m你输入的密码不对!\033[0m")
break
else:
print("\033[1;31m该账户名不存在!\033[0m")
break
# 2.注册
def register():
print("\033[1;32m-------------------------欢迎进入账户注册页面-------------------------\033[0m")
username = input("请注册你的账户: ").strip()
if username.isalpha():
if not check_username(username):
password = input("请设置你的密码: ").strip()
if not len(password) < 3:
tel = input("请输入你的手机号: ").strip()
if tel.isdecimal() and len(tel) == 11:
money = 0
insert_atm_user(username, password, money, tel)
print("\033[1;32m恭喜你,你已经注册成功,请开始登录使用吧!\033[0m")
else:
print("\033[1;31m请输入标准的11为数字手机号码!\033[0m")
else:
print("\033[1;31m密码字符长度不可以小于3个!\033[0m")
else:
print("\033[1;35m" + username + "\033[0m" + " \033[1;31m账户名已经被注册过!\033[0m")
else:
print("\033[1;31m用户名填写格式不对,用户名必须是字母组成!\033[0m")
# 3.余额
def balance():
if_online = online()
if if_online == 'online':
print("\033[1;32m-------------------------正在查询当前账户余额信息-------------------------\033[0m")
sql1 = "select username,money from atm.user where username=%s;"
values = (atm_user['username'],)
sql_cmd.execute(sql1, values)
result = sql_cmd.fetchall()
for i in result:
print("你的余额: [ " + "\033[1;33m" + i[1] + "\033[0m" + " ¥ ]")
return int(result[0][1])
else:
print("\033[1;31m请先登录你的账户!\033[0m")
# 4.存款
def deposit():
if_online = online()
if if_online == 'online':
save_money = input("请输入你要存款的金额: ").strip()
if save_money.isdecimal():
sql1 = "select username,money from atm.user where username=%s;"
val1 = (atm_user['username'],)
sql_cmd.execute(sql1, val1)
result = sql_cmd.fetchall()
before_money = int(result[0][1]) # 之前的钱
save_money = int(save_money) # 存款金额
last_money = before_money + save_money # 最后更新的余额
sql2 = "update atm.user set money=%s where username=%s;"
val2 = (last_money, atm_user['username'],)
sql_cmd.execute(sql2, val2)
conn.commit()
print("[", sql_cmd.rowcount, "]", "\033[1;32m数据更新成功!\033[0m")
else:
print("\033[1;31m请不要使用假币!存款金额应该为数字!\033[0m")
else:
print("\033[1;31m请先登录你的账户!\033[0m")
# 5.取款
def outmoney():
if_online = online()
if if_online == 'online':
out_money = input("请输入你要取款的金额: ").strip()
if out_money.isdecimal():
sql1 = "select username,money from atm.user where username=%s;"
val1 = (atm_user['username'],)
sql_cmd.execute(sql1, val1)
result = sql_cmd.fetchall()
before_money = int(result[0][1]) # 之前的钱
out_money = int(out_money) # 取款金额
last_money = before_money - out_money # 最后更新的余额
if out_money <= before_money:
sql2 = "update atm.user set money=%s where username=%s;"
val2 = (last_money, atm_user['username'],)
sql_cmd.execute(sql2, val2)
conn.commit()
print("[", sql_cmd.rowcount, "]", "\033[1;32m数据更新成功!\033[0m")
else:
print("\033[1;31m账户余额不足!\033[0m")
else:
print("\033[1;31m取款金额应该为数字!\033[0m")
else:
print("\033[1;31m请先登录你的账户!\033[0m")
# 6.修改密码
def change_password():
if_online = online()
if if_online == 'online':
org_passowrd = input("请输入原密码: ").strip()
sql1 = "select username,password from atm.user where username=%s;"
val1 = (atm_user['username'],)
sql_cmd.execute(sql1, val1)
result = sql_cmd.fetchall()
if org_passowrd == result[0][1]:
new1_password = input("请设置新密码: ").strip()
new2_password = input("请再次确认新密码: ").strip()
if new1_password == new2_password:
if new1_password.isdecimal() and new2_password.isdecimal() and len(new1_password and new2_password) == 6:
sql2 = "update atm.user set password=%s where username=%s;"
val2 = (new2_password, atm_user['username'],)
sql_cmd.execute(sql2, val2)
conn.commit()
atm_user['status'] = 'offline'
atm_user['username'] = 'None'
print("\033[1;32m密码修改成功!请重新登录!\033[0m")
else:
print("\033[1;31m密码填写格式不对,必须由6位数字组成!\033[0m")
else:
print("\033[1;31m两次设置输入新密码不一致,修改密码失败!\033[0m")
else:
print("\033[1;31m原密码错误!\033[0m")
else:
print("\033[1;31m请先登录你的账户!\033[0m")
# 7.注销账号
def delete_user():
if_online = online()
if if_online == 'online':
balance_num = balance()
if not balance_num == 0:
print("请先取走你所有的余额!避免事后纠纷!")
else:
print("请你再次确认是否注销当前的账号,账号注销成功后将无法找回!")
delete_ok = input("请你确认,注销Yes \ 不注销No! ---> ").strip()
if delete_ok == "yes":
sql1 = "delete from atm.user where username=%s;"
val1 = (atm_user['username'],)
sql_cmd.execute(sql1, val1)
conn.commit()
print("\033[1;32m你的账号 \033[0m" + atm_user['username'] + "\033[1;32m 已经注销,欢迎你下次注册使用!\033[0m")
atm_user['username'] = 'None'
atm_user['password'] = ''
atm_user['status'] = 'offline'
elif delete_ok == "no":
pass
else:
print("\033[1;31m请输入的指令不对!\033[0m")
else:
print("\033[1;31m请先登录你的账户!\033[0m")
# 8.退出
def logout():
choice_exit = input("退出ATM请输入q, 返回上一级请输入b: ").strip()
if choice_exit == 'q':
atm_user['status'] = 'offline'
print("\033[1;36m欢迎下次光临,你已经退出ATM。\033[0m")
exit()
elif choice_exit == 'b':
pass
# 9.查询个人资料
def oneself_message():
if_online = online()
if not if_online == 'online':
print("\033[1;31m请先登录你的账户!\033[0m")
else:
print("\033[1;32m-------------------------正在查询当前账户个人信息-------------------------\033[0m")
sql1 = "select username,password,money,tel,create_time from atm.user where username=%s;"
values = (atm_user['username'],)
sql_cmd.execute(sql1, values)
result = sql_cmd.fetchall()
username = result[0][0]
password = len(result[0][1]) * "*"
money = result[0][2]
tel = result[0][3]
create_time = str(result[0][4])
print("账号: " + username)
print("密码: " + password)
print("余额: " + money + "¥")
print("手机号: " + tel)
print("账号注册时间: " + create_time)
# atm业务菜单(字典)
menu_dict = {
1: login, # 登录
2: register, # 注册
3: balance, # 余额
4: deposit, # 存款
5: outmoney, # 取款
6: change_password, # 修改密码
7: delete_user, # 注销账号
8: logout, # 退出
9: oneself_message, # 查询个人资料
}
def menu():
""" 打印ATM菜单选项 """
print("""\033[1;32m
\033[1;36m1 登录\033[0m
\033[1;36m2 注册\033[0m
\033[1;36m3 余额\033[0m
\033[1;36m4 存款\033[0m
\033[1;36m5 取款\033[0m
\033[1;36m6 修改密码\033[0m
\033[1;31m7 注销账号\033[0m
\033[1;36m8 退出\033[0m
\033[1;36m9 查询个人资料\033[0m
""")
def online():
""" 当前账号是否在线 """
if atm_user['status'] == 'online':
return "online"
else:
return "offline"
def run():
while True:
menu()
# online()
print('当前账户: ' + "\033[1;32m" + atm_user['username'] + "\033[0m" + '\t\t状态: ' + "\033[1;32m" + atm_user['status'] + "\033[0m")
num = input("请输入你要办理的业务编号: ").strip()
if num.isdecimal():
num = int(num)
if 0 < num <= 9:
if num == 1 or num == 2:
if atm_user['status'] == 'online':
print("\033[1;31m当前账户正在登录,请先退出,否则不可以直接跳转到其他账户登录界面!更不能进行新账户注册业务!\033[0m")
else:
menu_dict[num]()
else:
menu_dict[num]()
else:
print("\033[1;31m你输入的不在办理业务的数字范围内,请重新输入!\033[0m")
else:
print("\033[1;31m你输入的不是业务办理的数字,请重新输入!\033[0m")
# 入口函数
if __name__ == "__main__":
os.system('clear')
run()