class Book:
def __init__(self, bid, title, author, status=0):
self.bid = bid
self.title = title
self.author = author
self.status = status
self.next = None
class Books:
def __init__(self, file_name='book_info.txt'):
self.head = None
self.load_book_info(file_name)
self.books = []
def add_book(self):
bid = input('Input book id: ')
title = input('Input book title: ')
author = input('Input author name: ')
new_book = Book(bid, title, author)
if self.head is None:
self.head = new_book
else:
current = self.head
while current is not None:
if current.bid == new_book.bid:
print('ID existed')
return
if current.next is None:
current.next = new_book
break
current = current.next
self.store_book_info() # Gọi phương thức store_book_info() sau khi thêm sách
def load_book_info(self, file_name):
try:
with open(file_name, 'r') as f:
for line in f:
book_info = line.strip().split(',')
bid = book_info[0]
title = book_info[1]
author = book_info[2]
status = int(book_info[3])
new_book = Book(bid, title, author, status)
if self.head is None:
self.head = new_book
else:
current = self.head
while current.next is not None:
current = current.next
current.next = new_book
except FileNotFoundError:
print('Book information file not found')
def store_book_info(self, file_name='book_info.txt'):
if self.head is None:
print('No books found')
return
with open(file_name, 'w') as f: # Mở tệp ở chế độ 'w' để ghi đè nội dung hiện có
current = self.head
while current is not None:
f.write(
f'{current.bid},{current.title},{current.author},{current.status}\n')
current = current.next
print('Book information stored successfully')
def delete_book(self):
bid = input('Enter book id: ')
current = self.head
previous = None
while current is not None:
if current.bid == bid:
if previous is None:
self.head = current.next
print('Delete success')
else:
previous.next = current.next
print('Delete success')
return True
else:
previous = current
current = current.next
print('No result')
self.store_book_info()
def search_book(self):
bid = input('Enter book id: ')
current = self.head
while current is not None:
if current.bid == bid:
print(
f'{current.bid} | {current.title} | {current.author} | {current.status}')
return True
else:
current = current.next
print(f'Book with id {bid} was not found')
def view_book(self):
print('Bid | Title | Author | Status(0-available)')
if self.head is None:
print('Library is empty')
else:
current = self.head
while current is not None:
print(
f'{current.bid} | {current.title} | {current.author} | {current.status}')
current = current.next
def borrow(self):
bid = input('Enter book id: ')
current = self.head
while current is not None:
if current.bid == bid:
if current.status == 0:
current.user = input('Enter borrower name: ')
print('Borrow success')
current.status = 1
return True
else:
print(f'Book with id {bid} is not available')
return False
else:
current = current.next
print(f'Book with id {bid} was not found')
self.store_book_info()
def return_book(self):
bid = input('Enter book id: ')
current = self.head
while current is not None:
if current.bid == bid:
if current.status == 1:
print('Return success')
current.status = 0
return True
else:
print(f'Book with id {bid} was already returned')
return False
else:
current = current.next
print(f'Book with id {bid} was available')
self.store_book_info()
def borrowed_inf(self):
print('Bid | Borrower ')
if self.head is None:
print('Bag is empty')
else:
current = self.head
while current is not None:
if current.status == 1:
print(f'{current.bid} | {current.user}')
current = current.next
return
def choice():
p = Books()
while p is not None:
print('1.Add Book(s)')
print('2.View Book(s)')
print('3.Search Book')
print('4.Delete books')
print('5.Borrow book')
print('6.Return book')
print('7.Borrowed books')
print('8.Exit')
try:
a = int(input('Welcome! What do you want to do? '))
if a >= 1 and a <= 8:
if a == 1:
p.add_book()
if a == 2:
p.view_book()
if a == 3:
p.search_book()
if a == 4:
p.delete_book()
p.store_book_info()
if a == 5:
p.borrow()
p.store_book_info()
if a == 6:
p.return_book()
p.store_book_info()
if a == 7:
p.borrowed_inf()
if a == 8:
print('Goodbye!')
break
else:
print('Enter valid choice')
except:
print('Enter valid choice')
if __name__ == '__main__':
choice()