Write the Java code

Depending on the case, it may be useful to hide part of the complexity of a call to an API behind a "business" layer.

In the present example, you will choose to handle the intermediate result, namely the dollar stock price, transparently. The aim of this is to simplify as far as possible the succeeding JavaScript calls.

public class SampleClient 

       { private StockProvider remoteStockProvider; 
       private CurrencyConverter remoteCurrencyConverter; 

       private float usdStockQuote=-1; 

       public void connect() throws NotBoundException, IOException { 
              this.remoteStockProvider = (StockProvider) Naming.lookup( 
                     "//fooserver/StockProvider" 
              ); 
              this.remoteCurrencyConverter = (CurrencyConverter) Naming.lookup( 
                     "//fooserver/CurrencyConverter" 
              ); 
       } 
       
       public boolean isConnected() {
              return this.remoteStockProvider != null && remoteCurrencyConverter != null; 
       } 

       public void getStockQuoteUSD(String stockSymbol) throws RemoteException { 
              if (!isConnected()) { 
                     throw new IllegalStateException("Not connected to server"); 
              } 

              this.usdStockQuote = this.remoteStockProvider.getStockQuote(stockSymbol); 
       } 

       public float getStockQuoteEur() throws RemoteException { 
              if (this.usdStockQuote < 0) { 
                     throw new IllegalStateException("The method getStockQuoteUSD() " + 
                                                     "has not been called."); 
              } 
              return this.remoteCurrencyConverter.currencyConvert( 
                     "USD", "EUR", this.usdStockQuote 
              ); 
       } 

}

The connect() method is used to connect to the remote object, getStockQuoteUSD() retrieves the specified stock price and stores it internally, and getStockQuoteEur() returns the stock price converted into Euros.