python模拟实现图书管理系统
本文实例为大家分享了python实现图书管理系统的具体代码,供大家参考,具体内容如下
软件python
事先用代码写一个存储数据的数据库,也就是,.txt文件
这是我模拟的一个数据d1='[{"用户名": "admin", "密码": "123", "姓名": "张三"},{"用户名": "aaa", "密码": "123", "姓名": "李四"}]' with open(r"name_list.txt","w") as f: f.write(d1) d2='[{"编号":1001, "书名": "红楼梦", "作者": "曹雪芹", "借出状态": "可借"},\ {"编号":1002, "书名": "java教程","作者": "齐一天", "借出状态": "可借"},\ {"编号":1003, "书名": "圣经","作者": "耶稣", "借出状态": "已借出"},\ {"编号":1004, "书名": "李白诗集","作者": "李白", "借出状态": "可借"}\ ]' with open(r"books.txt","w") as f: f.write(d2)
数据准备好开始:
一步是读取数据库中的数据,将其转换为字符串,因为接下来的数据都是字符串的处理
def readUser(): with open(r"name_list.txt","r") as f: jsondata=f.read() userlist=json.loads(jsondata) return userlist
这一步是将外部数据写入数据库
def writeData(listb): jsondata=json.dumps(listb,ensure_ascii=False) with open(r"name_list.txt","ucgAPrSUzQw") as f: f.write(jsondata) print("----------------数据写入成功!")
1.用户登录:
def login(): userlist=readUser() uname=input("请输入您的用户名:")www.cppcns.com upwd=input("请输入您的密码:") msg="失败" for user in userlist: if uname==user["用户名"] and upwd==user["密码"]: print("恭喜",user["姓名"],"登录成功") msg="成功" if msg=="失败": print("登录失败,请从新登录!") return msg
2.显示图书列表:
def Showbooklist(): Booklist=readBook() print("**************************图书目录**************************") for data in Booklist: print(data["编号"],"\t",data["书名"],"\t\t\t",data["作者"],"\t\t\t",data[编程客栈"借出状态"]) print("---------------------------------------------------")
3.添加图书
def addBook(): Booklist=readBook() numlist=[] for book in Booklist: numlist.append(book["编号"]) newnum=max(numlist)+1 Bname=input("请输入书名:") writer=input("请输入图书作者:") condi="可借" newBooklist={"编号": newnum, "书名": Bname, "作者": writer,"借出状态":condi} Booklist.append(newBooklist) writeBook(Booklist)
4.删除图书
def delbook(): BoucgAPrSUzQoklist=readBook() Bnum=int(input("请输入要删除的图书编号:")) Bname=input("请输入要删除的图书名称:") for book in Booklist: if Bnum==book["编号"] or Bname==book["书名"]: Booklist.remove(book) print("图书已",book["书名"],"经删除") writeBook(Booklist)
5.借阅图书
def lendbook(): Booklist=readBook() Bnum=int(input("请输入要借阅的图书编号:")) Bname=input("请输入要借阅的图书名称:") for book in Booklist: if Bnum==book["编号"] or Bname==book["书名"]: if book["借出状态"]=="可借": print("此书在架,可借阅") a=int(input("借阅请安1,退出请按0:")) if a==1: book["借出状态"]="已借出" else: break else: print("此书已被借出,等归还后再借") writeBook(Booklist)
6.归还图书
def returnbook(): Booklist=readBook() Bnum=int(input("请输入要还的图书编号:")) Bname=input("请输入要还的图书名称:") for book in Booklist: if Bnum==book["编号"] or Bname==book["书名"]: if book["借出状态"]=="已借出": book["借出状态"]="可借" print(book["书名"],"已还") else: print(book["书名"],"书已在架,不用归还") writeBook(Booklist)
最后是调用所有模块的总程序:
while 1==1: print("编程客栈*************************图书管理系统2.0******************************") msg=login() if msg=="成功": print("1.显示所有的图书;\n2.图书上架;\n3.图书下架;\n4.借阅;\n5.归还;\n6.退出") print("---------------------------------------------------------------------") while 2==2: choice=int(input("请输入业务编号(1-6):")) if choice==1: Showbooklist() elif choice==2: addBook() elif choice==3: delbook() elif choice==4: lendbook() elif choice==5: returnbook() elif choice==6: break else: print("没有此业务,请重新选择") continue
这位是执行后的效果图。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
精彩评论