LIBRARY MANAGEMENT SYSTEM

                 

                                                LIBRARY MANAGEMENT USING RMI


1.Remote Interface - LibraryService.java

import java.rmi.Remote;

import java.rmi.RemoteException;

import java.util.List;


public interface LibraryService extends Remote {

    String registerUser(String name) throws RemoteException;

    String addBook(String title, String author) throws RemoteException;

    String issueBook(String userName, String title) throws RemoteException;

    String returnBook(String userName, String title) throws RemoteException;

    List<Book> listBooks() throws RemoteException;

}


2.Book data class - Book.java

import java.io.Serializable;


public class Book implements Serializable {

    private static final long serialVersionUID = 1L;


    private String title;

    private String author;

    private boolean issued;

    private String issuedTo;


    public Book(String title, String author) {

        this.title = title;

        this.author = author;

        this.issued = false;

        this.issuedTo = "";

    }


    public String getTitle() { return title; }

    public String getAuthor() { return author; }

    public boolean isIssued() { return issued; }

    public String getIssuedTo() { return issuedTo; }


    public void issue(String user) {

        this.issued = true;

        this.issuedTo = user;

    }


    public void returnBack() {

        this.issued = false;

        this.issuedTo = "";

    }


    public String toString() {

        return "\"" + title + "\" by " + author +

               (issued ? " [Issued to " + issuedTo + "]" : " [Available]");

    }

}


3.Implementation - LibraryServiceImpl.java

import java.rmi.server.UnicastRemoteObject;

import java.rmi.RemoteException;

import java.util.*;


public class LibraryServiceImpl extends UnicastRemoteObject implements LibraryService {

    private List<Book> books;

    private Set<String> users;


    protected LibraryServiceImpl() throws RemoteException {

        books = new ArrayList<>();

        users = new HashSet<>();

    }


    public synchronized String registerUser(String name) throws RemoteException {

        if (users.contains(name)) return "User already registered.";

        users.add(name);

        return "User " + name + " registered.";

    }


    public synchronized String addBook(String title, String author) throws RemoteException {

        books.add(new Book(title, author));

        return "Book added: " + title;

    }


    public synchronized String issueBook(String userName, String title) throws RemoteException {

        if (!users.contains(userName)) return "User not registered.";

        for (Book book : books) {

            if (book.getTitle().equalsIgnoreCase(title) && !book.isIssued()) {

                book.issue(userName);

                return "Book issued to " + userName;

            }

        }

        return "Book not available.";

    }


    public synchronized String returnBook(String userName, String title) throws RemoteException {

        for (Book book : books) {

            if (book.getTitle().equalsIgnoreCase(title) &&

                book.isIssued() &&

                book.getIssuedTo().equals(userName)) {

                book.returnBack();

                return "Book returned.";

            }

        }

        return "Return failed. Book not found or not issued to " + userName;

    }


    public List<Book> listBooks() throws RemoteException {

        return books;

    }

}


4.Server - LibraryServer.java

import java.rmi.Naming;

import java.rmi.registry.LocateRegistry;


public class LibraryServer {

    public static void main(String[] args) {

        try {

            LocateRegistry.createRegistry(1099);

            LibraryService service = new LibraryServiceImpl();

            Naming.rebind("LibraryService", service);

            System.out.println("Library Server is running...");

        } catch (Exception e) {

            System.out.println("Server Error: " + e.getMessage());

        }

    }

}


5.Console client - LibraryClient.java

import java.rmi.Naming;

import java.util.List;


public class LibraryClient {

    public static void main(String[] args) {

        try {

            LibraryService service = (LibraryService) Naming.lookup("rmi://localhost/LibraryService");


            System.out.println(service.registerUser("Alice"));

            System.out.println(service.addBook("The Hobbit", "J.R.R. Tolkien"));

            System.out.println(service.addBook("1984", "George Orwell"));


            System.out.println(service.issueBook("Alice", "The Hobbit"));

            System.out.println(service.returnBook("Alice", "The Hobbit"));

            System.out.println(service.issueBook("Alice", "1984"));


            System.out.println("\n--- All Books ---");

            List<Book> books = service.listBooks();

            for (Book b : books) {

                System.out.println(b);

            }


        } catch (Exception e) {

            e.printStackTrace();

        }

    }

}

6. Expected Output

Server
















Client



Comments

Popular posts from this blog

LOCATION TRACKER

HEALTH ADMINISTRATION