0% found this document useful (0 votes)
21 views4 pages

Library System

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views4 pages

Library System

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

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 +
'}';
}
}

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;
}
}
}

You might also like