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

OOP Lab Umang

Uploaded by

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

OOP Lab Umang

Uploaded by

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

Name: Umang Banerjee

Keshav Agarwal

Semester: 3rd
3nd Semester
Semester

2320801067
Roll No: 2330802007

Subject: Object Oriented


Programming

Course : B.Tech CSE & AI/ML)


B.Tech(CSE

Mini-Project: Library
Management System
STAREX UNIVERSITY
GURUGRAM

SCHOOL OF COMPUTER SCIENCE


B.Tech Computer Science & Engineering
Mini-Project: Library Management
System
This Library Management System project is designed for educational
and demonstration purposes to showcase the implementation of core
Object-Oriented Programming (OOP) principles, such as
encapsulation, inheritance, and polymorphism. While it effectively
simulates a basic library system's functionalities, including adding,
issuing, and returning books, it is not intended for deployment in
real-world library environments without further modifications and
enhancements.

This project uses a simplified data management approach with


ArrayList to store book and user records, which is sufficient for
learning and small-scale usage. However, for production-level
systems, more robust data handling mechanisms, such as database
integration, user authentication, error handling, and scalability, would
be required. Additionally, the system assumes trust among users and
administrators and does not implement advanced features like user
roles, fine calculations for overdue books, or detailed reporting
capabilities.

Users should view this project as a stepping stone towards


understanding Java programming and building more complex
applications in the future. It is encouraged to experiment further with
this project by adding new features, improving the UI, or integrating
backend technologies for an enriched learning experience.
Aim:
Build a Library Management System using Java and implement core OOP principles like
encapsulation, inheritance, and polymorphism.

Problem statement:

To design a system that manages library records, including adding, issuing, and returning
books.

Explanation :
In a Library Management System, the main tasks include:

1. Adding Books: A librarian can add books to the system.


2. Issuing Books: Students can borrow books if they are available.
3. Returning Books: After reading, books are returned, and their availability is updated.
4. Display Available Books: The system displays all books with their availability status.

Solution :

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Scanner;

class Book {
String title;
String author;
boolean isAvailable;
String issuedTo;

Book(String title, String author) {


this.title = title;
this.author = author;
this.isAvailable = true;
this.issuedTo = null;
}
}

class User {
String name;
ArrayList<IssuedBook> issuedBooks;

User(String name) {
this.name = name;
this.issuedBooks = new ArrayList<>();
}

// Add a book to the user's issued books list


public void addIssuedBook(Book book, LocalDate issueDate) {
issuedBooks.add(new IssuedBook(book, issueDate));
}

// Remove a book from the user's issued books list


public void removeIssuedBook(Book book) {
issuedBooks.removeIf(issuedBook -> issuedBook.book.equals(book));
}

// Display issued books


public void displayIssuedBooks() {
if (issuedBooks.isEmpty()) {
System.out.println(name + " has not issued any books.");
} else {
System.out.println(name + "'s Issued Books:");
for (int i = 0; i < issuedBooks.size(); i++) {
IssuedBook issuedBook = issuedBooks.get(i);
System.out.println((i + 1) + ". " + issuedBook.book.title + " by " +
issuedBook.book.author +
" (Issued on: " + issuedBook.issueDate + ")");
}
}
}
}

class IssuedBook {
Book book;
LocalDate issueDate;
LocalDate dueDate;

IssuedBook(Book book, LocalDate issueDate) {


this.book = book;
this.issueDate = issueDate;
this.dueDate = issueDate.plusDays(7);
}

// Return the due date


public LocalDate getDueDate() {
return dueDate;
}

// Check if the book is overdue


public boolean isOverdue() {
return LocalDate.now().isAfter(dueDate);
}
}

class Library {
private ArrayList<Book> books = new ArrayList<>();
private ArrayList<User> users = new ArrayList<>();

// Add a new book to the library


public void addBook(String title, String author) {
for (Book book : books) {
if (book.title.equalsIgnoreCase(title)) {
System.out.println("This book is already in the library.");
return;
}
}
books.add(new Book(title, author));
System.out.println("Book added successfully: " + title + " by " + author);
}

// Add a new user to the library system


public User addUser(String name) {
for (User user : users) {
if (user.name.equalsIgnoreCase(name)) {
return user;
}
}
User newUser = new User(name);
users.add(newUser);
return newUser;
}

// Issue a book to a user


public void issueBook(String title, User user) {
boolean found = false;
for (Book book : books) {
if (book.title.equalsIgnoreCase(title)) {
found = true;
if (book.isAvailable) {
book.isAvailable = false;
book.issuedTo = user.name;
LocalDate issueDate = LocalDate.now();
user.addIssuedBook(book, issueDate);
System.out.println("Book issued: " + title + " to " + user.name + " on " +
issueDate);
} else {
System.out.println("The book is currently issued by " + book.issuedTo + ".");
}
break;
}
}
if (!found) {
System.out.println("Book not found in the library.");
}
}

// Return a book from a user


public void returnBook(User user) {
user.displayIssuedBooks();
if (user.issuedBooks.isEmpty()) {
return;
}

Scanner scanner = new Scanner(System.in);


System.out.print("Enter the title of the book to return: ");
String bookTitle = scanner.nextLine();

boolean found = false;


for (IssuedBook issuedBook : user.issuedBooks) {
if (issuedBook.book.title.equalsIgnoreCase(bookTitle)) {
found = true;
Book book = issuedBook.book;
book.isAvailable = true;
book.issuedTo = null;
user.removeIssuedBook(book);
LocalDate returnDate = LocalDate.now();
System.out.println("Book returned: " + book.title + " by " + user.name + " on " +
returnDate);
// Check if the book is overdue
if (issuedBook.isOverdue()) {
System.out.println("This book is overdue! Please return books on time.");
}
break;
}
}

if (!found) {
System.out.println("You have not issued a book with that title.");
}
}

// Display available books


public void displayBooks() {
if (books.isEmpty()) {
System.out.println("No books in the library.");
} else {
System.out.println("\nLibrary Books:");
for (Book book : books) {
String status = book.isAvailable ? "Available" : "Issued by " + book.issuedTo;
System.out.println(book.title + " by " + book.author + " (" + status + ")");
}
}
}

// Display the list of books issued by a user


public void displayUserIssuedBooks(User user) {
user.displayIssuedBooks();
}
}

public class LibraryManagementSystem {


public static void main(String[] args) {
Library library = new Library();
Scanner scanner = new Scanner(System.in);

while (true) {
System.out.println("\n1. Add Book\n2. Issue Book\n3. Return Book\n4. Display
Books\n5. View User's Issued Books\n6. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
scanner.nextLine();

switch (choice) {
case 1:
System.out.print("Enter book title: ");
String title = scanner.nextLine();
System.out.print("Enter book author: ");
String author = scanner.nextLine();
library.addBook(title, author);
break;

case 2:
System.out.print("Enter your name: ");
String userName = scanner.nextLine();
User user = library.addUser(userName);
if (user != null) {
System.out.print("Enter book title to issue: ");
title = scanner.nextLine();
library.issueBook(title, user);
}
break;

case 3:
System.out.print("Enter your name: ");
userName = scanner.nextLine();
user = library.addUser(userName);
if (user != null) {
library.returnBook(user);
}
break;

case 4:
library.displayBooks();
break;

case 5:
System.out.print("Enter your name: ");
userName = scanner.nextLine();
user = library.addUser(userName);
if (user != null) {
library.displayUserIssuedBooks(user);
}
break;

case 6:
System.out.println("Exiting... Thank you for using the library management
system.");
scanner.close();
return;

default:
System.out.println("Invalid choice. Please try again.");
}
}
}
}

Output: For choice 1: Add Book

For choice 2: Issue Book

For choice 3: Return Book

For choice 4: Display All the Books


For choice 5: Display All the Books Issued by a particular User

For choice 6: Exit

Code Explanation :

1. Class Design:

Book: Contains attributes like title, author, isAvailable, and issuedTo. This
class represents a book in the library.
User: Represents a user of the library (a student). This class contains methods
to add and remove issued books and display issued books.
IssuedBook: Represents a book that has been issued to a user, including the
issueDate and dueDate.
Library: Manages all the books and users. This class handles adding books,
issuing books to users, returning books, and displaying books.
LibraryManagementSystem: The main class that implements the
command-line interface for the system.

2. Key Features:

Add Books: Adds new entries to the library system.


Issue Books: Marks books as issued if available.
Return Books: Changes the availability of issued books.
Display Available Books: Lists all books with their availability status.
View User's Issued Books: Displays books issued by a specific user.

3. Data Structure:

An ArrayList is used for storing book records dynamically. Similarly, users


and their issued books are managed using ArrayList.
Thank You!

We sincerely thank you for exploring the Library Management


System project. Your interest and time invested in understanding and
working with this project mean a lot to us. This system was built with
the aim of simplifying the process of managing library records while
demonstrating the principles of Object-Oriented Programming (OOP)
in Java.

You might also like