圖書管理系統圖書管理系統
某圖書館所藏圖書如表1所示:
書號 |
書名 |
出版社 |
作者 |
價格 |
庫存 |
10001 |
C語言程序設計 |
清華大學出版社 |
張三 |
51 |
5 |
10002 |
Python程序設計基礎 |
高等教育出版社 |
李四 |
45 |
6 |
… |
… |
… |
… |
… |
… |
借閱信息如表2所示:
學號 |
書號 |
借閱日期 |
1001 |
10001 |
20190309 |
1002 |
10002 |
20190401 |
… |
… |
… |
學生信息如表3所示
學號 |
姓名 |
性別 |
班級 |
1001 |
張三 |
男 |
化學1801 |
1002 |
李四 |
女 |
化學1802 |
主要實現以下功能:
(1)能分別輸入表1和表3的信息,並將其分別保存到兩個文件
(2)從分別從文件中讀出圖書信息和學生信息
(3)實現借閱功能:輸入學號和書號,如果借閱成功(學號所對應的學生在表3中並且書號所對應的圖書在表1中且庫存大於等於1),修改表1和表2,並保存到文件
(4)實現還書功能:從表2中刪除該學生的借閱信息,並修改表1的庫存信息,並保存到文件
(5)輸入某學生姓名,可以查詢該生的借閱圖書信息
(6)輸入某書號,可以查詢借閱該書的學生信息
(7)統計某出版社的藏書量,統計某學生當前借書量
(8)利用第三方庫matplotlib中的pyplot繪製統計圖,如繪製各出版社的藏書量折線圖
import time
import pandas as pd
import os
import matplotlib.pyplot as plt
# 先定義一個配置類
from Tools.scripts.treesync import raw_input
class Config:
# 定義表一
table1_url = "C:\\Users\\Shinelon\\Desktop\\untitled\\resource\\table1.csv"
table1_header = ["書號", "書名", "出版社", "作者", "價格", "庫存"]
# 定義表二
table2_url = "C:\\Users\\Shinelon\\Desktop\\untitled\\resource\\table2.csv"
table2_header = ["學號", "書號", "借閱日期"]
# 定義表三
table3_url = "C:\\Users\\Shinelon\\Desktop\\untitled\\resource\\table3.csv"
table3_header = ["學號", "姓名", "性別", "班級"]
class library_system(object):
def __init__(self):
print("圖書館系統初始化中...")
self.input_table1_table2()
self.book_table, self.borrow_table, self.student_table = self.get_info_from_file()
print("圖書館系統初始化完成!")
self.menu()
def input_table1_table2(self):
print("請輸入圖書館藏書表")
table1 = []
print("輸入藏書表:書號 書名 出版社 作者 價格 庫存\n")
x = raw_input('請輸入數據:')
while x != "exit\n":
y = x.split()
if len(y) != 6:
x = raw_input('列數不一致\n請重新輸入:')
else:
table1.append(y)
x = raw_input('下一條記錄:')
print("請輸入學生信息表")
table3 = []
print("輸入學生表:學號 姓名 性別 班級\n")
x = raw_input('請輸入數據:')
while x != "exit\n":
y = x.split()
if len(y) != 4:
x = raw_input('數據列數不一致\n請重新輸入:')
else:
table3.append(y)
x = raw_input('下一條記錄:')
table1 = pd.DataFrame(table1)
table3 = pd.DataFrame(table3)
table1.to_csv(Config.table1_url, header=Config.table1_header, index=None)
table3.to_csv(Config.table3_url, header=Config.table3_header, index=None)
def get_info_from_file(self):
book_table = pd.read_csv(Config.table1_url)
borrow_table = []
student_table = pd.read_csv(Config.table3_url)
print("信息讀取成功...")
return book_table, borrow_table, student_table
def save_file(self):
self.book_table.to_csv(Config.table1_url, header=Config.table1_header, index=None)
boo = pd.DataFrame(self.borrow_table)
boo.to_csv(Config.table2_url, header=Config.table2_header, index=None)
self.student_table.to_csv(Config.table3_url, header=Config.table3_header, index=None)
print("保存文件成功")
# 實現借閱功能:輸入學號和書號,如果借閱成功(學號所對應的學生在表3中並且書號所對應的圖書在表1中且庫存大於等於1),修改表1和表2,並保存到文件
def borrow(self, student_id, book_id):
flag1=True
for i in range(len(self.student_table["學號"])):
if int(self.student_table["學號"][i])==int(student_id):
print("查詢到該學生")
flag1=False
for j in range(len(self.book_table["書號"])):
if int(self.book_table["書號"][j])==int(book_id) and int(self.book_table["庫存"][j])>0:
print("庫存充足")
self.book_table["庫存"][j] = str(int(self.book_table["庫存"][j])-1)
borrow = [student_id,book_id, time.strftime("%d/%m/%Y")]
self.borrow_table .append(borrow)
self.save_file()
break
if not flag1:
break
if flag1:
print("未查詢到該學生")
print("借閱圖書")
# 實現還書功能:從表2中刪除該學生的借閱信息,並修改表1的庫存信息,並保存到文件
def let_back(self, student_id, book_id):
for j in range(len(self.book_table["書號"])):
if int(self.book_table["書號"][j])==int(book_id):
self.book_table["庫存"][j] = str(int(self.book_table["庫存"][j])+1)
for i in range(len(self.borrow_table)):
if int(self.borrow_table[i][0])==int(student_id) and int(self.borrow_table[i][1])==int(book_id):
del self.borrow_table[i]
self.save_file()
break
break
print("還書")
# 輸入某書號,可以查詢借閱該書的學生信息
def find_by_book_id(self, book_id):
stu_ids=set()
#先獲取對應人的學號
for i in range(len(self.borrow_table)):
if int(self.borrow_table[i][1])==int(book_id):
stu_ids.add(int(self.borrow_table[i][0]))
stu=[]
for i in range(len(self.student_table["學號"])):
if int(self.student_table["學號"][i]) in stu_ids:
stu.append(self.student_table.iloc[i])
print("查詢借了某本書的學生信息")
print(stu)
def sum_by_student_id(self, student_id):
sum=0
for i in range(len(self.borrow_table)):
if int(self.borrow_table[i][0])==int(student_id):
sum+=1
print("統計某學生當前借書量"+str(sum))
# 統計某出版社的藏書量,統計某學生當前借書量
def sum_by_publish(self, publish_name):
book_id = set()
sum=0
for i in range(len(self.book_table["出版社"])):
if str(self.book_table["出版社"][i])==str(publish_name):
book_id.add(int(self.book_table["出版社"][0]))
sum+=int(self.book_table["庫存"][i])
for i in range(len(self.borrow_table)):
if self.borrow_table[i][1] in book_id:
sum+=1
print(str(publish_name)+"統計藏書量"+str(sum))
return sum
# 輸入某學生姓名,可以查詢該生的借閱圖書信息
def find_by_student_name(self, student_name):
stu_id=set()
for i in range(len(self.student_table["姓名"])):
if str(self.student_table["姓名"][i])==str(student_name):
stu_id.add(int(self.student_table["學號"][i]))
print("找到")
book_id=set()
res=[]
for i in range(len(self.borrow_table)):
if int(self.borrow_table[i][0]) in stu_id:
book_id.add(int(self.borrow_table[i][1]))
for i in range(len(self.book_table["書號"])):
if int(self.book_table["書號"][i]) in book_id:
res.append(self.book_table.iloc[i])
print("查詢某學生的借書信息")
print(res)
#獲取各出版社的藏書量折線圖
def get_publish(self):
pub = set()
for i in range(len(self.book_table["出版社"])):
pub.add(str(self.book_table["出版社"][i]))
pubL = list(pub)
x1 = range(len(pubL))
numL = []
for i in range(len(pubL)):
numL.append(self.sum_by_publish(pubL[i]))
plt.title('各出版社的藏書量折線圖')
plt.xlabel('出版社名字')
plt.ylabel('藏書量')
plt.plot(pubL, numL, 'r', label='藏書量')
plt.xticks(x1, pubL, rotation=0)
plt.legend()
plt.grid()
plt.show()
# 利用第三方庫matplotlib中的pyplot繪製統計圖,如繪製各出版社的藏書量折線圖,繪製各學生借書量的餅圖等
def get_stu(self):
stu_id = set()
for i in range(len(self.student_table["學號"])):
stu_id.add(int(self.student_table["學號"][i]))
stuL = list(stu_id)
numL = []
for i in range(len(stuL)):
numL.append(self.sum_by_student_id(stuL[i]))
plt.rcParams['font.sans-serif'] = 'SimHei' # 設置中文顯示
plt.figure(figsize=(6, 6)) # 將畫布設定爲正方形,則繪製的餅圖是正圓
plt.pie(numL, labels=stuL, autopct='%1.1f%%') # 繪製餅圖
plt.title('2018年餅圖') # 繪製標題
plt.show()
# 1繪製各出版社的藏書量折線圖,2繪製各學生借書量的餅圖
def show_diaglo(self,chos):
if str(chos)=="1":
self.get_publish()
if str(chos)=='2':
self.get_stu()
print("展現圖表")
def cls(self):
os.system("cls")
def menu(self):
level_1_choose = raw_input("按回車繼續")
while level_1_choose != "exit\n":
self.cls()
print("圖書管理系統菜單\n")
print("1 借閱功能 請輸入: 1 學號 書號")
print("2 還書功能 請輸入: 2 學號 書號")
print("3 查詢學生借閱信息 請輸入: 3 學生姓名")
print("4 查詢借該書的學生信息 請輸入: 4 書號")
print("5 統計某出版社的藏書量 請輸入: 5 出版社名")
print("6 統計某學生當前的借書量 請輸入: 6 學生學號")
print("7 繪圖功能 請輸入:7")
level_1_choose = raw_input("請選擇1-7你要選擇的功能,輸入exit退出")
turn = True
while turn:
sp = level_1_choose.split()
# 1-7
if sp[0] == "1":
if len(sp) == 3:
self.borrow(sp[1], sp[2])
turn = False
else:
turn = True
level_1_choose = raw_input("輸入錯誤,請重新輸入\n")
elif sp[0] == "2":
if len(sp) == 3:
self.let_back(sp[1], sp[2])
turn = False
else:
turn = True
level_1_choose = raw_input("輸入錯誤,請重新輸入\n")
elif sp[0] == "3":
if len(sp) == 2:
self.find_by_student_name(sp[1])
turn = False
else:
turn = True
level_1_choose = raw_input("輸入錯誤,請重新輸入\n")
elif sp[0] == "4":
if len(sp) == 2:
self.find_by_book_id(sp[1])
turn = False
else:
turn = True
level_1_choose = raw_input("輸入錯誤,請重新輸入\n")
elif sp[0] == "5":
if len(sp) == 2:
self.sum_by_publish(sp[1])
turn = False
else:
turn = True
level_1_choose = raw_input("輸入錯誤,請重新輸入\n")
elif sp[0] == "6":
if len(sp) == 2:
self.sum_by_student_id(sp[1])
turn = False
else:
turn = True
level_1_choose = raw_input("輸入錯誤,請重新輸入\n")
elif sp[0] == "7":
if len(sp) == 2:
self.show_diaglo(sp[1])
turn = False
else:
turn = True
level_1_choose = raw_input("輸入錯誤,請重新輸入\n")
else:
turn = True
level_1_choose = raw_input("輸入錯誤,請重新輸入\n")
if __name__ == '__main__':
ls = library_system()