Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write a Library Management system with python

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Most people do not understand the knowledge points of this article "how to write a library management system with python", so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to use python to prepare a library management system" article.

Topic: write a simple book lending system ‪‬‮‬‪‬‭‬‪‬‮‬‭‬‫‬‪‬‮‬‭‬‫‬‪‬‮‬‪‬‭‬‪‬‮‬‪‬‮‬‪‬‫‬

School second-hand books are a big market. Teacher A decided to get involved in this field. She needs to write a library management system to help her manage the borrowing of books. She hopes the program can do this: ‪‬‮‬‪‬‭‬‪‬‮‬‭‬‫‬‪‬‮‬‭‬‫‬‪‬‮‬‪‬‭‬‪‬‮‬‪‬‮‬‪‬‫‬

1. Query books: you can query the information and lending information of all books in the system with one click ‪‬‮‬‪‬‭‬‪‬‮‬‭‬‫‬‪‬‮‬‭‬‫‬‪‬‮‬‪‬‭‬‪‬‮‬‪‬‮‬‪‬‫‬

two。 Add books: when adding books to the system, you need to enter basic information about books (title, author) Reason for recommendation) ‪‬‮‬‪‬‭‬‪‬‮‬‭‬‫‬‪‬‮‬‭‬‫‬‪‬‮‬‪‬‭‬‪‬‮‬‪‬‮‬‪‬‫‬

3. Borrow books: books can be borrowed only when the status of the book is "unlent". The status of the loaned book is "lent" ‪‬‮‬‪‬‭‬‪‬‮‬‭‬‫‬‪‬‮‬‭‬‫‬‪‬‮‬‪‬‭‬‪‬‮‬‪‬‮‬‪‬‫‬

4. Return the book: after the successful return, the status of the book will be changed to "unlent", and the next student can borrow it again. ‪‬‮‬‪‬‭‬‪‬‮‬‭‬‫‬‪‬‮‬‭‬‫‬‪‬‮‬‪‬‭‬‪‬‮‬‪‬‮‬‪‬‫‬

Now that you've learned about classes and objects, you don't have to think about other programming patterns. ‪‬‮‬‪‬‭‬‪‬‮‬‭‬‫‬‪‬‮‬‭‬‫‬‪‬‮‬‪‬‭‬‪‬‮‬‪‬‮‬‪‬‫‬

There are two uses of class: 1. Class as a template for instance objects 2. Class as a function package. The first use is to use classes to generate instance objects. The class acts as a template for instance objects, and after each instance is created, it will have all the properties and methods of the class. The second use is to use classes to package multiple functions (methods) together so that the methods in the class cooperate with each other. ‪‬‮‬‪‬‭‬‪‬‮‬‭‬‫‬‪‬‮‬‭‬‫‬‪‬‮‬‪‬‭‬‪‬‮‬‪‬‮‬‪‬‫‬

In this project, the object is to deal with each specific book, and each book has its own attribute information, so you can define a Book class and use the Book class to create instances of each book. Bind property (first usage) ‪‬‮‬‪‬‭‬‪‬‮‬‭‬‫‬‪‬‮‬‭‬‫‬‪‬‮‬‪‬‭‬‪‬‮‬‪‬‮‬‪‬‫‬

For the running body of this management system, it is the superposition of multiple optional functions, so we can create a system running class BookManager. Encapsulate functions such as querying books, adding books, etc., into methods in the class for calling (the second use) ‪‬‮‬‪‬‭‬‪‬‮‬‭‬‫‬‪‬‮‬‭‬‫‬‪‬‮‬‪‬‭‬‪‬‮‬‪‬‮‬‪‬‫‬

In class, we have written the basic functions, as shown in the attachment 1BookRent.py ‪‬‮‬‪‬‭‬‪‬‮‬‭‬‫‬‪‬‮‬‭‬‫‬‪‬‮‬‪‬‭‬‪‬‮‬‪‬‮‬‪‬‫‬

-- ‪‬‮‬‪‬‭‬‪‬‮‬‭‬‫‬‪‬‮‬‭‬‫‬‪‬‮‬‪‬‭‬‪‬‮‬‪‬ ‪‬‪‬‮‬‪‬‫‬

Upgrade the program: ‪‬‮‬‪‬‭‬‪‬‮‬‭‬‫‬‪‬‮‬‭‬‫‬‪‬‮‬‪‬‭‬‪‬‮‬‪‬‮‬‪‬‫‬

1. The book information is obtained from the CSV file (Annex 2). Pay attention to modifying the status of the book ‪‬‮‬‪‬‭‬‪‬‮‬‭‬‫‬‪‬‮‬‭‬‫‬‪‬‮‬‪‬‭‬‪‬‮‬‪‬‮‬‪‬‫‬ when borrowing and returning it.

