当前位置:   article > 正文

图书管理系统--Python基础项目(2)_图书管理系统的编写: 图书类book: 属性:书名name 作者author 是否借出isborro

图书管理系统的编写: 图书类book: 属性:书名name 作者author 是否借出isborrow 书

一、需求:

"""
5.图书管理系统的编写
图书类Book:
​     属性:书名name  作者author  是否借出isborrow  书籍位置index
​     注意:书籍的位置不能重复
图书管理系统BookManager类
​     存放图书的工具使用列表
​     方法:
​           1.添加图书
​           2.借书 (根据图书名字借书)
​              要检验图书是否存在、图书是否已经借出
​           3.还书
​           4.查询书籍 (根据名字查询)
"""

 二、编程

1、框架

  1. class Book():
  2. __slots__ = ("name","author","isborrow","index")
  3. def __init__(self,name,author,index):
  4.         pass
  5. def __str__(self):
  6. pass
  7. class BookManage():
  8. booklist = [] #图书库
  9. def start_book(self): #图书库初始化
  10. pass
  11. def menu(self):
  12. self.start_book()
  13. while True:
  14. print("欢迎进入图书管理系统".center(100, "="))
  15. print("""
  16. 1. 添加图书
  17. 2. 借书
  18. 3. 还书
  19. 4. 查询书籍
  20. 5. 查看所有书籍
  21. 6. 退出""")
  22. select = int(input("请选着功能:"))
  23. if select == 1:
  24. self.add_book()
  25. elif select == 2:
  26. self.borrow_book()
  27. elif select == 3:
  28. self.return_book()
  29. ...
  30. def check_book(self,bookname):
  31. pass
  32. ...

设置两个类,分别为Book、与BookManage(遵循大驼峰规则)

BookManage:内嵌图书库初始化函数

menu函数:内嵌各类增、删、改、查函数

2、功能实现

1. Book类实现

  1. class Book():
  2. __slots__ = ("name","author","isborrow","index")
  3. def __init__(self,name,author,index):
  4. self.name = name
  5. self.author = author
  6. self.index = index
  7. self.isborrow = False # 初始状态为未借出
  8. def __str__(self):
  9. return f"书名为{self.name},作者为{self.author},索引号为{self.index}"
  10. __repr__ = __str__ # 当打印一个对象的时候,默认会打印该对象的地址,如果在类中重写了__str__函数,则打印对象的时候会调用__str__,从而打印出和对象相关的属性信息

1. __slots__ =  (" ",):属性限制,若属性为单一属性,必须加","消除歧义

2. __init__():初始化函数(初始化类对象)、__new__():创建类对象(系统自动调用,无需敲出)

3. __str__(self):重写系统自带函数(可通过dir()查看),重写后不再打印改对象的地址

2. 图书库初始化函数

  1. booklist = [] #图书库
  2. def start_book(self): #图书库初始化
  3. book1 = Book("西游记", "wjy", 1)
  4. book2 = Book("三国演绎", "qs", 2)
  5. book3 = Book("红楼梦", "cocair", 3)
  6. self.booklist = [book1,book2,book3]
  7. print(f"书籍有{self.booklist}")

 创建三个Book对象,初始化图书库(self.booklist = []列表初始化的方法)

 3. 菜单函数

  1. def menu(self):
  2. self.start_book()
  3. while True:
  4. print("欢迎进入图书管理系统".center(100, "="))
  5. print("""
  6. 1. 添加图书
  7. 2. 借书
  8. 3. 还书
  9. 4. 查询书籍
  10. 5. 查看所有书籍
  11. 6. 退出""")
  12. select = int(input("请选着功能:"))
  13. if select == 1:
  14. self.add_book()
  15. elif select == 2:
  16. self.borrow_book()
  17. pass
  18. elif select == 3:
  19. self.return_book()
  20. elif select == 4:
  21. bookname = input("请输入书本名:")
  22. self.check_book(bookname)
  23. elif select == 5:
  24. self.show_all_book()
  25. elif select == 6:
  26. print("退出成功,欢迎再次登入~~")
  27. break
  28. else:
  29. print("暂无此功能")

用死循环while True控制 

4. 查询函数

  1. def check_book(self,bookname):
  2. for bname in self.booklist:
  3. if bname.name == bookname:
  4. print(f"{bname.name}存在")
  5. return True
  6. return print(f"您输入的{bookname}不存在嗷~~")

5. 增加函数

  1. def add_book(self):
  2. book_name = input("请输入图书名:")
  3. book_author = input("请输入图书作者:")
  4. # book_index = input("请输入图书索引号:")
  5. book_indexs = [book.index for book in self.booklist]
  6. # for book_self in book_indexs:
  7. # if book_self != book_index:
  8. # book = Book(book_name,book_author,book_index)
  9. # self.booklist.append(book)
  10. # print("图书添加成功")
  11. # break
  12. # else:
  13. # print("该图书已经在图书库中")
  14. # break #没有考虑到索引存在,而再次输入新书名情况
  15. while True:
  16. bindex = input("请输入存放书的位置:")
  17. # 筛查书籍位置
  18. if bindex not in book_indexs:
  19. # 这是一个新的位置
  20. break
  21. else:
  22. print('该位置已有书籍 请重新设置位置')
  23. book = Book(book_name,book_author,bindex)
  24. self.booklist.append(book)
  25. print("添加图书%s成功!" % (book))

