简介
这是一个简易的图书管理系统,是我初学python的一个练习,后续没有继续完善功能和加以改进,只是给python初学者的一点参考。
介绍
这个简单的图书管理系统满足了在文件下的增删改查和借阅归还的功能,没有分为几种管理员,使用者等几种角色。
代码块
首先,我是使用列表进行存储图书信息,当然也可以用字典或者其他容器进行存储。
#图书管理系统
book_list=""
其次,对图书信息创建一个txt文档,并记录txt文档所在地址。
增:
增加图书信息就比较简单,只用添加书本id,名字,地址,状态。
注意:我这里的状态False使表示未借出,True表示借出,即我默认一本书在图书馆只有一本。这是比较局限的,当然大家可以把状态改成图书数量,增加就+1就好了。
#增
def book_add():
print("Book add")
book_id=input("Enter the book id : ")
book_name=input("Enter the book name : ")
book_adress=input("Enter the book adress : ")
book_status=False
add_content=f"{book_id},{book_name},{book_adress},{book_status}"
print(book_list)
with open("D:\\daima\\data.txt", "a", encoding="utf-8") as fa:
fa.write(f"\n{add_content}")
print(f"已添加图书信息的为:id:{book_id},name:{book_name},adress:{book_adress},status:{book_status}")
删:
删除图书信息只需注意要对每个信息以列表的方式读取,且对信息进行分割便于我们验证要删除信息的id是否与当前验证的id相等。
注意:使用book_id作为books的下标索引修改内容时,要把前者转换为int类型
#删
def book_delete():
print("Book delete")
book_id=input("Enter the book id : ")
with open("D:\\daima\\data.txt", "r", encoding="utf-8") as fr:
books=fr.readlines() #readlines可以把读取到的每一行信息放进列表
for line in books:
line=line.strip().split(",") #对列表的每个信息进行分割,方便验证图书id
if line[0]==book_id: #验证
del_content=f"{book_id},该图书已删除\n"
books[int(book_id)]=del_content
print("删除成功")
else:
continue;
with open("D:\\daima\\data.txt", "w", encoding="utf-8") as fw:
fw.writelines(books)
改:
修改图书信息与删除图书信息类似,找到对应的图书信息后对每个内容进行修改即可
#改
def book_motify():
print("Book motify")
book_id=input("Enter the book id : ")
book_name = input("Enter the book name : ")
book_adress = input("Enter the book adress : ")
book_status = input("Enter the book status : ")
with open("D:\\daima\\data.txt", "r", encoding="utf-8") as fr:
books = fr.readlines()
for line in books:
if line[0] == book_id:
with open("D:\\daima\\data.txt", "w", encoding="utf-8") as fw:
motify_content = f"{book_id},{book_name},{book_adress},{book_status}"
books[int(book_id)] = motify_content + '\n'
fw.writelines(books)
print(f"已修改图书信息的为:id:{book_id},name:{book_name},adress:{book_adress},status:{book_status}")
else:
continue
查询:
#查
def book_search():
print("Book search")
with open("D:\\daima\\data.txt","r",encoding="utf-8") as f:
books=f.readlines()
for book in books:
print(book,end="")
print("\n")
借阅:
同理,找到图书信息后,需要判断图书的状态是否为False,False代表未借出,True代表借出,如果为False则把该图书信息改为True,若没有检测到有输入的图书id系统会默认退出到功能界面。
注意:若把状态改进为数量,只需判断数量是否大于0.
#借
def book_borrow():
print("Book borrow")
with open("D:\\daima\\data.txt", "r", encoding="utf-8") as fr:
books=fr.readlines()
borrow_id=input("Enter the book id : ")
for line in books:
line = line.strip()
line=line.split(",")
#print(line[0])
if line[0]==borrow_id:
#print(line[3])
if line[3] == "False":
with open("D:\\daima\\data.txt", "w", encoding="utf-8") as fw:
new_content=f"{line[0]},{line[1]},{line[2]},{True}"
books[int(borrow_id)]=new_content+'\n'
fw.writelines(books)
print("书本借阅成功")
else:
print("该图书已被借阅")
else:
continue
归还:
与借阅同理。
#还
def book_return():
print("Book return")
with open("D:\\daima\\data.txt", "r", encoding="utf-8") as fr:
books=fr.readlines()
return_id=input("Enter the book id : ")
for line in books:
line = line.strip()
line=line.split(",")
if line[0]==return_id:
#print(bool(line[3]))
if line[3]=="True":
with open("D:\\daima\\data.txt", "w", encoding="utf-8") as fw:
print("%s,%s,%s,Flase" % (line[0], line[1], line[2]))
new_content = f"{line[0]},{line[1]},{line[2]},{False}"
books[int(return_id)] = new_content + '\n'
fw.writelines(books)
print("书本归还成功")
else:
print("没有借阅记录")
else:
continue
功能选择:
#主菜单
while True:
print("该图书馆管理系统的功能有:"
"1.增加图书信息"
"2.删除图书信息"
"3.修改图书信息"
"4.查询图书信息"
"5.借阅图书"
"6.归还图书"
"7.退出系统"
)
number=input("Enter the number: ")
if number=="1":
book_add()
elif number=="2":
book_delete()
elif number=="3":
book_motify()
elif number=="4":
book_search()
elif number=="5":
book_borrow()
elif number=="6":
book_return()
else:
break