In Lab 3, you will enhance a library management system by incorporating data persistence using JSON. This critical upgrade will require understanding and modifying the existing classes - Book, Person, and Library - housed in separate modules. Implement JSON serialization and deserialization techniques to ensure that all library data, including books, members, and loan information, is maintained across sessions.
Ensure that you are signed into your GitHub account
Join the GitHub Classroom and accept this assignment: Project 14 - Library System
Clone the repository to your computer using GitHub Desktop or open it online using Codespaces
The lab3.py module serves as the entry point to the Library Management System. It contains the main() function, which orchestrates user interactions with the system through a command-line interface. Users can perform a variety of operations such as adding books and members, listing and managing loans, and exiting the program. This module ties together the functionalities defined in separate classes - Book, Person, and Library.
This will be the file you need to execute to run the program; however, you shouldn't need to change anything in this file.
The book.py file contains the Book class, a fundamental component of the library management system. This class defines the structure and attributes of a book, including its title, author, ISBN, and loan status.
The Book class provides methods for basic interactions such as loaning and returning books, making it essential for the system's operations. No modifications will be required in this file.
The person.py module contains the Person class, which represents individuals who are members of the library. Each Person has attributes for their name and a unique member ID, and they maintain a list of books they have currently loaned out.
This class is crucial for managing membership and tracking which books are on loan to which members. No changes will need to be made to this module either.
The library.py file contains the Library class, which serves as the central management hub for the library system. This class is responsible for overseeing all library operations such as adding books and members, managing loans, and processing returns.
It maintains comprehensive lists of books and registered members, and provides methods to list, loan, and retrieve books. The Library class encapsulates the core functionalities required to run the library, ensuring that all interactions are managed through well-defined interfaces.
Initialization (__init__ method)
Start the library system by ensuring that both books and members are loaded from their respective JSON files when the library instance is created. This setup mimics a real-world application where you would expect to retrieve existing data upon startup.
Saving Data (save_books and save_members methods)
Implement the save_books method to serialize the _books list into JSON format and write it to 'books.json'.
Each book object will need to be converted to a dictionary.
Recall how to use a list comprehension and the __dict__ attribute or a custom method in the Book class to prepare each object for serialization.
Loading Data (load_books and load_members methods)
For load_books, read 'books.json' and convert each dictionary back into a Book object.
In load_members, convert each entry from 'members.json' back into a Person object.
Remember to handle cases where the file might not exist when the library is first set up.
Adding and Updating Entries
Every time a new book or member is added, or an existing entry is updated (like loaning or returning a book), ensure the data is immediately persisted to file. This mimics saving changes in real-time in a database.
Open lab3.py
Complete the Library System by implementing data persistence with JSON:
Initialize Data Loading: Load books and members within the __init__ method to ensure the library is populated with existing data at startup.
Persist Books Data: Implement save_books to serialize and save the current list of books to 'books.json' after any modification.
Persist Members Data: Implement save_members to serialize and save the current list of library members to 'members.json' after any updates.
Load Books from JSON: Complete load_books to deserialize 'books.json' into book objects and populate the library's book list.
Load Members from JSON: Complete load_members to deserialize 'members.json' into member objects and update the library's member list.
Commit and push your code