列表推导式赋值book_indexs:用于判断图书是否存在于图书库中 

 6. 借出函数

  1. def borrow_book(self):
  2. bookname = input("请输入需要借阅的书:")
  3. if self.check_book(bookname): # 没错,但面向对象不测底
  4. for book_self in self.booklist:
  5. if book_self.name == bookname:
  6. if book_self.isborrow:
  7. print(f"无法借阅,该图书{book_self}已经借出")
  8. break
  9. else:
  10. book_self.isborrow = True
  11. print(f"图书{book_self}借阅成功")
  12. break
  13. else:
  14. print(f"{bookname}不在此图书系统中")

 用book_self.isborrow来标志书本是否借出

7. 归还函数

  1. def return_book(self):
  2. bookname = input("请输入待还的书:")
  3. if self.check_book(bookname):
  4. for book_self in self.booklist:
  5. if book_self.name == bookname:
  6. if book_self.isborrow:
  7. book_self.isborrow = False
  8. print(f"该图书{book_self}退还成功")
  9. break
  10. else:
  11. print(f"图书{book_self}无需退还")
  12. break
  13. else:
  14. print(f"{bookname}不在此图书系统中")

8. 展示函数

  1. def show_all_book(self):
  2. for book_ in self.booklist:
  3. print(book_)

三、运行与实现 

代码:

  1. class Book():
  2. __slots__ = ("name","author","isborrow","index")
  3. def __init__(self,name,author,index):
  4. self.name = name
  5. self.author = author
  6. self.index = index
  7. self.isborrow = False # 初始状态为未借出
  8. def __str__(self):
  9. return f"书名为{self.name},作者为{self.author},索引号为{self.index}"
  10. __repr__ = __str__ # 当打印一个对象的时候,默认会打印该对象的地址,如果在类中重写了__str__函数,则打印对象的时候会调用__str__,从而打印出和对象相关的属性信息
  11. class BookManage():
  12. booklist = [] #图书库
  13. def start_book(self): #图书库初始化
  14. book1 = Book("西游记", "wjy", 1)
  15. book2 = Book("三国演绎", "qs", 2)
  16. book3 = Book("红楼梦", "cocair", 3)
  17. self.booklist = [book1,book2,book3]
  18. print(f"书籍有{self.booklist}")
  19. def menu(self):
  20. self.start_book()
  21. while True:
  22. print("欢迎进入图书管理系统".center(100, "="))
  23. print("""
  24. 1. 添加图书
  25. 2. 借书
  26. 3. 还书
  27. 4. 查询书籍
  28. 5. 查看所有书籍
  29. 6. 退出""")
  30. select = int(input("请选着功能:"))
  31. if select == 1:
  32. self.add_book()
  33. elif select == 2:
  34. self.borrow_book()
  35. pass
  36. elif select == 3:
  37. self.return_book()
  38. elif select == 4:
  39. bookname = input("请输入书本名:")
  40. self.check_book(bookname)
  41. elif select == 5:
  42. self.show_all_book()
  43. elif select == 6:
  44. print("退出成功,欢迎再次登入~~")
  45. break
  46. else:
  47. print("暂无此功能")
  48. def check_book(self,bookname):
  49. for bname in self.booklist:
  50. if bname.name == bookname:
  51. print(f"{bname.name}存在")
  52. return True
  53. return print(f"您输入的{bookname}不存在嗷~~")
  54. def add_book(self):
  55. book_name = input("请输入图书名:")
  56. book_author = input("请输入图书作者:")
  57. # book_index = input("请输入图书索引号:")
  58. book_indexs = [book.index for book in self.booklist]
  59. # for book_self in book_indexs:
  60. # if book_self != book_index:
  61. # book = Book(book_name,book_author,book_index)
  62. # self.booklist.append(book)
  63. # print("图书添加成功")
  64. # break
  65. # else:
  66. # print("该图书已经在图书库中")
  67. # break #没有考虑到索引存在,而再次输入新书名情况
  68. while True:
  69. bindex = input("请输入存放书的位置:")
  70. # 筛查书籍位置
  71. if bindex not in book_indexs:
  72. # 这是一个新的位置
  73. break
  74. else:
  75. print('该位置已有书籍 请重新设置位置')
  76. book = Book(book_name,book_author,bindex)
  77. self.booklist.append(book)
  78. print("添加图书%s成功!" % (book))
  79. def borrow_book(self):
  80. bookname = input("请输入需要借阅的书:")
  81. if self.check_book(bookname): # 没错,但面向对象不测底
  82. for book_self in self.booklist:
  83. if book_self.name == bookname:
  84. if book_self.isborrow:
  85. print(f"无法借阅,该图书{book_self}已经借出")
  86. break
  87. else:
  88. book_self.isborrow = True
  89. print(f"图书{book_self}借阅成功")
  90. break
  91. else:
  92. print(f"{bookname}不在此图书系统中")
  93. def return_book(self):
  94. bookname = input("请输入待还的书:")
  95. if self.check_book(bookname):
  96. for book_self in self.booklist:
  97. if book_self.name == bookname:
  98. if book_self.isborrow:
  99. book_self.isborrow = False
  100. print(f"该图书{book_self}退还成功")
  101. break
  102. else:
  103. print(f"图书{book_self}无需退还")
  104. break
  105. else:
  106. print(f"{bookname}不在此图书系统中")
  107. def show_all_book(self):
  108. for book_ in self.booklist:
  109. print(book_)
  110. bookmanage = BookManage()
  111. bookmanage.menu()

测试:

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/556003
推荐阅读
相关标签
  

闽ICP备14008679号