コード例 #1
0
 def assign_book(self, book_id):
     book = Book.find_with_alert(book_id)
     if not book:
         return
     with open('db/member_books.txt', 'r+') as file:
         for line in file:
             if line.startswith(f"{self.record_id},{book_id}"):
                 print('Already assigned')
                 return
         file.write(f"{self.record_id},{book_id}\n")
     book.available -= 1
     book.save()
     return 'Operation successful'
コード例 #2
0
 def return_book(self, book_id):
     success = False
     book = Book.find_with_alert(book_id)
     if not book:
         return
     with open('db/member_books.txt', 'r+') as file:
         lines = file.readlines()
         file.seek(0)
         for line in lines:
             if line.startswith(f"{self.record_id},{book_id}"):
                 book.available += 1
                 book.save()
                 success = True
             else:
                 file.write(line)
         file.truncate()
     if success:
         return 'Operation successful'
     else:
         return 'Record not found'
コード例 #3
0
            print(
                f'{str(index).ljust(8)}{str(book.record_id).ljust(7)}{book.name.ljust(30)}{book.author.ljust(20)}{book.available}'
            )

    elif option == '7':
        name = input('Book name: ')
        author_name = input('Author name: ')
        available = input('Available book count: ')
        if confirm():
            book = Book(name, author_name, available)
            book.save()
            print('Book added successfully')

    elif option == '8':
        book_id = input('Book ID: ')
        book = Book.find_with_alert(book_id)
        if not book:
            continue
        print(
            "Enter the updated details. If you don't provide value it will remain unchanged"
        )
        name = input(f'Book name: ({book.name}): ').strip()
        author = input(f'Book author: ({book.author}): ').strip()
        available = input(f'Books available: ({book.available}): ').strip()
        if name:
            book.name = name
        if author:
            book.author = author
        if available:
            book.available = available
        if name or author or available: