Implement a Remote Interface

This code sample provides an implementation for the remote interface. The implementation runs on a Java Card Platform, so it can use only features that are supported by a Java Card Platform, Version 3.1.

package examples.purse;

import javacard.framework.*;
import javacard.framework.service.*;
import java.rmi.*;

public class PurseImpl extends CardRemoteObject implements Purse {
    private short balance;

    PurseImpl() {
        super();
        balance = 0;
    }

    public short debit(short amount) throws RemoteException, UserException {
        if ((amount < 0) || (amount > MAX_AMOUNT))
            UserException.throwIt(REQUEST_FAILED);
        balance -= amount;
        return balance;
    }

    public short credit(short amount) throws RemoteException, UserException {
        if ((amount < 0) || (balance < amount))
            UserException.throwIt(REQUEST_FAILED);
        balance += amount;
        return balance;
   }

   public short getBalance() throws RemoteException, UserException {
       return balance;
   }
}

Here, the remote interface is the Purse interface, which declares the remotely accessible methods. By implementing this interface, the class establishes a contract between itself and the compiler, by which the class promises that it will provide method bodies for all the methods declared in the interface:

public class PurseImpl extends CardRemoteObject implements Purse

The class also extends the javacard.framework.service.CardRemoteObject class. This class provides basic support for remote objects, and in particular the ability to export or unexport an object.