📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (176K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Step 1: Setup Your Project
Create Packages and Classes: Inside the project, create a package net.javaguides.lms. Within this package, create three classes: LibraryManager, Book, and Main.
Step 2: Implement the Book Class
Start with the Book class in Book.java. This class represents a book in the library with attributes like id, title, author, and isBorrowed. Implement getters, setters, and a toString method for printing book details.package net.javaguides.lms;
public class Book {
private int id;
private String title;
private String author;
private boolean isBorrowed;
public Book(int id, String title, String author) {
this.id = id;
this.title = title;
this.author = author;
this.isBorrowed = false;
}
// Getters and Setters
public int getId() { return id; }
public void setId(int id) { this.id = id; }
public String getTitle() { return title; }
public void setTitle(String title) { this.title = title; }
public String getAuthor() { return author; }
public void setAuthor(String author) { this.author = author; }
public boolean isBorrowed() { return isBorrowed; }
public void setBorrowed(boolean borrowed) { isBorrowed = borrowed; }
@Override
public String toString() {
return "Book{" +
"id=" + id +
", title='" + title + '\'' +
", author='" + author + '\'' +
", isBorrowed=" + isBorrowed +
'}';
}
}
Step 3: Develop the LibraryManager Class
package net.javaguides.lms;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;
public class LibraryManager {
private List<Book> books = new ArrayList<>();
public void addBook(Book book) {
books.add(book);
System.out.println("Book added successfully!");
}
public void updateBook(int id, String title, String author) {
books.stream()
.filter(book -> book.getId() == id)
.findFirst()
.ifPresent(book -> {
book.setTitle(title);
book.setAuthor(author);
System.out.println("Book updated successfully!");
});
}
public void deleteBook(int id) {
if (books.removeIf(book -> book.getId() == id)) {
System.out.println("Book deleted successfully!");
} else {
System.out.println("Book not found!");
}
}
public void listBooks() {
books.forEach(System.out::println);
}
public void searchBooks(String query) {
List<Book> foundBooks = books.stream()
.filter(book -> book.getTitle().toLowerCase().contains(query.toLowerCase()) ||
book.getAuthor().toLowerCase().contains(query.toLowerCase()))
.collect(Collectors.toList());
if (foundBooks.isEmpty()) {
System.out.println("No books found matching the query.");
} else {
foundBooks.forEach(System.out::println);
}
}
public void checkOutBook(int id) {
books.stream()
.filter(book -> book.getId() == id && !book.isBorrowed())
.findFirst()
.ifPresentOrElse(book -> {
book.setBorrowed(true);
System.out.println("Book checked out successfully!");
}, () -> System.out.println("Book is not available."));
}
public void checkInBook(int id) {
books.stream()
.filter(book -> book.getId() == id && book.isBorrowed())
.findFirst()
.ifPresentOrElse(book -> {
book.setBorrowed(false);
System.out.println("Book checked in successfully!");
}, () -> System.out.println("Book was not checked out."));
}
// Method to input book details
private Book inputBookDetails(Scanner scanner) {
System.out.print("Enter Book ID: ");
int id = scanner.nextInt();
scanner.nextLine(); // consume newline
System.out.print("Enter Book Title: ");
String title = scanner.nextLine();
System.out.print("Enter Book Author: ");
String author = scanner.nextLine();
return new Book(id, title, author);
}
// Method to start the library management system
public void start() {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("\nLibrary Management System");
System.out.println("1. Add Book");
System.out.println("2. Update Book");
System.out.println("3. Delete Book");
System.out.println("4. List All Books");
System.out.println("5. Search Books");
System.out.println("6. Check Out Book");
System.out.println("7. Check In Book");
System.out.println("8. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
scanner.nextLine(); // Fix for Scanner bug
switch (choice) {
case 1:
addBook(inputBookDetails(scanner));
break;
case 2:
System.out.print("Enter Book ID to update: ");
int id = scanner.nextInt();
scanner.nextLine(); // Consume newline
Book book = inputBookDetails(scanner);
updateBook(id, book.getTitle(), book.getAuthor());
break;
case 3:
System.out.print("Enter Book ID to delete: ");
id = scanner.nextInt();
deleteBook(id);
break;
case 4:
listBooks();
break;
case 5:
System.out.print("Enter search query (title or author): ");
String query = scanner.nextLine();
searchBooks(query);
break;
case 6:
System.out.print("Enter Book ID to check out: ");
id = scanner.nextInt();
checkOutBook(id);
break;
case 7:
System.out.print("Enter Book ID to check in: ");
id = scanner.nextInt();
checkInBook(id);
break;
case 8:
System.out.println("Exiting...");
return;
default:
System.out.println("Invalid choice, please enter a number between 1 and 8.");
break;
}
}
}
}
addBook(Book book): Adds a new book to the library. Step 4: Build the Main Class
package net.javaguides.lms;
public class Main {
public static void main(String[] args) {
LibraryManager manager = new LibraryManager();
manager.start();
}
}
Step 5: Running the Library Management System
Library Management System
1. Add Book
2. Update Book
3. Delete Book
4. List All Books
5. Search Books
6. Check Out Book
7. Check In Book
8. Exit
Enter your choice: 1
Enter Book ID: 101
Enter Book Title: Effective Java
Enter Book Author: Joshua Bloch
Book added successfully!
Library Management System
1. Add Book
2. Update Book
3. Delete Book
4. List All Books
5. Search Books
6. Check Out Book
7. Check In Book
8. Exit
Enter your choice: 1
Enter Book ID: 102
Enter Book Title: Java Concurrency in Practice
Enter Book Author: Brian Goetz
Book added successfully!
Library Management System
1. Add Book
2. Update Book
3. Delete Book
4. List All Books
5. Search Books
6. Check Out Book
7. Check In Book
8. Exit
Enter your choice: 4
Book{id=101, title='Effective Java', author='Joshua Bloch', isBorrowed=false}
Book{id=102, title='Java Concurrency in Practice', author='Brian Goetz', isBorrowed=false}
Library Management System
1. Add Book
2. Update Book
3. Delete Book
4. List All Books
5. Search Books
6. Check Out Book
7. Check In Book
8. Exit
Enter your choice: 5
Enter search query (title or author): java
Book{id=101, title='Effective Java', author='Joshua Bloch', isBorrowed=false}
Book{id=102, title='Java Concurrency in Practice', author='Brian Goetz', isBorrowed=false}
Library Management System
1. Add Book
2. Update Book
3. Delete Book
4. List All Books
5. Search Books
6. Check Out Book
7. Check In Book
8. Exit
Enter your choice: 6
Enter Book ID to check out: 101
Book checked out successfully!
Library Management System
1. Add Book
2. Update Book
3. Delete Book
4. List All Books
5. Search Books
6. Check Out Book
7. Check In Book
8. Exit
Enter your choice: 7
Enter Book ID to check in: 101
Book checked in successfully!
Library Management System
1. Add Book
2. Update Book
3. Delete Book
4. List All Books
5. Search Books
6. Check Out Book
7. Check In Book
8. Exit
Enter your choice: 8
Exiting...
Process finished with exit code 0
Comments
Post a Comment
Leave Comment