Introduction :
The Library Management App is a web-based application designed to help users manage a collection of books. Built using HTML, CSS, and JavaScript, the app allows users to add, view, and search for books in their library. It leverages Bootstrap for styling and responsive design, ensuring a smooth user experience across different devices.
This App is a comprehensive, web-based application designed to streamline the management of a personal or small-scale library. Developed using a combination of HTML, CSS, and JavaScript, the app provides an intuitive interface for users to efficiently add, view, and search for books. The app leverages the Bootstrap framework for a polished and responsive design, ensuring that it functions seamlessly across various devices, including desktops, tablets, and smartphones.
This project aims to simplify the task of cataloging books by allowing users to input essential details such as the book’s name, author, and genre. The genres currently supported include Fiction, Computer Programming, and Cooking, reflecting common categories that might be present in a diverse library collection. By using local storage, the app retains book information even after the browser is closed, providing a persistent and reliable experience for users.
The Library Management App stands out due to its clean and user-friendly interface, enhanced by Bootstrap’s styling capabilities. The use of JavaScript for dynamic content manipulation makes the app interactive and engaging, providing immediate feedback to user actions. For instance, upon adding a book, the application instantly updates the displayed list of books and provides confirmation messages, enhancing the user experience.
Furthermore, the app showcases both ES5 and ES6 JavaScript syntax, offering a learning opportunity for developers to understand and compare different coding standards and practices. The ES6 version employs modern JavaScript features such as classes and template literals, demonstrating more efficient and readable code.
Overall, the Library Management App is an ideal project for those interested in web development, offering practical insights into integrating front-end technologies and applying JavaScript to create dynamic web applications. Whether used for managing a personal book collection or as a foundational project for learning web development, this app serves as a valuable and functional tool.
Explanation :
HTML Structure
The HTML code is the foundational layer of the Library Management App, establishing the layout and primary elements that users interact with. Here’s a breakdown of its key components:
Head Section:
- Meta Tags: These ensure proper character encoding and responsive behavior across devices.
- Bootstrap CSS: The
link
tag includes Bootstrap’s CSS, which provides pre-designed styles and a responsive grid system.
Body Section:
- Navigation Bar:
- Navbar: A dark-themed navbar with a brand label (“Book Library”) and a search form for user convenience.
- Toggler Button: Ensures the navbar can collapse on smaller screens, enhancing mobile usability.
- Message Section: An empty
div
with the IDmessage
serves as a placeholder for displaying success or error notifications. - Main Container:
- Form Section: Contains a form (
libraryForm
) with input fields for book name, author, and type. This form is central to adding new books to the library. - Table Section: Displays the list of books in a structured table format, making it easy for users to see their added books.
- Form Section: Contains a form (
- Navigation Bar:
CSS (Bootstrap)
Bootstrap’s utility classes are employed extensively to ensure the app is visually appealing and user-friendly:
- Navbar: Classes like
navbar
,navbar-dark
, andbg-dark
create a visually distinct and responsive navigation bar. - Form Controls: Classes such as
form-group
,form-control
, andbtn
ensure that form elements are properly aligned and styled. - Table: The
table
andtable-striped
classes provide a clean, striped table layout for displaying the list of books.
JavaScript Logic
JavaScript brings interactivity to the app, making it possible to add, validate, and display books dynamically. Two versions of the script illustrate different approaches:
ES5 Version (index.js)
Book Constructor:
- Function: Defines a
Book
object with propertiesname
,author
, andtype
.
- Function: Defines a
Display Constructor:
- Function: Manages the UI-related functionalities.
Display Prototype Methods:
add(book)
: Injects a new book entry into the HTML table. This method dynamically constructs a table row using the book’s details and appends it to thetableBody
.clear()
: Resets the form fields after a book is added.validate(book)
: Checks that both the book’s name and author are at least two characters long, ensuring data integrity.show(type, displayMessage)
: Displays messages (success or error) to the user. This method constructs an alert message dynamically and removes it after a short delay.
Event Listener:
- libraryFormSubmit(e): Attached to the form’s submit event. It gathers input values, validates them, and updates the UI. If validation fails, it shows an error message.
ES6 Version (indexes6.js)
Book Class:
- Class: Represents a book with a constructor that initializes
name
,author
, andtype
properties.
- Class: Represents a book with a constructor that initializes
Display Class:
- Class: Contains methods to handle UI updates.
add(book)
: Similar to the ES5 version but utilizes template literals for cleaner string interpolation.clear()
: Resets the form fields.validate(book)
: Ensures that book name and author are at least two characters long.show(type, displayMessage)
: Displays messages with enhanced readability and modern JavaScript syntax.
Event Listener:
- libraryFormSubmit(e): Handles form submission, similar to the ES5 version, but implemented with ES6 class methods.
Purpose of JavaScript Functions
Book Class (Constructor Function in ES5):
- Purpose: Encapsulates book details into a single object, simplifying data management.
Display Class (Constructor Function in ES5):
- Purpose: Handles all tasks related to updating the UI, ensuring separation of concerns between data and presentation logic.
add(book):
- Purpose: Adds a book entry to the table, updating the display dynamically.
clear():
- Purpose: Clears the form inputs, preparing the form for the next book entry.
validate(book):
- Purpose: Checks the validity of book data, preventing incomplete or erroneous entries from being added.
show(type, displayMessage):
- Purpose: Provides feedback to users, indicating whether their action (adding a book) was successful or if there was an error.
libraryFormSubmit(e):
- Purpose: Orchestrates the process of collecting form data, validating it, updating the display, and showing feedback messages, ensuring a smooth user experience.
Conclusion
The Library Management App is a robust and user-friendly tool designed to help users catalog and manage their books. By integrating HTML, CSS, and JavaScript, it provides a seamless experience for adding and viewing books. The use of Bootstrap enhances its visual appeal and responsiveness, while the dual implementation in ES5 and ES6 JavaScript serves as a valuable learning resource for developers. This app not only facilitates library management but also exemplifies best practices in web development.
SOURCE CODE:
HTML (index.html)
Welcome to Library
Manage Library
Your books
Name
Author
Type
JavaScript (index.js)
console.log("This is index.js");
// Todos"
// 1. Store all the data to the localStorage
// 2. Give another column as an option to delete the book
// 3. Add a scroll bar to the view
// Constructor
function Book(name, author, type) {
this.name = name;
this.author = author;
this.type = type;
}
// Display Constructor
function Display() {
}
// Add methods to display prototype
Display.prototype.add = function (book) {
console.log("Adding to UI");
tableBody = document.getElementById('tableBody');
let uiString = `
${book.name}
${book.author}
${book.type}
`;
tableBody.innerHTML += uiString;
}
// Implement the clear function
Display.prototype.clear = function () {
let libraryForm = document.getElementById('libraryForm');
libraryForm.reset();
}
// Implement the validate function
Display.prototype.validate = function (book) {
if (book.name.length < 2 || book.author.length < 2) {
return false
}
else {
return true;
}
}
Display.prototype.show = function (type, displayMessage) {
let message = document.getElementById('message');
message.innerHTML = `
Messge: ${displayMessage}
`;
setTimeout(function () {
message.innerHTML = ''
}, 2000);
}
// Add submit event listener to libraryForm
let libraryForm = document.getElementById('libraryForm');
libraryForm.addEventListener('submit', libraryFormSubmit);
function libraryFormSubmit(e) {
console.log('YOu have submitted library form');
let name = document.getElementById('bookName').value;
let author = document.getElementById('author').value;
let type;
let fiction = document.getElementById('fiction');
let programming = document.getElementById('programming');
let cooking = document.getElementById('cooking');
if (fiction.checked) {
type = fiction.value;
}
else if (programming.checked) {
type = programming.value;
}
else if (cooking.checked) {
type = cooking.value;
}
let book = new Book(name, author, type);
console.log(book);
let display = new Display();
if (display.validate(book)) {
display.add(book);
display.clear();
display.show('success', 'Your book has been successfully added')
}
else {
// Show error to the user
display.show('danger', 'Sorry you cannot add this book');
}
e.preventDefault();
}
JavaScript (indexes6.js)
console.log('This is ES6 version of Project 2');
class Book {
constructor(name, author, type) {
this.name = name;
this.author = author;
this.type = type;
}
}
class Display {
add(book) {
console.log("Adding to UI");
let tableBody = document.getElementById('tableBody');
let uiString = `
${book.name}
${book.author}
${book.type}
`;
tableBody.innerHTML += uiString;
}
clear() {
let libraryForm = document.getElementById('libraryForm');
libraryForm.reset();
}
validate(book) {
if (book.name.length < 2 || book.author.length < 2) {
return false
}
else {
return true;
}
}
show(type, displayMessage) {
let message = document.getElementById('message');
let boldText;
if(type==='success'){
boldText = 'Success';
}
else{
boldText = 'Error!';
}
message.innerHTML = `
${boldText}: ${displayMessage}
`;
setTimeout(function () {
message.innerHTML = ''
}, 5000);
}
}
// Add submit event listener to libraryForm
let libraryForm = document.getElementById('libraryForm');
libraryForm.addEventListener('submit', libraryFormSubmit);
function libraryFormSubmit(e) {
console.log('YOu have submitted library form');
let name = document.getElementById('bookName').value;
let author = document.getElementById('author').value;
let type;
let fiction = document.getElementById('fiction');
let programming = document.getElementById('programming');
let cooking = document.getElementById('cooking');
if (fiction.checked) {
type = fiction.value;
}
else if (programming.checked) {
type = programming.value;
}
else if (cooking.checked) {
type = cooking.value;
}
let book = new Book(name, author, type);
console.log(book);
let display = new Display();
if (display.validate(book)) {
display.add(book);
display.clear();
display.show('success', 'Your book has been successfully added')
}
else {
// Show error to the user
display.show('danger', 'Sorry you cannot add this book');
}
e.preventDefault();
}