python编写图书管理系统

yizhihongxing

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方法即可使用。

具体操作如下:

  1. 打开命令行或者终端,切换到.py文件所在的目录下
  2. 输入python xxx.py(xxx为你的.py文件名),运行程序
  3. 按照菜单提示进行选择,即可完成对图书的基本管理和查询操作

总结

通过本文的介绍,我们知道了使用Python编写图书管理系统的完整攻略。这是一个非常基础的小型应用程序,希望可以对初学者有所帮助。在实际开发中,还需要考虑数据持久化、用户权限等方面的问题,此处未作过多展开,需要读者自行完善。

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:python编写图书管理系统 - Python技术站

(0)
上一篇 2023年5月30日
下一篇 2023年5月30日

相关文章

  • Python爬取读者并制作成PDF

    本攻略将介绍如何使用Python爬取小说网站的数据,并使用Python的pdfkit库将小说内容制作成PDF文件。 爬取小说内容 我们可以使用Python的requests库和BeautifulSoup库爬取小说网站的数据。以下是一个示例代码,用于爬取小说内容: import requests from bs4 import BeautifulSoup ur…

    python 2023年5月15日
    00
  • python 实现PIL模块在图片画线写字

    如果要在Python中对图片进行处理,常用的模块是Python Imaging Library (PIL)。PIL提供了很多操作图片的方法,包括在图片上描绘线条和文字。 在Python中实现PIL模块在图片画线写字需要安装PIL模块。可以用pip指令安装:pip install Pillow,或者直接从官网下载安装包手动安装。 接下来,我们来讲两个示例来说明…

    python 2023年5月13日
    00
  • 详解Python3.6的py文件打包生成exe

    下面我就详细讲解“详解Python3.6的py文件打包生成exe”的完整攻略。 简介 在Python开发中,经常需要将程序包装成exe文件形式,从而实现批量处理、部署、传输等功能。Python3中,我们可以使用pyinstaller模块来实现将.py文件打包成.exe文件。 打包过程 安装pyinstaller模块 首先,我们需要安装pyinstaller模…

    python 2023年5月14日
    00
  • 解决python问题 Traceback (most recent call last)

    当Python程序出现错误时,通常会输出Traceback信息,其中包含了错误的详细信息和错误发生的位置。Traceback信息通常以最后一次调用为起点,向上追溯程序的入口点。本攻略将提供解决Python问题Traceback(most recent call last)的完整攻略,包括常见错误类型和解决方法,并提供两个示例。 常见错误类型 以下是Pytho…

    python 2023年5月13日
    00
  • python上的简单迭代

    【问题标题】:simple Iteration on pythonpython上的简单迭代 【发布时间】:2023-04-01 20:00:02 【问题描述】: 我的目标是编写一个类并仅使用__iter__ 和next 方法来查找数字的除数。这是我写的: class Divisors(object): def __init__(self, integer):…

    Python开发 2023年4月8日
    00
  • Python离线安装PIL 模块的方法

    下面是详细讲解Python离线安装PIL模块的方法的完整攻略: 环境准备 首先需要下载PIL模块的安装包,可以从Pillow官方网站获取。下载地址:https://pypi.org/project/Pillow/#files 离线安装Python,建议使用Anaconda,因为在Anaconda中,可以通过conda这个软件包管理工具来进行离线安装。可在An…

    python 2023年5月14日
    00
  • Matplotlib控制坐标轴刻度间距与标签实例代码

    下面我会详细讲解一下Matplotlib控制坐标轴刻度间距与标签实例代码的完整攻略。 1. Matplotlib控制坐标轴刻度间距与标签的方法简介 在Matplotlib中,我们可以使用xticks和yticks方法来控制坐标轴的刻度间距和标签。xticks方法用于设置X轴刻度,而yticks方法用于设置Y轴刻度。 这两个方法的常用参数有: ticks:刻度…

    python 2023年6月6日
    00
  • Python 中random 库的详细使用

    下面是对“Python 中 random 库的详细使用”进行详细讲解的攻略。 一、什么是 random 库? random 库是 Python 标准库中的一个模块,它提供了用于生成随机数的函数。在进行数据处理、密码学、游戏编程等领域时,经常会使用到 random 库。 二、如何使用 random 库? 1. 随机整数 使用 random 模块中的 randi…

    python 2023年6月3日
    00
合作推广
合作推广
分享本页
返回顶部