two。 Consider classifying books: inherit the Book class, create some subclasses, such as FictionBook, and modify its initialization method Add the parameter type=' fantasy class' ‪‬‮‬‪‬‭‬‪‬‮‬‭‬‫‬‪‬‮‬‭‬‫‬‪‬‮‬‪‬‭‬‪‬‮‬‪‬‮‬‪‬‫‬

3. Implement fuzzy query: for example, if you let the user enter the author's name, you can print out the relevant information about all the author's books in the system; enter the fantasy class and print out the relevant information about all such books.

The CSV table section is as follows:

Code:

# coding = utf-8import csvfrom itertools import isliceimport pandas as pdimport osclass Book: def _ _ init__ (self, name, author, recommendation, type Status=0): # initialize instance properties self.name = name self.author = author self.recommendation = recommendation self.status = status self.type = type def _ _ str__ (self): if self.status = = 1: status= 'has loaned' else: status= 'but not the name' return': "% s" author:% s recommendation:% s category:% s status:% s'% (self.name Self.author, self.recommendation, self.type, status) class FictionBook (Book): def _ _ init__ (self, type=' Fantasy'): self.type = typeclass NovelBook (Book): def _ init__ (self, type=' novel'): self.type = typeclass JottingsBook (Book): def _ init__ (self Type=' essay'): self.type = typeclass BookManager: books = [] def _ init__ (self): F = open ('BookInfos.csv', 'r') readers = csv.reader (f) for row in islice (readers, 1, None): self.books.append (Book (row [0], row [1], row [2], row [3]) Row [4]) f.close () def menu (self): # Show selection menu Call different methods print according to the options of different menus ('welcome to use big data team Book Saving system, every book is a good book. I hope Mr. Zhong's market is getting better and better. \ n') while True: print ('1. Query books\ n2. Add books\ n3. Borrow books\ n4. Return books\ n5. Exit the system\ n') choice = int (input ('Please enter the function corresponding to the numeric option) For example: 1 (refers to....)) if choice = = 1: self.show_all_book () elif choice = = 2: self.add_book () elif choice = = 3: self.lend_book () elif choice = = 4: self.return_book () Elif choice = = 5: print ('Thank you for using! Welcome to come again!') Break def show_all_book (self): # display book information print ("1. Query the author's related books\ n2. Query category-related books\ n3. Query all books ") ch = eval (input ('Please enter your choice: for example: 1) if ch = 1: author = input (" Please enter the author's name:) for book in self.books: if book.author = = author: print ('') Print (book) elif ch = = 2: type = input ("Please enter a book category:") for book in self.books: if book.type = = type: print ('') print (book) elif ch = = 3: for book in self.books: Print (') print (book) else: print ("you typed incorrectly!") Def add_book (self): # add a book new_name = input ('Please enter the name of the book:') new_author = input ('Please enter the author of the book:') new_comment = input ('Please enter recommendations for the book:') new_type = input ('please enter the category of the book:') new_book = [new_name, new_author, new_comment, new_type 0] # write CSV file f = open ('BookInfos.csv',' asides') writer = csv.writer (f) writer.writerow (new_book) f.close () # change books [] self.books.append (Book (new_book [0], new_book [1], new_book [2], new_book [3]) New_ Bookbook [4]) print ('book input successful! \ n') def check_book (self Name): # check whether the book exists for book in self.books: if book.name = = name: return book else: return-1 def lend_book (self): # borrowed book name = input ('Please enter the name of the book you want to borrow:) res = self.check_book (name) If res! =-1: if res.status = = 1: print ('you're late The book has been borrowed') else: print ('borrowed successfully! Res.status = 1 manager.change_status (name) else: print ('this book is not in the system yet!') Def return_book (self): # return the book name = input ('Please enter the name of the returned book:') res = self.check_book (name) if res =-1: print ('without this book, I'm afraid you typed the wrong title ~') else: if res.status = = 0: print ('this book has not been borrowed!') Else: print ('returned successfully') res.status = 0 manager.change_status (name) # here I have some problems with the experiment, get rid of manager. It will appear that name is not defined is not good at learning and doesn't know enough about python. Excuse @ staticmethod def change_status (name): # modify the status of borrowed and returned books in the CSV file in_file = open ("BookInfos.csv", "r") reader = csv.reader (in_file) out_file = open ("BookInfos1.csv", "w") Newline='') writer = csv.writer (out_file) for row in reader: if row [0] = = name: if row [4] = '0mm: row [4] =' 1' else: row [4] ='0' writer.writerow (row) In_file.close () out_file.close () os.remove ("BookInfos.csv") # Delete the original os.rename ("BookInfos1.csv") "BookInfos.csv") # rename the write file This method is not advisable, but I can't find a good method for the time being. Print ("modified CSV file") # here I also have some problems when I do the experiment. 1. Specific cells are not modified, 2. The method of replacing the original file after modification is chosen. If this method is really used in the management of large files, it will lead to a great reduction in efficiency and undesirable. Excuse me, manager = BookManager () manager.menu (). The above is about "how to write a library management system with python". I believe everyone has a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge. Please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report