🎓 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 (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
In this tutorial, we will build a simple Library Management System project using Core Java. This project demonstrates the basic functionalities such as adding books, displaying all the books, displaying book details, deleting books, and borrowing books. In this project, we use the in-memory object to store the book objects (we don't use a database).
Please note that this is a simple project, and a real-world library management system would involve more complex features and database integration.
1. Defining the Book Class
Firstly, we'll define our core entity, the Book class, which represents individual books.
public class Book {
private int isbn;
private String title;
private String author;
public Book(int isbn, String title, String author) {
this.isbn = isbn;
this.title = title;
this.author = author;
}
// Getters and setters
public int getIsbn() {
return isbn;
}
public void setIsbn(int isbn) {
this.isbn = isbn;
}
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;
}
@Override
public String toString() {
return "ISBN: " + isbn + ", Title: " + title + ", Author: " + author;
}
}2. Implementing the LibraryManagementSystem Class
import java.util.ArrayList;
import java.util.List;
public class LibraryManagementSystem {
private List<Book> books = new ArrayList<>();
// Add a book
public void addBook(Book book) {
books.add(book);
}
// View all books
public void viewBooks() {
books.forEach(System.out::println);
}
// Update a book's details
public void updateBook(int isbn, String newTitle, String newAuthor) {
for (Book b : books) {
if (b.getIsbn() == isbn) {
b.setTitle(newTitle);
b.setAuthor(newAuthor);
break;
}
}
}
// Delete a book by ISBN
public void deleteBook(int isbn) {
books.removeIf(b -> b.getIsbn() == isbn);
}
// Search for a book by title
public Book searchBookByTitle(String title) {
for (Book b : books) {
if (b.getTitle().equalsIgnoreCase(title)) {
return b;
}
}
return null;
}
}This class manages the collection of books. It allows adding, viewing, updating, deleting, and searching for books in the library.3. Main Execution
public class Main {
public static void main(String[] args) {
LibraryManagementSystem lms = new LibraryManagementSystem();
// Add new books
lms.addBook(new Book(1010, "Learn Java", "John Doe"));
lms.addBook(new Book(1011, "Learn Advanced Java", "Jane Doe"));
// Display all books
System.out.println("Library Books:");
lms.viewBooks();
// Update the title of "Learn Advanced Java"
lms.updateBook(1011, "Learn Java: Advanced Topics", "Jane Doe");
// Search for a book by title
System.out.println("\nSearching for 'Learn Java':");
Book foundBook = lms.searchBookByTitle("Learn Java");
System.out.println(foundBook != null ? foundBook : "Book not found.");
// Delete the "Learn Java" book
lms.deleteBook(1010);
System.out.println("\nAfter deleting 'Learn Java':");
lms.viewBooks();
}
}This class demonstrates the working of our Library Management System, from adding new books to searching and removing them.
Output:
Library Books:
ISBN: 1010, Title: Learn Java, Author: John Doe
ISBN: 1011, Title: Learn Advanced Java, Author: Jane Doe
Searching for 'Learn Java':
ISBN: 1010, Title: Learn Java, Author: John Doe
After deleting 'Learn Java':
ISBN: 1011, Title: Learn Java: Advanced Topics, Author: Jane DoeThis basic Library Management System provides an efficient way to manage books. As an enhancement, you might consider adding more advanced features such as borrowing and returning books, managing users, integrating with a database, or incorporating a user interface.Related Java Projects
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment