Methods Summary |
---|
public synchronized FunnyMoney | closeAccount(java.lang.String name, java.lang.String password)Close the named account. This method is synchronized to make it
thread safe, since it manipulates the accounts hashtable.
Account acct;
acct = verify(name, password);
accounts.remove(name);
// Before changing the balance or transactions of any account, we first
// have to obtain a lock on that account to be thread safe.
synchronized (acct) {
int balance = acct.balance;
acct.balance = 0;
return new FunnyMoney(balance);
}
|
public void | deposit(java.lang.String name, java.lang.String password, FunnyMoney money)Deposit the specified FunnyMoney to the named account
Account acct = verify(name, password);
synchronized(acct) {
acct.balance += money.amount;
acct.transactions.addElement("Deposited " + money.amount +
" on " + new Date());
}
|
public int | getBalance(java.lang.String name, java.lang.String password)Return the current balance in the named account
Account acct = verify(name, password);
synchronized(acct) { return acct.balance; }
|
public java.util.Vector | getTransactionHistory(java.lang.String name, java.lang.String password)Return a Vector of strings containing the transaction history
for the named account
Account acct = verify(name, password);
synchronized(acct) { return acct.transactions; }
|
public static void | main(java.lang.String[] args)The main program that runs this RemoteBankServer.
Create a RemoteBankServer object and give it a name in the registry.
Read a system property to determine the name, but use "FirstRemote"
as the default name. This is all that is necessary to set up the
service. RMI takes care of the rest.
try {
// Create a bank server object
RemoteBankServer bank = new RemoteBankServer();
// Figure out what to name it
String name = System.getProperty("bankname", "FirstRemote");
// Name it that
Naming.rebind(name, bank);
// Tell the world we're up and running
System.out.println(name + " is open and ready for customers.");
}
catch (Exception e) {
System.err.println(e);
System.err.println("Usage: java [-Dbankname=<name>] RemoteBankServer");
System.exit(1); // Force an exit because there might be other threads
}
|
public synchronized void | openAccount(java.lang.String name, java.lang.String password)Open a bank account with the specified name and password
This method is synchronized to make it thread safe, since it
manipulates the accounts hashtable.
// Check if there is already an account under that name
if (accounts.get(name) != null)
throw new BankingException("Account already exists.");
// Otherwise, it doesn't exist, so create it.
Account acct = new Account(password);
// And register it
accounts.put(name, acct);
|
public RemoteBankServer$Account | verify(java.lang.String name, java.lang.String password)This convenience method is not a remote method. Given a name and password
it checks to see if an account with that name and password exists. If
so, it returns the Account object. Otherwise, it throws an exception.
synchronized(accounts) {
Account acct = (Account)accounts.get(name);
if (acct == null) throw new BankingException("No such account");
if (!password.equals(acct.password))
throw new BankingException("Invalid password");
return acct;
}
|
public FunnyMoney | withdraw(java.lang.String name, java.lang.String password, int amount)Withdraw the specified amount from the named account
Account acct = verify(name, password);
synchronized(acct) {
if (acct.balance < amount)
throw new BankingException("Insufficient Funds");
acct.balance -= amount;
acct.transactions.addElement("Withdrew " + amount + " on "+new Date());
return new FunnyMoney(amount);
}
|