用戶類
# -*- coding: utf-8 -*-
"""
Created on Tue Jul 23 09:57:05 2019
@author: Mr.Ming
"""
class User:
def __init__(self, userName = None, password = None):
self.userName = userName
self.__password = password
def getUserName(self):
return self.userName
def setUserName(self, userName):
self.userName = userName
def getpassword(self):
return self.__password
def setPassword(self, password):
self.__password = password
def __repr__(self):
return "User{" + "userName=" + self.userName + "}"
書籍類
# -*- coding: utf-8 -*-
"""
Created on Mon Jul 22 23:13:21 2019
@author: Mr.Ming
"""
class Book:
def __init__(self, Id = None, bookName = None, info = None):
self.id = Id
self.bookName = bookName
self.info = info
def getId(self):
return self.id
def setID(self, Id):
self.id = Id
def getBookName(self):
return self.bookName
def setBookName(self, bookName):
self.bookName = bookName
def getInfo(self):
return self.info
def setInfo(self, info):
self.info = info
def __repr__(self):
return "Book{" + "id=" + str(hash(self.id)) + ", bookName='" + self.bookName + "'" + ", info='" + self.info + "'}"
借閱記錄類
# -*- coding: utf-8 -*-
"""
Created on Tue Jul 23 10:55:46 2019
@author: Mr.Ming
"""
class Order:
def __init__(self, timer = None, bookName = None):
self.timer = timer
self.bookName = bookName
def getTimer(self):
return self.timer
def getBookName(self):
return self.bookName
def __repr__(self):
return "Order{" + "timer=" + self.timer + ", books='" + self.bookName +
書籍管理系統(tǒng)主程序
# -*- coding: utf-8 -*-
"""
Created on Tue Jul 23 10:45:34 2019
@author: Mr.Ming
"""
import time
import uuid
from User import User
from Book import Book
from Order import Order
userList = []
bookList = []
orderList = []
def mian():
# 數(shù)據(jù)準備
userList.append(User(userName = "Louis", password = "123123"))
userList.append(User(userName = "Angelina", password = "123456"))
userList.append(User(userName = "Julian", password = "666666"))
bookList.append(Book(uuid.uuid3(uuid.NAMESPACE_DNS, 'C Primer Plus'), 'C Primer Plus', 'C語言學習經(jīng)典'))
bookList.append(Book(uuid.uuid3(uuid.NAMESPACE_DNS, 'Core Python Application Programming'),'Core Python Application Programming', 'Python學習經(jīng)典'))
bookList.append(Book(uuid.uuid3(uuid.NAMESPACE_DNS, '世界上下五千年'), '世界上下五千年', '世界歷史'))
bookList.append(Book(uuid.uuid3(uuid.NAMESPACE_DNS, '何以笙簫默'), '何以笙簫默', '顧漫小說'))
# 主界面
# 登錄 注冊
print('{:#^30}'.format('歡迎使用圖書館管理系統(tǒng)'))
print("請選擇功能:")
print("\t◇1.登錄")
print("\t◇2.注冊")
print("\t◇3.退出")
print(">" * 20 + "<" * 20)
loginChoice = input("~$")
if eval(loginChoice) == 1:
# 登錄
loginContinue = True
while loginContinue:
if login(): # 通過登錄函數(shù)的返回值判斷是否登錄成功
print("登錄成功")
loginContinue = False
else:
print("登錄失敗,請重新登錄")
pass
elif eval(loginChoice) == 2:
# 注冊
registe()
loginContinue = True
while loginContinue:
if login():
print("登錄成功")
loginContinue = False
else:
print("登錄失敗,請重新登錄")
pass
elif eval(loginChoice) == 3:
# 退出
return print("感謝您登錄,再見!")
else:
print("輸入錯誤")
# 登錄后的主界面
looding = True
while looding:
print("請選擇功能:")
print("\t◇1.查看圖書")
print("\t◇2.用戶借閱和借閱記錄")
print("\t◇3.圖書管理")
print("\t◇4.退出")
print(">" * 20 + "<" * 20)
functionChoice = input("~$")
if eval(functionChoice) == 1:
# 查看圖書
print(bookList)
elif eval(functionChoice) == 2:
# 用戶借書和查看借閱記錄
circleLooding = True
while circleLooding:
print("請輸入執(zhí)行的操作:")
print("\t◇1.借書")
print("\t◇2.借閱記錄")
print("\t◇3.返回主菜單")
print(">" * 20 + "<" * 20)
functionChoice = input("~$")
if eval(functionChoice) == 1:
borrowBook = input("請輸入你要借閱的書籍:")
for book in bookList:
if book.getBookName() == borrowBook: # 判斷需要借閱的書籍是否在書庫中
orderList.append(Order(time.ctime(), borrowBook))
print("借閱成功")
elif eval(functionChoice) == 2:
# 借閱列表
print(orderList)
elif eval(functionChoice) == 3:
circleLooding = False
else:
print("輸入錯誤")
elif eval(functionChoice) == 3:
# 圖書管理
print("請選擇:")
print("\t◇1.添加書籍")
print("\t◇2.刪除書籍")
print(">" * 20 + "<" * 20)
functionChoice = input("~$")
if eval(functionChoice) == 1:
# 添加書籍
addBook()
elif eval(functionChoice) == 2:
# 刪除數(shù)據(jù)
deleteBook()
else:
print("輸入錯誤")
elif eval(functionChoice) == 4:
# 退出
looding = False
return print("感謝您登錄,再見!")
else:
print("輸入錯誤")
pass
def login():
userName = input("請輸入用戶名:")
password = input("請輸入密碼:")
for user in userList:
# 當輸入的用戶名不為空且用戶名和密碼和用戶對象列表中的數(shù)據(jù)匹配時返回真(登錄成功)
if userName != "" and user.getUserName() == userName and user.getpassword() == password:
return True
return False
def registe():
userName = input("請輸入用戶名:")
password = input("請輸入密碼:")
# 防止重復用戶名
for user in userList:
if user.getUserName == userName:
print("用戶名已存在,注冊失敗,請換一個")
registe()
# 將新增的用戶對象添加到用戶對象列表
userList.append(User(userName = userName, password = password))
print("注冊成功")
pass
def addBook():
# 添加書籍
looding = True
while looding:
bookName = input("請輸入添加的書籍名:")
bookInfo = input("請輸入書籍的描述信息:")
bookList.append(Book(uuid.uuid3(uuid.NAMESPACE_DNS, bookName), bookName, bookInfo))
print("是否繼續(xù)添加:")
print("\t◇1.繼續(xù)")
print("\t◇2.返回主菜單")
print(">" * 20 + "<" * 20)
exitChoice = input("~$")
if eval(exitChoice) == 2:
looding = False
print("添加完畢")
pass
def deleteBook():
# 刪除書籍
looding = True
while looding:
bookName = input("請輸入要刪除的書籍名:")
for i in range(len(bookList)):
if bookList[i].getBookName() == bookName:
k = i # 獲取要刪除書籍的下標
bookList.pop(k)
print("是否繼續(xù)刪除:")
print("\t◇1.繼續(xù)")
print("\t◇2.返回主菜單")
print(">" * 20 + "<" * 20)
exitChoice = input("~$")
if eval(exitChoice) == 2:
looding = False
print("刪除完畢")
pass
mian()
運行演示:
image.png
image.png