Python Programming - Classes, Attributes, Methods, and Objects. ⚑🎩
Introduction
The purpose of
this program is to put on a simple library management system that allows users
to manage books, including functions such as borrowing and returning books, as
well as adding new books to the library. This system is designed to showcase
basic functionalities through object-oriented programming principles, in that
way making the code flexible, reusable, and easier to understand. However, the
system consists of two main classes: Book and Library. The Book class is
responsible for encapsulating the characteristics of a book, such as its title,
author, International Standard Book Numbers (ISBN), and availability status
(whether the book is available or not). Meanwhile, the Library class manages a
collection of Book instances (objects), allowing users to interact with them
(add, borrow, return, and list available books).
Object-Oriented
Design Explanation
There are two
classes created, the Book Class and the Library Class. However, the Book class
models a book with attributes such as title, author, ISBN, and availability
status. On the other hand, this class (book class) contains methods of
borrowing and returning the book, and also for displaying information. The
other class created was the Library Class. This Library class models a library
which contains multiple books. However, it provides methods to add books to the
library, list available books, as well as handle borrowing and returning of
books through their ISBN. Moreover, under the Book Class, the
attributes are the title, author, ISBN, and availability status of the book;
therefore, it is designed in a way that it uses the constructor (__int__) to
initialize or assign the attributes of the class with specific values or
properties. On the other hand, the Methods (functions the object can perform)
are borrow_book (): which marks the book as borrowed if it is available,
return_book (): which indicates the book ass returned if it was previously
borrowed, display_info (): which displays or print information about the book,
including its current status. Furthermore, under the Library
Class, the attributes are the name of the library and a list that contains all
book instances (objects) in the library; therefore, it was designed I such a
way that the constructor (__int__) initialize or assign the attributes of the
class with specific values or properties. On the other hand, the Methods
(things the object can execute) are add_book(book): which adds a book instance
to the library, list_available_books(): which lists all books that are
currently available in the library, borrow_book(isbn): which allows borrowing
of a specific book identified by its ISBN, and return_book(isbn): which allows
returning of a specific book by its ISBN. Moreover, an object of the
Book class is instantiated or represented by
providing the title, author, and ISBN of the book, whereas the objects of the library
class are instantiated or represented by indicating the name of the library.
For example, consult the screenshot below.
book1 = Book("In The Pursuit of Wisdom: The
Principal Thing", "Alex Uwajeh", "9788892513891")
my_library = Library("Google
Books")
Implementation
Overview
The flow of the
program outlines that at first, the program creates instances (objects) of
books and a library. It adds the book instances (objects) to the library's
collection and demonstrates the fundamental operations (methods), which are: borrowing,
returning, and listing available books. Moreover, the four (4) main code
snippets are: (i). Creating Book Instances: This line demonstrates how a new
book object is created with specific attributes. For example, consult the
screenshot below.
book1 = Book("In
The Pursuit of Wisdom: The Principal Thing", "Alex
Uwajeh", "9788892513891")
(ii). Borrowing
a Book: This method checks if the book is available and marks it as borrowed if
true. For example, consult the screenshot below
book1.borrow_book()
(iii). Returning
a Book: This method allows the book to be returned, marking it as available
again. For example, consult the screenshot below.
book1.return_book()
(iv). Listing
Available Books: This method repeats over the library's book collection and
prints the details of all available books in the library.
my_library.list_available_books()
On the contrary,
the borrow method searches the book via ISBN and calls the borrow_book() method
of the Book Class, the return method searches for the book via ISBN and calls
the return_book() method of the Book class, the add method adding a new book
instance to the books list of the Library class, and the list function filters
the books list based on availability and displays each book’s information using
the display_info() method.
Sample
Outputs
Furthermore, the
sample output portrays the borrowing and the returning of a book, as shown
below.
On the other
hand, the sample output also shows the listing of available books in the
library, as shown below.
Challenges
and Reflections
One of the
challenges encountered was the “leaving a space after the assignment of
attributes and each method.” I was thinking, what input will I type in my code
to leave or give a space after certain methods and attributes? Therefore, a
voice whispered in my ear and said, “See on top of the screen of the VS Code,
there is an emoji that resembles an animal (which is the Toggle Chat), navigate to it and
ask your question.” Hence, I tapped it and asked this question: “How will I
leave a space after a certain code?” The feedback was “print ().” Therefore, I
used this print () command in my code to give a space after the attributes and
the methods in both classes (book and library classes). Moreover, in this
assignment, I learned new skills about giving or leaving a space after certain
sentences (as described earlier), I learned about how to create objects in a
given class, and I also learned the importance of thinking in terms of objects
and how encapsulation can improve code structure. However, there is one thing I
would consider for future improvement is “adding a graphical user interface
(GUI) to enhance user interaction.
Conclusion
In conclusion,
this library management system or program shows why object-oriented programming
is useful. It keeps data safe, makes changes easy, and lets code be reused. The
library management system was structured using object-oriented programming
principles helps add new features without making things confusing. Through
this, I learned how to use classes and objects to organize code for real-world
problems.
References
Lutz, M. (2013). Learning
Python: Powerful Object-Oriented Programming. United States: O'Reilly
Media. Retrieved from: https://www.google.com.au/books/edition/Learning_Python/ePyeNz2Eoy8C?hl=en&gbpv=1
O. L. Madsen and
B. Moller-Pedersen. 1989. Virtual classes: a powerful mechanism in
object-oriented programming. In Conference proceedings on Object-oriented
programming systems, languages and applications (OOPSLA '89). Association
for Computing Machinery, New York, NY, USA, 397–406. Retrieved
from: https://dl.acm.org/doi/pdf/10.1145/74877.74919
Comments
Post a Comment