ch.
srikanth
Case Study:
2023003828
Problem Description
You are tasked with developing a simple Library Management System in Java to manage books and
members. The system should allow librarians to add books, register members, and track book borrowing. It
should also handle exceptional cases (e.g., invalid member details or insufficient book stock) and provide a
basic reporting feature. Your program must demonstrate the use of object-oriented programming principles,
arrays, strings, inheritance, interfaces, packages, and exception handling.
📦 library
┣ 📂 books
┃ ┣ 📜 LibraryItem.java
┃ ┣ 📜 Book.java
┃ ┗ 📜 StockException.java
┣ 📂 members
┃ ┣ 📜 Member.java
┃ ┗ 📜 InvalidMemberException.java
┗ 📜 LibraryManagement.java (Main class)
Code:
//srikanth
//2023003828
import java.util.ArrayList;
import java.util.Scanner;
// Abstract class representing a resource in the library
abstract class Resource {
protected String title;
protected int id;
public Resource(String title, int id) {
this.title = title;
this.id = id;
}
public abstract void displayInfo();
}
// Interface for resources that can be lent
interface Borrowable {
boolean lendItem(int quantity);
}
// Class representing a book, which extends Resource and implements Borrowable
class Book extends Resource implements Borrowable {
private String author;
private int stock;
public Book(String title, int id, String author, int stock) throws OutOfStockException {
super(title, id);
if (stock < 0) throw new OutOfStockException("Cannot have negative stock.");
this.author = author;
this.stock = stock;
}
@Override
public void displayInfo() {
System.out.println("Book ID: " + id + ", Title: " + title + ", Author: " + author + ", Copies
Available: " + stock);
}
@Override
public boolean lendItem(int quantity) {
if (stock >= quantity) {
stock -= quantity;
return true;
}
return false;
}
}
// Custom exception when stock is insufficient
class OutOfStockException extends Exception {
public OutOfStockException(String message) {
super(message);
}
}
// Custom exception when user information is invalid
class UserInvalidException extends Exception {
public UserInvalidException(String message) {
super(message);
}
}
// Class representing a library user
class User {
private int userId;
private String name;
private int age;
public User(int userId, String name, int age) throws UserInvalidException {
if (!name.matches("[A-Za-z ]+") || age < 18 || age > 90) {
throw new UserInvalidException("Invalid user details.");
}
this.userId = userId;
this.name = name;
this.age = age;
}
public void showUserInfo() {
System.out.println("User ID: " + userId + ", Name: " + name + ", Age: " + age);
}
}
// Main class to simulate the library system
public class LibraryApp {
private static ArrayList<Book> books = new ArrayList<>();
private static ArrayList<User> users = new ArrayList<>();
// Method to find a book by its title
public static Book searchBook(String title) {
for (Book book : books) {
if (book != null && book.title.equalsIgnoreCase(title)) {
return book;
}
}
return null;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int choice;
do {
System.out.println("\n1. Add New Book\n2. Register User\n3. Lend Book\n4. View
Library Info\n5. Exit\nChoose an option: ");
choice = scanner.nextInt();
scanner.nextLine();
try {
switch (choice) {
case 1:
System.out.print("Enter book title: ");
String title = scanner.nextLine();
System.out.print("Enter book ID: ");
int id = scanner.nextInt();
scanner.nextLine();
System.out.print("Enter author's name: ");
String author = scanner.nextLine();
System.out.print("Enter number of available copies: ");
int stock = scanner.nextInt();
books.add(new Book(title, id, author, stock));
break;
case 2:
System.out.print("Enter user ID: ");
int userId = scanner.nextInt();
scanner.nextLine();
System.out.print("Enter full name: ");
String userName = scanner.nextLine();
System.out.print("Enter age: ");
int userAge = scanner.nextInt();
users.add(new User(userId, userName, userAge));
break;
case 3:
System.out.print("Enter book title to borrow: ");
String bookToLend = scanner.nextLine();
Book book = searchBook(bookToLend);
if (book != null) {
System.out.print("Enter quantity to borrow: ");
int quantity = scanner.nextInt();
if (book.lendItem(quantity)) {
System.out.println("Book borrowed successfully!");
} else {
System.out.println("Not enough copies available.");
}
} else {
System.out.println("Book not found.");
}
break;
case 4:
System.out.println("\nBooks in Library:");
for (Book b : books) {
b.displayInfo();
}
System.out.println("\nRegistered Users:");
for (User u : users) {
u.showUserInfo();
}
break;
case 5:
System.out.println("Shutting down Library System...");
break;
default:
System.out.println("Invalid selection. Please try again.");
}
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
} while (choice != 5);
scanner.close();
}
}
output: