阅读量:2
用python实现简易图书管理系统
目录
- 用python实现简易图书管理系统
- 前言
- 程序架构
- 前期准备
- txt文件
- 代码准备
- 代码实现
- 登陆系统
- 主程序
- 借书
- 还书
- 显示全部书籍
- 添加图书
- 全部代码
前言
很多图书馆都有自己的图书管理系统,它主要担负的功能有借书和还书,以及图书管理员对图书的操作等。
程序架构
用户登录/注册—>借书/还书/查看图书---->(管理员)添加、修改图书信息
前期准备
txt文件
首先,在任意位置创建一个文件夹,创建以下几个txt文件:
bookISDN,ISDN.txt:存储书籍的ISDN编号
can lend,lended.txt:存储书籍名称
number,usrinfo.txt:存储用户名,密码
point.txt:存储每个账号还能借出的图书本书
per.txt:存储权限文件
代码准备
因为这个程序会涉及到大量的文件读取,所以我们可以用def函数来简化过程:
def open_wf(name,write):#写入txt(覆盖) str(write) with open(name,"w",encoding="utf-8") as f: f.write(write) f.close() return True def open_wj(name,write):#写入txt(不覆盖) str(write) with open(name,"a",encoding="utf-8") as f: f.write(write) f.close() return True def open_r(name):#读取(仅第一行) with open (name,"r",encoding="utf-8") as f: data = f.readline() f.close() return data def open_r1(name):#全部读取 with open (name,"r",encoding="utf-8") as f: data = f.readlines() f.close() return data def now(): times=time.localtime() now=str(times.tm_hour)+":"+str(times.tm_min)+":"+str(time.tm_sec) return now def msgbox(flag,word): if flag=="1": print(word) elif flag=="2": print(word)
代码实现
登陆系统
代码:
def login(): usrall=open_r1("usrinfo.txt") number=open_r1("number.txt") #去掉\n for a in range(0,len(usrall)): usrall[a]=usrall[a].rstrip("\n") name=[] for b in range(0,len(number)): number[b]=number[b].rstrip("\n") name=[] seenum=0 print('''欢迎来到登录系统 提示:注册系统暂时关闭 *please choose:* **************** *****1.登录***** *****2.EMPTY**** ****************''') cho_0=input("*********choose:") go="0" if cho_0=="1": name_i=input("用户名:") number_i=input("密码:") if len(usrall)==len(number): for c in range(0,len(usrall)): if usrall[c]==name_i and number[c]==number_i: word=name_i+",登陆成功" msgbox("2",word) permission="none" open_wf("per.txt",permission) seenum=c main() elif "admin"==name_i and "admin123"==number_i: msgbox("2","管理员登陆成功") w=input("警告:管理员禁止操作借、还书系统!!!") time.sleep(1) permission="true" open_wf("per.txt",permission) print(permission) seenum=c main() elif c==len(usrall)-1: msgbox("1","用户名或密码错误") login() else: msgbox("1"," ): 程序遇到未知错误,请检查配置") elif cho_0=="2": print("注册系统已关闭") login() while (1==2): start="1" newname=input("请输入用户名:") newnum=input("请输入密码:") for d in range(0,len(usrall)): if newname==usrall[d]: msgobx("2","用户名已存在") break else: start="0" if start=="0": newname=newname+"\r" newnum=newnum+"\r" open_wj("usrinfo.txt",newname) open_wj("number.txt",newnum) word="5"+"\r" open_wj("point.txt",word) word="\r" open_wj("seeword.txt",word) msgbox("2","新用户创建成功,请重启系统登录") else: s=0
主程序
代码:
def main(): print('''欢迎来到图书管理系统 *******please choose:********* ****************************** **********0.显示全部********** ***1.借书********2.还书******* ***3.添加图书****4.删除图书*** ***5.修改图书****6.修改信息*** ***7.退出登录****8.退出******* ******************************''') s=input("choose:") if s=="1": lend_book() elif s=="0": view_all() elif s=="2": return_book() elif s=="3": add_book() elif s=="7": back_to_login() elif s=="8": exit()
借书
def lend_book(): canlendbook=open_r1("can lend.txt") bookISDN=open_r1("bookISDN.txt") points=open_r1("point.txt") for b in range(0,len(canlendbook)): canlendbook[b]=canlendbook[b].rstrip("\n") bookISDN[b]=bookISDN[b].rstrip("\n") for c in range(0,len(points)): points[c]=points[c].rstrip("\n") print('''请选择: **************** ***1.查找书名*** ***2.查找书号*** ****************''') s=input("choose:") if s=="1": bookname=input("请输入书名:") for b in range(0,len(canlendbook)): if canlendbook[b]==bookname: print("成功查找到书籍:",bookname) print("*********************************") print("请确认信息,无误请按Y") print() print("书籍名称:",canlendbook[b]) print("书籍ISDN号:",bookISDN[b]) s=input("请确认(Y/N)") if s=="Y": end_1=bookISDN[b]+"\r" end_2=canlendbook[b]+"\r" open_wj("ISDN.txt",end_1) open_wj("lended.txt",end_2) canlendbook.pop(b) bookISDN.pop(b) word_1=canlendbook[0]+"\r" word_2=bookISDN[0]+"\r" open_wf("can lend.txt",word_1) open_wf("bookISDN.txt",word_2) for b in range(0,len(bookISDN)-1): word1=canlendbook[b+1]+"\r" word2=bookISDN[b+1]+"\r" open_wj("can lend.txt",word1) open_wj("bookISDN.txt",word2) if int(points[seenum])>0: points[seenum]=str(int(points[seenum])-1) word=points[0]+"\r" open_wf("point.txt",word) for t in range(0,len(points)): word=points[t-1]+"\r" open_wj("point.txt",word) print("处理成功,您可再借出",points[seenum],"本书") main() break else: print("您借的书已达到5本,请先还书") main() break else: main() elif b==len(canlendbook)-1: word="找不到书籍'"+bookname+"',请检查拼写或此书已借出" msgbox("1",word) main() break elif s=="2": '''namenum=0''' findISDN=input("请输入ISDN编号:") for b in range(0,len(bookISDN)): if bookISDN[b]==findISDN: namenum=b print(b) print("成功查找到书籍:",canlendbook[b]) bookname=canlendbook[b] print("*********************************") print("请确认信息,无误请按Y") print() print("书籍名称:",canlendbook[b]) print("书籍ISDN号:",bookISDN[b]) s=input("请确认(Y/N)") if s=="Y": end_1=bookISDN[b]+"\r" end_2=canlendbook[b]+"\r" open_wj("ISDN.txt",end_1) open_wj("lended.txt",end_2) canlendbook.pop(b) bookISDN.pop(b) word_1=canlendbook[0]+"\r" word_2=bookISDN[0]+"\r" open_wf("can lend.txt",word_1) open_wf("bookISDN.txt",word_2) for b in range(0,len(bookISDN)-1): word_1=canlendbook[b+1]+"\r" word_2=bookISDN[b+1]+"\r" open_wj("can lend.txt",word_1) open_wj("bookISDN.txt",word_2) '''seewords=open_r1("seeword.txt") for i in range(0,len(seewords)): seewords[i]=seewords[i].rstrip("\n") seewords[seenum+1]=str(seewords[seenum])+","+str(namenum) word=seewords[0]+"\n" open_wf("seeword.txt",word) for i in range(0,len(seewords)-1): word=seewords[i+1]+"\n" open_wj("seeword.txt",word)''' if int(points[seenum])>0: points[seenum]=str(int(points[seenum])-1) word=points[0]+"\r" open_wf("point.txt",word) for t in range(0,len(points)): word=points[t-1]+"\r" open_wj("point.txt",word) print("处理成功,您可再借出",points[seenum],"本书") main() else: print("您借的书已达到5本,请先还书") main() else: main() else: if b==len(bookISDN)-1: word="找不到书籍'"+bookname+"',请检查拼写或此书已借出" msgbox("1",word) main()
还书
def return_book(): isdn=open_r1("ISDN.txt") namebook=open_r1("lended.txt") for i in range(0,len(isdn)): isdn[i]=isdn[i].rstrip("\n") namebook[i]=namebook[i].rstrip("\n") print('''请选择还书方式: **************** ***1.查找书名*** ***2.查找书号*** ****************''') s=input("choose:") if s=="1": s=input("书籍名称:") for x in range(0,len(namebook)): if s==namebook[x]: print("成功找到书籍",namebook[x]) print("*********************************") print("请确认信息,无误请按Y") print() print("书籍名称:",namebook[x]) print("书籍ISDN号:",isdn[x]) s=input("请确认(Y/N)") if s=="Y": word_1=namebook[x]+"\r" word_2=isdn[x]+"\r" open_wj("can lend.txt",word_1) open_wj("bookISDN.txt",word_2) isdn.pop(x) namebook.pop(x) word1=isdn[0]+"\r" word2=namebook[0]+"\r" open_wf("lended.txt",word2) open_wf("ISDN.txt",word1) for i in range(0,len(isdn)-1): word_1=isdn[i+1]+"\r" word_2=namebook[i+1]+"\r" open_wj("ISDN.txt",word_1) open_wj("lended.txt",word_2) points=open_r1("point.txt") for i in range(0,len(points)): points[i]=points[i].rstrip("\n") points[seenum]=str(int(points[seenum])+1) word=points[0]+"\r" open_wf("point.txt",word) for t in range(0,len(points)): word=points[t-1]+"\r" open_wj("point.txt",word) main() elif s=="N": main() else: if x==len(namebook)-1: print("没有找到图书'",s,"',请检查拼写是否正确") main() elif s=="2": s=input("请输入ISDN号:") for x in range(0,len(isdn)): if s==isdn[x]: print("成功找到书籍",namebook[x]) print("*********************************") print("请确认信息,无误请按Y") print() print("书籍名称:",namebook[x]) print("书籍ISDN号:",isdn[x]) s=input("请确认(Y/N)") if s=="Y": word_1=namebook[x]+"\r" word_2=isdn[x]+"\r" open_wj("can lend.txt",word_1) open_wj("bookISDN.txt",word_2) isdn.pop(x) namebook.pop(x) word1=isdn[0]+"\r" word2=namebook[0]+"\r" open_wf("lended.txt",word2) open_wf("ISDN.txt",word1) for i in range(0,len(isdn)-1): word_1=isdn[i+1]+"\r" word_2=namebook[i+1]+"\r" open_wj("ISDN.txt",word_1) open_wj("lended.txt",word_2) points=open_r1("point.txt") for i in range(0,len(points)): points[i]=points[i].rstrip("\n") points[seenum]=str(int(points[seenum])+1) word=points[0]+"\r" open_wf("point.txt",word) for t in range(0,len(points)): word=points[t-1]+"\r" open_wj("point.txt",word) main() elif s=="N": main() else: if x==len(namebook)-1: print("没有找到图书'",s,"',请检查拼写是否正确") main()
显示全部书籍
def view_all(): inbook=open_r1("can lend.txt") inISDN=open_r1("bookISDN.txt") borbook=open_r1("lended.txt") borISDN=open_r1("ISDN.txt") for b in range(0,len(inbook)): inbook[b]=inbook[b].rstrip("\n") inISDN[b]=inISDN[b].rstrip("\n") for c in range(0,len(borbook)): borbook[c]=borbook[c].rstrip("\n") borISDN[c]=borISDN[c].rstrip("\n") print("本馆内现有的图书(数量:",len(inbook),"本)") print("*****************************************") print("书名 ISDN(分区,书号)") for i in range(0,len(inbook)): print(inbook[i]," ",inISDN[i]) print("本馆已借出的图书(数量:",len(borbook),"本)") print("*****************************************") print("书名 ISDN(分区,书号)") for i in range(0,len(borbook)): print(borbook[i]," ",borISDN[i]) print("*****************************************") main()
添加图书
def add_book(): permission=open_r("per.txt") if permission=="true": print("欢迎来到图书添加系统") a_name=input("请输入书籍名称:") a_ISDN=input("请输入ISDN编号:") word_1=a_name+"\r" word_2=a_ISDN+"\r" open_wj("can lend.txt",word_1) open_wj("bookISDN.txt",word_2) print('''请选择: *************** **1.继续添加*** **2.退出添加*** ***************''') s=input("choose:") if s=="1": add_book() elif s=="2": main() else: print("您不是管理员,没有修改权限") main()
全部代码
import tkinter import tkinter.messagebox import random import time #used #a,b,c,d,e,f,g,h,i,j,k #a, #参数设置 c=[] seenum=0 permission=0 # def open_wf(name,write): str(write) with open(name,"w",encoding="utf-8") as f: f.write(write) f.close() return True def open_wj(name,write): str(write) with open(name,"a",encoding="utf-8") as f: f.write(write) f.close() return True def open_r(name): with open (name,"r",encoding="utf-8") as f: data = f.readline() f.close() return data def open_r1(name): with open (name,"r",encoding="utf-8") as f: data = f.readlines() f.close() return data def now(): times=time.localtime() now=str(times.tm_hour)+":"+str(times.tm_min)+":"+str(times.tm_sec) return now def msgbox(flag,word): if flag=="1": print(word) elif flag=="2": print(word) def login(): usrall=open_r1("usrinfo.txt") number=open_r1("number.txt") #去掉\n for a in range(0,len(usrall)): usrall[a]=usrall[a].rstrip("\n") name=[] for b in range(0,len(number)): number[b]=number[b].rstrip("\n") name=[] seenum=0 print('''欢迎来到登录系统 提示:注册系统暂时关闭 *please choose:* **************** *****1.登录***** *****2.EMPTY**** ****************''') cho_0=input("*********choose:") go="0" if cho_0=="1": name_i=input("用户名:") number_i=input("密码:") if len(usrall)==len(number): for c in range(0,len(usrall)): if usrall[c]==name_i and number[c]==number_i: word=name_i+",登陆成功" msgbox("2",word) permission="none" open_wf("per.txt",permission) seenum=c main() elif "admin"==name_i and "admin123"==number_i: msgbox("2","管理员登陆成功") w=input("警告:管理员禁止操作借、还书系统!!!") time.sleep(1) permission="true" open_wf("per.txt",permission) print(permission) seenum=c main() elif c==len(usrall)-1: msgbox("1","用户名或密码错误") login() else: msgbox("1"," ): 程序遇到未知错误,请检查配置") elif cho_0=="2": print("注册系统已关闭") login() while (1==2): start="1" newname=input("请输入用户名:") newnum=input("请输入密码:") for d in range(0,len(usrall)): if newname==usrall[d]: msgobx("2","用户名已存在") break else: start="0" if start=="0": newname=newname+"\r" newnum=newnum+"\r" open_wj("usrinfo.txt",newname) open_wj("number.txt",newnum) word="5"+"\r" open_wj("point.txt",word) word="\r" open_wj("seeword.txt",word) msgbox("2","新用户创建成功,请重启系统登录") else: s=0 def main(): print('''欢迎来到图书管理系统 *******please choose:********* ****************************** **********0.显示全部********** ***1.借书********2.还书******* ***3.添加图书****4.删除图书*** ***5.修改图书****6.修改信息*** ***7.退出登录****8.退出******* ******************************''') s=input("choose:") if s=="1": lend_book() elif s=="0": view_all() elif s=="2": return_book() elif s=="3": add_book() elif s=="7": back_to_login() elif s=="8": exit() #借书 def lend_book(): canlendbook=open_r1("can lend.txt") bookISDN=open_r1("bookISDN.txt") points=open_r1("point.txt") for b in range(0,len(canlendbook)): canlendbook[b]=canlendbook[b].rstrip("\n") bookISDN[b]=bookISDN[b].rstrip("\n") for c in range(0,len(points)): points[c]=points[c].rstrip("\n") print('''请选择: **************** ***1.查找书名*** ***2.查找书号*** ****************''') s=input("choose:") if s=="1": bookname=input("请输入书名:") for b in range(0,len(canlendbook)): if canlendbook[b]==bookname: print("成功查找到书籍:",bookname) print("*********************************") print("请确认信息,无误请按Y") print() print("书籍名称:",canlendbook[b]) print("书籍ISDN号:",bookISDN[b]) s=input("请确认(Y/N)") if s=="Y": end_1=bookISDN[b]+"\r" end_2=canlendbook[b]+"\r" open_wj("ISDN.txt",end_1) open_wj("lended.txt",end_2) canlendbook.pop(b) bookISDN.pop(b) word_1=canlendbook[0]+"\r" word_2=bookISDN[0]+"\r" open_wf("can lend.txt",word_1) open_wf("bookISDN.txt",word_2) for b in range(0,len(bookISDN)-1): word1=canlendbook[b+1]+"\r" word2=bookISDN[b+1]+"\r" open_wj("can lend.txt",word1) open_wj("bookISDN.txt",word2) if int(points[seenum])>0: points[seenum]=str(int(points[seenum])-1) word=points[0]+"\r" open_wf("point.txt",word) for t in range(0,len(points)): word=points[t-1]+"\r" open_wj("point.txt",word) print("处理成功,您可再借出",points[seenum],"本书") main() break else: print("您借的书已达到5本,请先还书") main() break else: main() elif b==len(canlendbook)-1: word="找不到书籍'"+bookname+"',请检查拼写或此书已借出" msgbox("1",word) main() break elif s=="2": '''namenum=0''' findISDN=input("请输入ISDN编号:") for b in range(0,len(bookISDN)): if bookISDN[b]==findISDN: namenum=b print(b) print("成功查找到书籍:",canlendbook[b]) bookname=canlendbook[b] print("*********************************") print("请确认信息,无误请按Y") print() print("书籍名称:",canlendbook[b]) print("书籍ISDN号:",bookISDN[b]) s=input("请确认(Y/N)") if s=="Y": end_1=bookISDN[b]+"\r" end_2=canlendbook[b]+"\r" open_wj("ISDN.txt",end_1) open_wj("lended.txt",end_2) canlendbook.pop(b) bookISDN.pop(b) word_1=canlendbook[0]+"\r" word_2=bookISDN[0]+"\r" open_wf("can lend.txt",word_1) open_wf("bookISDN.txt",word_2) for b in range(0,len(bookISDN)-1): word_1=canlendbook[b+1]+"\r" word_2=bookISDN[b+1]+"\r" open_wj("can lend.txt",word_1) open_wj("bookISDN.txt",word_2) '''seewords=open_r1("seeword.txt") for i in range(0,len(seewords)): seewords[i]=seewords[i].rstrip("\n") seewords[seenum+1]=str(seewords[seenum])+","+str(namenum) word=seewords[0]+"\n" open_wf("seeword.txt",word) for i in range(0,len(seewords)-1): word=seewords[i+1]+"\n" open_wj("seeword.txt",word)''' if int(points[seenum])>0: points[seenum]=str(int(points[seenum])-1) word=points[0]+"\r" open_wf("point.txt",word) for t in range(0,len(points)): word=points[t-1]+"\r" open_wj("point.txt",word) print("处理成功,您可再借出",points[seenum],"本书") main() else: print("您借的书已达到5本,请先还书") main() else: main() else: if b==len(bookISDN)-1: word="找不到书籍'"+bookname+"',请检查拼写或此书已借出" msgbox("1",word) main() def view_all(): inbook=open_r1("can lend.txt") inISDN=open_r1("bookISDN.txt") borbook=open_r1("lended.txt") borISDN=open_r1("ISDN.txt") for b in range(0,len(inbook)): inbook[b]=inbook[b].rstrip("\n") inISDN[b]=inISDN[b].rstrip("\n") for c in range(0,len(borbook)): borbook[c]=borbook[c].rstrip("\n") borISDN[c]=borISDN[c].rstrip("\n") print("本馆内现有的图书(数量:",len(inbook),"本)") print("*****************************************") print("书名 ISDN(分区,书号)") for i in range(0,len(inbook)): print(inbook[i]," ",inISDN[i]) print("本馆已借出的图书(数量:",len(borbook),"本)") print("*****************************************") print("书名 ISDN(分区,书号)") for i in range(0,len(borbook)): print(borbook[i]," ",borISDN[i]) print("*****************************************") main() def return_book(): isdn=open_r1("ISDN.txt") namebook=open_r1("lended.txt") for i in range(0,len(isdn)): isdn[i]=isdn[i].rstrip("\n") namebook[i]=namebook[i].rstrip("\n") print('''请选择还书方式: **************** ***1.查找书名*** ***2.查找书号*** ****************''') s=input("choose:") if s=="1": s=input("书籍名称:") for x in range(0,len(namebook)): if s==namebook[x]: print("成功找到书籍",namebook[x]) print("*********************************") print("请确认信息,无误请按Y") print() print("书籍名称:",namebook[x]) print("书籍ISDN号:",isdn[x]) s=input("请确认(Y/N)") if s=="Y": word_1=namebook[x]+"\r" word_2=isdn[x]+"\r" open_wj("can lend.txt",word_1) open_wj("bookISDN.txt",word_2) isdn.pop(x) namebook.pop(x) word1=isdn[0]+"\r" word2=namebook[0]+"\r" open_wf("lended.txt",word2) open_wf("ISDN.txt",word1) for i in range(0,len(isdn)-1): word_1=isdn[i+1]+"\r" word_2=namebook[i+1]+"\r" open_wj("ISDN.txt",word_1) open_wj("lended.txt",word_2) points=open_r1("point.txt") for i in range(0,len(points)): points[i]=points[i].rstrip("\n") points[seenum]=str(int(points[seenum])+1) word=points[0]+"\r" open_wf("point.txt",word) for t in range(0,len(points)): word=points[t-1]+"\r" open_wj("point.txt",word) main() elif s=="N": main() else: if x==len(namebook)-1: print("没有找到图书'",s,"',请检查拼写是否正确") main() elif s=="2": s=input("请输入ISDN号:") for x in range(0,len(isdn)): if s==isdn[x]: print("成功找到书籍",namebook[x]) print("*********************************") print("请确认信息,无误请按Y") print() print("书籍名称:",namebook[x]) print("书籍ISDN号:",isdn[x]) s=input("请确认(Y/N)") if s=="Y": word_1=namebook[x]+"\r" word_2=isdn[x]+"\r" open_wj("can lend.txt",word_1) open_wj("bookISDN.txt",word_2) isdn.pop(x) namebook.pop(x) word1=isdn[0]+"\r" word2=namebook[0]+"\r" open_wf("lended.txt",word2) open_wf("ISDN.txt",word1) for i in range(0,len(isdn)-1): word_1=isdn[i+1]+"\r" word_2=namebook[i+1]+"\r" open_wj("ISDN.txt",word_1) open_wj("lended.txt",word_2) points=open_r1("point.txt") for i in range(0,len(points)): points[i]=points[i].rstrip("\n") points[seenum]=str(int(points[seenum])+1) word=points[0]+"\r" open_wf("point.txt",word) for t in range(0,len(points)): word=points[t-1]+"\r" open_wj("point.txt",word) main() elif s=="N": main() else: if x==len(namebook)-1: print("没有找到图书'",s,"',请检查拼写是否正确") main() def add_book(): permission=open_r("per.txt") if permission=="true": print("欢迎来到图书添加系统") a_name=input("请输入书籍名称:") a_ISDN=input("请输入ISDN编号:") word_1=a_name+"\r" word_2=a_ISDN+"\r" open_wj("can lend.txt",word_1) open_wj("bookISDN.txt",word_2) print('''请选择: *************** **1.继续添加*** **2.退出添加*** ***************''') s=input("choose:") if s=="1": add_book() elif s=="2": main() else: print("您不是管理员,没有修改权限") main() def back_to_login(): ch=input("确认退出登录?(Y/N)") if ch=="Y": print("已成功退出登录") login() elif ch++"N": main() login()
零基础Python学习资源介绍
👉Python学习路线汇总👈
Python所有方向的技术点做的整理,形成各个领域的知识点汇总,它的用处就在于,你可以按照上面的知识点去找对应的学习资源,保证自己学得较为全面。(学习教程文末领取哈)
👉Python必备开发工具👈
温馨提示:篇幅有限,已打包文件夹,获取方式在:文末
👉Python学习视频600合集👈
观看零基础学习视频,看视频学习是最快捷也是最有效果的方式,跟着视频中老师的思路,从基础到深入,还是很容易入门的。
👉实战案例👈
光学理论是没用的,要学会跟着一起敲,要动手实操,才能将自己的所学运用到实际当中去,这时候可以搞点实战案例来学习。
👉100道Python练习题👈
检查学习结果。
👉面试刷题👈
资料领取
上述这份完整版的Python全套学习资料已经上传CSDN官方,朋友们如果需要可以微信扫描下方CSDN官方认证二维码输入“领取资料” 即可领取。