Python编写图书管理系统
简述
本文将介绍使用Python编写图书管理系统的完整攻略。图书管理系统是一种常见的信息管理系统,它可以对图书进行基本的管理和查询操作。Python作为一种高效、简洁的编程语言,适合用来编写此类小型应用程序。
开发环境
本文使用Python 3.6及以上版本进行开发,并在Windows、MacOS和Linux操作系统上测试通过。开发工具为Python自带的IDLE或者PyCharm。
设计思路
本文所述的图书管理系统是一个基于命令行操作的简单系统,实现了以下功能:
- 输入新书的基本信息,包括书名、作者、ISBN编号、价格等
- 修改已有图书的信息
- 按照书名、ISBN编号或作者查询图书
- 浏览系统中所有图书
基于这个功能需求,我们将采用面向对象的编程思路,设计以下类:
Book
封装了一个书籍的基本信息,包括书名、作者、ISBN编号、价格。该类有以下方法:
__init__(self, title, author, isbn, price)
用于初始化一个Book对象getTitle(self)
返回书籍的标题getAuthor(self)
返回书籍的作者getISBN(self)
返回书籍的ISBN编号getPrice(self)
返回书籍的价格setTitle(self, title)
设置书籍的标题setAuthor(self, author)
设置书籍的作者setISBN(self, isbn)
设置书籍的ISBN编号setPrice(self, price)
设置书籍的价格
Library
封装了一个图书馆的信息,包括所有书籍的列表。该类有以下方法:
__init__(self)
用于初始化一个Library对象addBook(self, book)
向图书馆中添加一本书removeBook(self, book)
从图书馆中移除一本书searchByTitle(self, title)
按照书名查询书籍searchByISBN(self, isbn)
按照ISBN编号查询书籍searchByAuthor(self, author)
按照作者查询书籍showAllBooks(self)
显示所有书籍
Menu
封装了一个文本菜单的信息,用于输出菜单项和处理用户输入。该类有以下方法:
__init__(self)
用于初始化一个Menu对象showMenu(self)
输出菜单项run(self)
处理用户输入
示例代码
Book类示例:
class Book:
def __init__(self, title, author, isbn, price):
self.title = title
self.author = author
self.isbn = isbn
self.price = price
def getTitle(self):
return self.title
def getAuthor(self):
return self.author
def getISBN(self):
return self.isbn
def getPrice(self):
return self.price
def setTitle(self, title):
self.title = title
def setAuthor(self, author):
self.author = author
def setISBN(self, isbn):
self.isbn = isbn
def setPrice(self, price):
self.price = price
Library类示例:
class Library:
def __init__(self):
self.books = []
def addBook(self, book):
self.books.append(book)
def removeBook(self, book):
self.books.remove(book)
def searchByTitle(self, title):
results = []
for book in self.books:
if book.getTitle() == title:
results.append(book)
return results
def searchByISBN(self, isbn):
results = []
for book in self.books:
if book.getISBN() == isbn:
results.append(book)
return results
def searchByAuthor(self, author):
results = []
for book in self.books:
if book.getAuthor() == author:
results.append(book)
return results
def showAllBooks(self):
for book in self.books:
print(f'Title: {book.getTitle()}')
print(f'Author: {book.getAuthor()}')
print(f'ISBN: {book.getISBN()}')
print(f'Price: {book.getPrice()}')
Menu类示例:
class Menu:
def __init__(self):
self.library = Library()
def showMenu(self):
print("1. Add a new book")
print("2. Remove a book")
print("3. Search by title")
print("4. Search by ISBN")
print("5. Search by author")
print("6. Show all books")
print("0. Exit")
def run(self):
while True:
self.showMenu()
choice = int(input("Enter your choice: "))
if choice == 1:
title = input("Enter title: ")
author = input("Enter author: ")
isbn = input("Enter ISBN: ")
price = float(input("Enter price: "))
book = Book(title, author, isbn, price)
self.library.addBook(book)
elif choice == 2:
title = input("Enter title to remove: ")
books = self.library.searchByTitle(title)
if len(books) == 0:
print("No book found!")
else:
for book in books:
self.library.removeBook(book)
elif choice == 3:
title = input("Enter title to search: ")
books = self.library.searchByTitle(title)
if len(books) == 0:
print("No book found!")
else:
for book in books:
print(f'Title: {book.getTitle()}')
print(f'Author: {book.getAuthor()}')
print(f'ISBN: {book.getISBN()}')
print(f'Price: {book.getPrice()}')
elif choice == 4:
isbn = input("Enter ISBN to search: ")
books = self.library.searchByISBN(isbn)
if len(books) == 0:
print("No book found!")
else:
for book in books:
print(f'Title: {book.getTitle()}')
print(f'Author: {book.getAuthor()}')
print(f'ISBN: {book.getISBN()}')
print(f'Price: {book.getPrice()}')
elif choice == 5:
author = input("Enter author to search: ")
books = self.library.searchByAuthor(author)
if len(books) == 0:
print("No book found!")
else:
for book in books:
print(f'Title: {book.getTitle()}')
print(f'Author: {book.getAuthor()}')
print(f'ISBN: {book.getISBN()}')
print(f'Price: {book.getPrice()}')
elif choice == 6:
self.library.showAllBooks()
elif choice == 0:
break
else:
print("Invalid choice!")
使用方法
将上述示例代码保存到三个不同的.py文件中(或者统一存放到一个.py文件中),并在终端中运行Menu类的run方法即可使用。
具体操作如下:
- 打开命令行或者终端,切换到.py文件所在的目录下
- 输入
python xxx.py
(xxx为你的.py文件名),运行程序 - 按照菜单提示进行选择,即可完成对图书的基本管理和查询操作
总结
通过本文的介绍,我们知道了使用Python编写图书管理系统的完整攻略。这是一个非常基础的小型应用程序,希望可以对初学者有所帮助。在实际开发中,还需要考虑数据持久化、用户权限等方面的问题,此处未作过多展开,需要读者自行完善。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:python编写图书管理系统 - Python技术站