Python basic article series
Python Basics (1) Basic understanding of Python and environment construction | Python Basics (1) Basic understanding of Python and environment construction |
---|---|
Python basics (2) Python variables and basic data types | Python basics (2) Python variables and basic data types |
Python basics (3) Python basic statements and basic operations | Python basics (3) Python basic statements and basic operations |
Python basics (4) Python functions | Python basics (4) Python functions |
Python Basics (5) Python Packages and Modules | Python Basics (5) Python Packages and Modules |
Python basics (6) Python file module | Python basics (6) Python file module |
Python Basics (7) Python’s exception handling mechanism | Python Basics (7) Python’s exception handling mechanism |
Python Basics (8) Python Classes and Objects | Python Basics (8) Python Classes and Objects |
Python Basics (9) Python’s built-in modules | Python Basics (9) Python’s built-in modules |
Python basics (10) Python implements a simple library management system | Python basics (10) Python implements a simple library management system |
4.10 Simple practice of library management system
The implementation of a simple library management system, the main functions are as follows:
- A class is defined
Library
for managing books in the library. - Provides functions such as adding books, deleting books, finding books, modifying books, displaying all books, saving books, loading books, and sorting books.
- Through file operations, you can save book information to a text file named "book.txt" and load book information from this file.
- A command line menu is provided that allows the user to select different operations such as add, delete, find, modify, display, save, load and sort books.
- The class is used
Book
to represent book information, including book title, author, price, publisher, publication date, rating, number of reviews, and links.
Users can manage books in the library by entering corresponding operation numbers to perform different operations.
- Books
# ---encoding:utf-8---
# @Time : 2023/9/3 19:30
# @Author : Darwin_Bossen
# @Email :[email protected]
# @Site : 书籍类
# @File : Book.py
class Book:
# 书籍类
def __init__(self, name, author, price, publish, date, score, comment, url):
self.name = name
self.author = author
self.price = price
self.publish = publish
self.date = date
self.score = score
self.comment = comment
self.url = url
# 重写str方法
def __str__(self):
return "书名:" + self.name + "\n作者:" + self.author + "\n价格:" + self.price + "\n出版社:" + self.publish + "\n出版日期:" + self.date + "\n评分:" + self.score + "\n评论数:" + self.comment + "\n链接:" + self.url
- Library class
# ---encoding:utf-8---
# @Time : 2023/9/3 19:32
# @Author : Darwin_Bossen
# @Email :[email protected]
# @Site : 图书类
# @File : Library.py
from Book import Book
# 图书馆类
class Library:
def __init__(self):
self.book_list = []
def __str__(self):
return str(len(self.book_list)) + "本书"
# 显示所有书籍
def showBook(self):
for book in self.book_list:
print(book)
print("--------------------------------------------------")
# 添加书籍
def addBook(self, book):
self.book_list.append(book)
# 删除书籍
def delBook(self, book):
if book in self.book_list:
self.book_list.remove(book)
print("删除成功")
else:
print("删除失败")
# 查找书籍
def findBook(self, name):
for book in self.book_list:
if book.name == name:
print(book)
return book
print("查无此书")
return None
# 修改书籍
def modifyBook(self, book):
if book in self.book_list:
self.book_list.remove(book)
self.book_list.append(book)
print("修改成功")
else:
print("修改失败")
# 保存书籍
def saveBook(self):
with open("book.txt", "w", encoding="utf-8") as f:
for book in self.book_list:
f.write(book.name + "," + book.author + "," + book.price + "," + book.publish + "," + book.date + "," + book.score + "," + book.comment + "," + book.url + "\n")
print("保存成功")
# 加载书籍
def loadBook(self):
with open("book.txt", "r", encoding="utf-8") as f:
while True:
line = f.readline()
if line == "":
break
book = line.split(",")
self.book_list.append(book)
print("加载成功")
# 排序
def sortBook(self):
self.book_list.sort(key=lambda book:book.score, reverse=True)
print("排序成功")
if __name__ == '__main__':
library = Library()
while True:
print("1.添加书籍")
print("2.删除书籍")
print("3.查找书籍")
print("4.修改书籍")
print("5.显示所有书籍")
print("6.保存书籍")
print("7.加载书籍")
print("8.排序")
print("0.退出")
num = input("请输入操作序号:")
if num == "1":
name = input("请输入书名:")
author = input("请输入作者:")
price = input("请输入价格:")
publish = input("请输入出版社:")
date = input("请输入出版日期:")
score = input("请输入评分:")
comment = input("请输入评论数:")
url = input("请输入链接:")
book = Book(name, author, price, publish, date, score, comment, url)
library.addBook(book)
elif num == "2":
name = input("请输入书名:")
book = library.findBook(name)
library.delBook(book)
elif num == "3":
name = input("请输入书名:")
library.findBook(name)
elif num == "4":
name = input("请输入书名:")
book = library.findBook(name)
if book != None:
author = input("请输入作者:")
price = input("请输入价格:")
publish = input("请输入出版社:")
date = input("请输入出版日期:")
score = input("请输入评分:")
comment = input("请输入评论数:")
url = input("请输入链接:")
book = Book(name, author, price, publish, date, score, comment, url)
library.modifyBook(book)
elif num == "5":
library.showBook()
elif num == "6":
library.saveBook()
elif num == "7":
library.loadBook()
elif num == "8":
library.sortBook()
elif num == "0":
break
- Effect
At this point, our basic knowledge of Python is complete. Here is the advanced knowledge of Python, threading