之前没有记着边学边写笔记,没有这样的习惯,现在来培养下。之前写了不少了,先贴上代码,之后再来补上具体的其他的。而且对于这里的笔记,不在考虑结构内容啥的,写完一个功能就会来总结下。还会有些突发奇想在里面,就像是得知自己癌症晚期后,想要记录自己的一切的那种想法是一样的想法。
User类
将所有的属性设置为非公开的属性,然后写get和set公开方法来提供公共的借口,但是我还没有写完,
class User:
"""用户"""
def __init__(self, name: str, number: str, password: str, balance: float):
self._user_name = name
self._user_number = number
self._password = password
self._balance = balance
def get_name(self) -> str:
return self._user_name
def set_name(self, new_name) -> bool:
self._user_name = new_name
return True
def get_number(self) -> str:
return self._user_number
def get_balance(self) -> float:
return self._balance
def set_balance(self, new_balance):
self._balance = new_balance
return True
def get_password(self):
return self._password
def __str__(self):
s = "用户名:" + self.get_name() + "\t卡号:" + self.get_number() + \
"\t余额:" + str(self.get_balance())
return s
if __name__ == '__main__':
user = User("张三", "123421", "124422", 12.0)
print(user)
Item类
from datetime import datetime
from User import User
class Item:
"""定义日志类"""
def __init__(self, user, type, money, status):
self._time = str(datetime.now())
self._user = user
self._type = type
self._money = money
self._status = status
def get_time(self):
return self._time
def get_user(self):
return self._user
def get_type(self):
return self._type
def get_money(self):
return self._money
def get_status(self):
return self._status
def __str__(self):
s = "[" + self.get_time() + "] " + self.get_user().get_name()\
+ " | " + self.get_type() + " | " + self.get_money() \
+ " | " + self.get_status()
return s
if __name__ == '__main__':
user = User("DK", "1433223", "123456", 1000)
item = Item(user, "存款", 100, "成功")
print(item)
DBHelper类
这类还挺复杂的
from config import *
import pymysql as msql
from Item import Item
from User import User
class DBHelper:
"""数据库助手,存数据,取数据"""
# -----------------------------默认直接链接数据库-------------------------
def __init__(self):
self.connect = msql.connect(host=MYSQL_HOST, user=MYSQL_USER,
port=MYSQL_PORT, password=MYSQL_PASSWORD,
db=MYSQL_DATABASE,
charset=MYSQL_DATABASE_CHARSET)
self.cursor = self.connect.cursor()
# ----------------------user对象的增删改查--------------------------------
def get_user(self, number: str) -> User:
"""查"""
self.connect.ping(reconnect=True)
sql = 'SELECT * FROM user ' \
'WHERE number=(%s)' % number
# self.connect.begin()
self.cursor.execute(sql)
result = self.cursor.fetchone()
if result:
user = User(number=result[0], name=result[1], password=result[2],
balance=result[3])
return user
def insert_user(self, user: User):
"""增"""
number = user.get_number()
name = user.get_name()
password = user.get_password()
balance = user.get_balance()
# print(number, name, password, balance) #
self.connect.ping(reconnect=True)
sql = "INSERT INTO user(number, name,password, balance)" \
"VALUES('%s', '%s', '%s', '%f')"\
% (number, name, password, balance)
try:
self.connect.begin()
self.cursor.execute(sql)
self.connect.commit()
except Exception as e:
print("新用户注册失败")
print(e.args)
self.connect.rollback()
return False
return True
def del_user(self, number: str):
"""删"""
sql = " DELETE FROM user WHERE number=%s" % number
try:
self.connect.begin()
self.cursor.execute(sql)
self.connect.commit()
except Exception as e:
print("用户销户失败")
print(e.args)
self.connect.rollback()
return False
return True
def update_user(self, change_item: str, change_value: str):
"""改"""
sql = "UPDATE user SET " + change_item + "=" + change_value
try:
self.connect.begin()
self.cursor.execute(sql)
self.connect.commit()
except Exception as e:
print("用户信息更改失败")
print(e.args)
self.connect.rollback()
return False
return True
# --------------------item记录的增删查-----------------------------
def insert_log(self, item: Item):
"""新加一条记录"""
number = item.get_user().get_number()
time = item.get_time()
name = item.get_user().get_name()
type = item.get_type()
money = item.get_money()
status = item.get_status()
balance = item.get_user().get_balance()
sql = "INSERT INTO log (number, time, name, type, money, status, " \
"balance)" \
"VALUES('%s', '%s', '%s','%s', '%f', '%s', '%f')" \
% (number, time, name, type, money, status, balance)
try:
self.connect.begin()
self.cursor.execute(sql)
self.connect.commit()
except Exception as e:
print("日志记录失败")
print(e.args)
self.connect.rollback()
return False
return True
def del_all_logs(self, number):
"""删除用户的所有记录,仅用于销户"""
sql = "DELETE FROM lOG WHERE numbers="+number
try:
self.connect.begin()
self.cursor.execute(sql)
self.connect.commit()
except Exception as e:
print("删除用户操作记录失败")
self.connect.rollback()
print(e.args)
return False
return True
# --------------------关闭数据库的连接-------------------------------
def close(self):
self.connect.close()
if __name__ == '__main__':
db = DBHelper()
user = User("张三", "123321", "971221", 1200)
item = Item(user, "存款", 1000, "成功")
db.insert_log(item)
db.close()
除此之外,还有数据库的相关配置,用来连接数据库。
# 用来保存相关的设置,数据库的相关信息等
MYSQL_HOST = 'localhost'
MYSQL_PORT = 3306
MYSQL_PASSWORD = 'password'
MYSQL_USER = 'root'
MYSQL_DATABASE = 'atm'
MYSQL_DATABASE_USER = "user"
MYSQL_DATABASE_LOG = 'log'
MYSQL_DATABASE_CHARSET = 'utf8'