Java Reference
In-Depth Information
Example 16−1: Bank.java (continued)
}
/**
* This class is a simple stand-alone client program that interacts
* with a RemoteBank server. It invokes different RemoteBank methods
* depending on its command-line arguments, and demonstrates just how
* simple it is to interact with a server using RMI.
**/
public static class Client {
public static void main(String[] args) {
try {
// Figure out what RemoteBank to connect to by reading a system
// property (specified on the command line with a -D option to
// java) or, if it is not defined, use a default URL. Note
// that by default this client tries to connect to a server on
// the local machine
String url = System.getProperty("bank", "rmi:///FirstRemote");
// Now look up that RemoteBank server using the Naming object,
// which contacts the rmiregistry server. Given the url, this
// call returns a RemoteBank object whose methods may be
// invoked remotely
RemoteBank bank = (RemoteBank) Naming.lookup(url);
// Convert the user's command to lower case
String cmd = args[0].toLowerCase();
// Now, go test the command against a bunch of possible options
if (cmd.equals("open")) { // Open an account
bank.openAccount(args[1], args[2]);
System.out.println("Account opened.");
}
else if (cmd.equals("close")) { // Close an account
FunnyMoney money = bank.closeAccount(args[1], args[2]);
// Note: our currency is denominated in wooden nickels
System.out.println(money.amount +
" wooden nickels returned to you.");
System.out.println("Thanks for banking with us.");
}
else if (cmd.equals("deposit")) { // Deposit money
FunnyMoney money=new FunnyMoney(Integer.parseInt(args[3]));
bank.deposit(args[1], args[2], money);
System.out.println("Deposited " + money.amount +
" wooden nickels.");
}
else if (cmd.equals("withdraw")) { // Withdraw money
FunnyMoney money = bank.withdraw(args[1], args[2],
Integer.parseInt(args[3]));
System.out.println("Withdrew " + money.amount +
" wooden nickels.");
}
else if (cmd.equals("balance")) { // Check account balance
int amt = bank.getBalance(args[1], args[2]);
System.out.println("You have " + amt +
" wooden nickels in the bank.");
}
else if (cmd.equals("history")) {
// Get transaction history
List transactions =
Search WWH ::




Custom Search