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

P3

The document is a Java implementation of a Library Management System that allows users to add, view, search, issue, and return books. It defines a Book class with attributes such as book ID, title, author, and issuance status, along with methods for managing these attributes. The main class contains a menu-driven interface for user interaction and utilizes an ArrayList to store the collection of books.

Uploaded by

aryanrajpro949
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)
2 views

P3

The document is a Java implementation of a Library Management System that allows users to add, view, search, issue, and return books. It defines a Book class with attributes such as book ID, title, author, and issuance status, along with methods for managing these attributes. The main class contains a menu-driven interface for user interaction and utilizes an ArrayList to store the collection of books.

Uploaded by

aryanrajpro949
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/ 3

import java.util.

ArrayList;
import java.util.Scanner;

class Book {
private String bookId;
private String title;
private String author;
private boolean isIssued;

public Book(String bookId, String title, String author) {


this.bookId = bookId;
this.title = title;
this.author = author;
this.isIssued = false;
}

public String getBookId() {


return bookId;
}

public boolean isIssued() {


return isIssued;
}

public void issue() {


if (!isIssued) {
isIssued = true;
System.out.println("Book issued successfully!");
} else {
System.out.println("Book is already issued.");
}
}

public void returnBook() {


if (isIssued) {
isIssued = false;
System.out.println("Book returned successfully!");
} else {
System.out.println("Book was not issued.");
}
}

public void display() {


System.out.println("ID: " + bookId);
System.out.println("Title: " + title);
System.out.println("Author: " + author);
System.out.println("Status: " + (isIssued ? "Issued" : "Available"));
System.out.println("---------------------------------");
}
}

public class LibraryManagementSystem {


static ArrayList<Book> bookList = new ArrayList<>();
static Scanner sc = new Scanner(System.in);

public static void main(String[] args) {


int choice;

do {
System.out.println("\n***** LIBRARY MANAGEMENT SYSTEM *****");
System.out.println("1. Add Book");
System.out.println("2. View All Books");
System.out.println("3. Search Book by ID");
System.out.println("4. Issue Book");
System.out.println("5. Return Book");
System.out.println("6. Exit");
System.out.print("Enter your choice: ");
choice = sc.nextInt();
sc.nextLine(); // consume newline

switch (choice) {
case 1:
addBook();
break;
case 2:
viewBooks();
break;
case 3:
searchBook();
break;
case 4:
issueBook();
break;
case 5:
returnBook();
break;
case 6:
System.out.println("Thank you for using the Library System!");
break;
default:
System.out.println("Invalid choice. Try again!");
}
} while (choice != 6);
}

static void addBook() {


System.out.print("Enter Book ID: ");
String id = sc.nextLine();
System.out.print("Enter Book Title: ");
String title = sc.nextLine();
System.out.print("Enter Author Name: ");
String author = sc.nextLine();

Book book = new Book(id, title, author);


bookList.add(book);
System.out.println("Book added successfully!");
}

static void viewBooks() {


if (bookList.isEmpty()) {
System.out.println("No books in the library.");
return;
}

for (Book b : bookList) {


b.display();
}
}
static void searchBook() {
System.out.print("Enter Book ID to search: ");
String id = sc.nextLine();

boolean found = false;


for (Book b : bookList) {
if (b.getBookId().equals(id)) {
b.display();
found = true;
break;
}
}

if (!found) {
System.out.println("Book not found.");
}
}

static void issueBook() {


System.out.print("Enter Book ID to issue: ");
String id = sc.nextLine();

Book book = findBook(id);


if (book != null) {
book.issue();
} else {
System.out.println("Book not found.");
}
}

static void returnBook() {


System.out.print("Enter Book ID to return: ");
String id = sc.nextLine();

Book book = findBook(id);


if (book != null) {
book.returnBook();
} else {
System.out.println("Book not found.");
}
}

static Book findBook(String id) {


for (Book b : bookList) {
if (b.getBookId().equals(id)) {
return b;
}
}
return null;
}
}

You might also like