0% found this document useful (0 votes)
15 views

Python Assignment: using Class and OOP in python

Python Assignment: create a Library system using Class

Uploaded by

11247275
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Python Assignment: using Class and OOP in python

Python Assignment: create a Library system using Class

Uploaded by

11247275
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

myeopnnkm

Assignment 3 - Programming
December 29, 2024

Name: Nguyen Do Anh Duong


Class: EP16B
ID: 11247275

1 Library system
[1]: #Library asignment
class Book:
'''
this is where i work with informations of 1 book only
including its title, author, id, remaining copies
actions need to be in here:
init, str, borrow book - -1 remaining copies, return - +1 remaining␣
↪copies

'''

def __init__(self, book_id, title, author, B_copies):


self._book_id = book_id
self._title = title
self._author = author
self._B_copies = B_copies

def __str__(self):
return f'Book ID: {self._book_id}, Title: {self._title}, Author: {self.
↪_author}, Copies: {self._B_copies}.'

def B_borrow_book(self, B_copies_to_borrow = 1):


if B_copies_to_borrow <= self._B_copies:
self._B_copies -= B_copies_to_borrow
return True
else:
return False

def B_return_book(self, B_copies_to_return = 1):


self._B_copies += B_copies_to_return
return True

1
class User:
'''
this is where i work with 1 user's(guest) data: id, name, the list of books␣
↪they borrowed

actions:
- borrow book(book: Book): add a book into their list if there still enough␣
↪copies

- return book(book: Book): remove a book from their list


'''

def __init__(self, user_id, name):


self._user_id = user_id
self._name = name
self._list_of_borrowed_books = []

def U_borrow_book(self, book: Book):


if book.B_borrow_book():
self._list_of_borrowed_books.append(book)
return True
else:
return False

def U_return_book(self, book: Book):


if book in self._list_of_borrowed_books:
book.B_return_book
self._list_of_borrowed_books.remove(book)
return True
else:
return False

class Library:
'''
this is where i do the functions, this will affect the data of 2 classes␣
↪above

data: list of books in the library, list of users


actions:
- add.book(book: Book): add new book into the library list
- add user(user: User): add new user into the library list
- find book by title
- borrow book (user_id: str, book_id: str): allow user borrow a book
- return book
- display all books
- display user's borrow list (show title only)
'''

2
def __init__(self):
self._list_of_all_books = []
self._list_of_all_users = []

def add_book(self, book: Book):


self._list_of_all_books.append(book)
print(f'"{book._title}" has been succesfully added into the library.')

def add_user(self, user: User):


self._list_of_all_users.append(user)
print(f'{user._name} has sucessfully registered.')

def find_book_by_title(self, title):


for book in self._list_of_all_books:
if title.lower() == book._title.lower():
return book

return f'"{title}" not found'

def L_borrow_book(self, user_id, book_id):


user = next((user for user in self._list_of_all_users if user._user_id␣
↪== user_id), None)

book = next((book for book in self._list_of_all_books if book._book_id␣


↪== book_id), None)

if book and user:


if user.U_borrow_book(book):
return f'{user._name} has successfully borrowed {book._title}.'
elif user.U_borrow_book(book) == False:
return f'Not enough copies of "{book._title}" to borrow.'
else:
return f'Book or User not found.'

def L_return_book(self, user_id, book_id):


user = next((user for user in self._list_of_all_users if user._user_id␣
↪== user_id), None)

book = next((book for book in self._list_of_all_books if book._book_id␣


↪== book_id), None)

if user and book:


if user.U_return_book(book):
return f'{user._name} has succesfully returned "{book._title}".'
elif user.U_return_book(book) == False:
return f'{user._name} did not borrow "{book._title}".'
else:
return f'User or Book not found'

def display_books(self):
return "\n".join(str(book) for book in self._list_of_all_books)

3
def display_user_borrowed_books(self, user_id):
user = next((user for user in self._list_of_all_users if user._user_id␣
↪== user_id), None)

if user:
borrowed_books = "\n".join(f'- {book._title}' for book in user.
↪_list_of_borrowed_books)

return f"{user._name} has borrowed:\n{borrowed_books}"


else:
return "User not found."

#system processing - use mostly if/else

#giving the list of functions to choose then input

medthods = [
'1: Add books',
'2: Add users',
'3: Find book by title',
'4: Let an user borrow 1 book',
'5: Let an user return 1 book',
'6: Display all books in the system',
'7: Display user\'s borrowed books',
'8: Back to the homepage',
'0: Stop the system'
]

