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 Doe
This 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.
Comments
Post a Comment
Leave Comment