Java case study
Java case study
study
A.Hrushikesh varma
2023002077
The Library Management System is a simple,
yet effective Java-based application designed to
manage books and members in a library. It
allows librarians to add books, register
members, track book borrowing, and
display records while ensuring data integrity
through exception handling.
CODE:
import java.util.Scanner;
class LibraryItem {
protected String title;
protected int itemId;
interface Borrowable {
boolean borrowItem(int quantity);
}
@Override
public void displayDetails() {
super.displayDetails();
System.out.println("Author: " + author + ", Stock: " +
stock);
}
@Override
public boolean borrowItem(int quantity) {
if (quantity <= stock) {
stock -= quantity;
return true;
}
return false;
}
}
class Member {
private int memberId;
private String name;
private int age;
do {
System.out.println("\nLibrary Management System");
System.out.println("1. Add a book");
System.out.println("2. Register a member");
System.out.println("3. Borrow a book");
System.out.println("4. Display all books and members");
System.out.println("5. Exit");
System.out.print("Enter your choice: ");
choice = scanner.nextInt();
scanner.nextLine();
switch (choice) {
case 1:
if (bookCount < books.length) {
System.out.print("Enter book title: ");
String title = scanner.nextLine();
System.out.print("Enter item ID: ");
int itemId = scanner.nextInt();
scanner.nextLine();
System.out.print("Enter author: ");
String author = scanner.nextLine();
System.out.print("Enter stock: ");
int stock = scanner.nextInt();
try {
books[bookCount++] = new Book(title,
itemId, author, stock);
} catch (StockException e) {
System.out.println(e.getMessage());
}
} else {
System.out.println("Book storage full!");
}
break;
case 2:
if (memberCount < members.length) {
System.out.print("Enter member ID: ");
int memberId = scanner.nextInt();
scanner.nextLine();
System.out.print("Enter name: ");
String name = scanner.nextLine();
System.out.print("Enter age: ");
int age = scanner.nextInt();
try {
members[memberCount++] = new
Member(memberId, name, age);
} catch (InvalidMemberException e) {
System.out.println(e.getMessage());
}
} else {
System.out.println("Member storage full!");
}
break;
case 3:
System.out.print("Enter book title: ");
String title = scanner.nextLine();
System.out.print("Enter quantity: ");
int quantity = scanner.nextInt();
Book book = searchBookByTitle(title);
if (book != null && book.borrowItem(quantity)) {
System.out.println("Book borrowed
successfully!");
} else {
System.out.println("Book not available or
insufficient stock.");
}
break;
case 4:
System.out.println("\nBooks:");
for (Book b : books) {
if (b != null) b.displayDetails();
}
System.out.println("\nMembers:");
for (Member m : members) {
if (m != null) m.printMemberInfo();
}
break;
case 5:
System.out.println("Exiting...");
break;
default:
System.out.println("Invalid choice!");
}
} while (choice != 5);
scanner.close();
}
}
OUTPUT: