Python Assignment: using Class and OOP in python
Python Assignment: using Class and OOP in python
Assignment 3 - Programming
December 29, 2024
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 __str__(self):
return f'Book ID: {self._book_id}, Title: {self._title}, Author: {self.
↪_author}, Copies: {self._B_copies}.'
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
class Library:
'''
this is where i do the functions, this will affect the data of 2 classes␣
↪above
2
def __init__(self):
self._list_of_all_books = []
self._list_of_all_users = []
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)
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'
]
lib = Library()
#the true process:
flag = True
while flag:
try:
require = int(input('What do you want to do? '))
if require == 0:
raise IndexError
4
title = input('Enter book\'s title: ')
author = input('Enter author: ')
copies = int(input('Enter available copies: '))
for _ in range(num_of_users_to_add):
name = input('Enter user\'s name: ')
user_id = input('Enter user\'s ID: ')
5
print(lib.display_books())
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:
7
What do you want to do? 0
System stopped.