print('Welcome to Library system!')


print('Homepage')
for choice in medthods:
print(choice)

lib = Library()
#the true process:
flag = True
while flag:
try:
require = int(input('What do you want to do? '))
if require == 0:
raise IndexError

elif require == 1: #add book


print('Add books page.')
num_of_books_to_add = int(input('How many books do you want to add?␣
↪'))
for i in range(num_of_books_to_add):
book_id = input('Enter book ID: ')

4
title = input('Enter book\'s title: ')
author = input('Enter author: ')
copies = int(input('Enter available copies: '))

book = Book(book_id, title, author, copies)


lib.add_book(book)

print('Adding process completed!')

elif require == 2: #add user


print('Add users page.')
num_of_users_to_add = int(input('How many users do you want to add?␣
'))

for _ in range(num_of_users_to_add):
name = input('Enter user\'s name: ')
user_id = input('Enter user\'s ID: ')

user = User(user_id, name)


lib.add_user(user)

print('Adding process completed!')

elif require == 3: #find


print('Finding page.')
print('If you want to come back to homepage, enter 8.')
find = input('Enter book\'s title need to find: ')
while find != '8':
print('Search result:')
print(lib.find_book_by_title(find))
find = input('Enter book\'s title need to find: ')

print('Coming back to homepage...')

elif require == 4: #borrow


print('Borrow page.')
user_id = input('Enter user\'s ID: ')
book_id = input('Enter book ID: ')
print(lib.L_borrow_book(user_id, book_id))

elif require == 5: #return


print('Return page.')
user_id = input('Enter user\'s ID: ')
book_id = input('Enter book ID: ')
print(lib.L_return_book(user_id, book_id))

elif require == 6: #display books


print('Available books.')

5
print(lib.display_books())

elif require == 7: #user's borrow


user_id = input('Enter user\'s ID: ')
print(lib.display_user_borrowed_books(user_id))

else: #input not numbers or not in list


print('Please enter the medthods mentioned in the list.')
except IndexError: #(require = 0)
flag = False
print('System stopped.')
except:
print('Please enter number from 0 to 8 only.')
continue

Welcome to Library system!


Homepage
1: Add books
2: Add users
3: Find book by title
4: Let an user borrow 1 book
5: Let an user return 1 book
6: Display all books in the system
7: Display user's borrowed books
8: Back to the homepage
0: Stop the system
What do you want to do? 1
Add books page.
How many books do you want to add? 2
Enter book ID: B001
Enter book's title: Python
Enter author: Jane
Enter available copies: 3
"Python" has been succesfully added into the library.
Enter book ID: B002
Enter book's title: Data
Enter author: Smith
Enter available copies: 1
"Data" has been succesfully added into the library.
Adding process completed!
What do you want to do? 2
Add users page.
How many users do you want to add? 2
Enter user's name: Alice
Enter user's ID: U001
Alice has sucessfully registered.
Enter user's name: Jack

6
Enter user's ID: U002
Jack has sucessfully registered.
Adding process completed!
What do you want to do? 6
Available books.
Book ID: B001, Title: Python, Author: Jane, Copies: 3.
Book ID: B002, Title: Data, Author: Smith, Copies: 1.
What do you want to do? 4
Borrow page.
Enter user's ID: U001
Enter book ID: B001
Alice has successfully borrowed Python.
What do you want to do? 4
Borrow page.
Enter user's ID: U002
Enter book ID: B002
Jack has successfully borrowed Data.
What do you want to do? 4
Borrow page.
Enter user's ID: U001
Enter book ID: B002
Not enough copies of "Data" to borrow.
What do you want to do? 4
Borrow page.
Enter user's ID: ákdl:
Enter book ID: ápdo
Book or User not found.
What do you want to do? 5
Return page.
Enter user's ID: U001
Enter book ID: B001
Alice has succesfully returned "Python".
What do you want to do? 3
Finding page.
If you want to come back to homepage, enter 8.
Enter book's title need to find: pYthOn
Search result:
Book ID: B001, Title: Python, Author: Jane, Copies: 2.
Enter book's title need to find: 8
Coming back to homepage…
What do you want to do? 7
Enter user's ID: U001
Alice has borrowed:

What do you want to do? 7


Enter user's ID: U002
Jack has borrowed:
- Data

7
What do you want to do? 0
System stopped.

You might also like