RMI – implementation of the library book

As from previous post, the Borrowable interface, for the RMI (Remote Method Invocation), this is the part of the Book class, this is what is called from the Library as such to see if a book has been taken out or not.

import java.util.*;
import java.rmi.*;
import java.rmi.server.*;
 
// UnicaseRemoteObject = remote object classes need to implement the related
// remote interface
public class Book extends UnicastRemoteObject implements Borrowable {
	private Date borrowDate;
	private Date returnDate;
	private String borrowerID;
	private String title;
	private boolean checkedOut = false;
 
	// create book.
	public Book (String bookName) throws RemoteException
	{
		title = bookName;
	}
 
	// is the book already checked out ?
	public boolean isCheckedOut() throws RemoteException
	{
		return checkedOut;
	}
 
	// if the book is checked out, return the library card number + date it was taken out on
	public String checkedOutCardNumber() throws RemoteException
	{
		String returnID = null;
		if (isCheckedOut())
		{
			returnID = borrowerID + " Date taken out on "+ borrowDate.toString();
		}
		return returnID;
	}
 
	public boolean checkOut(String cardNumber, Date d ) throws RemoteException
	{
		// do not check out the book if already checked out!
		if (isCheckedOut())
			return false;
		borrowerID = cardNumber;
		borrowDate = d;
		checkedOut = true;
		return true;
	}
 
	public boolean checkIn(String cardNumber, Date d) throws RemoteException
	{
		// if it is checked out, check the book in.
		if (isCheckedOut())
		{
			borrowerID = cardNumber;
			returnDate = d;
			checkedOut = false;
			return true;
		}
		return false;
	}
}

if you save that as Book.java, next comes the fun part the actual Library server as such.

Leave a Reply

Your email address will not be published. Required fields are marked *