📄 bankimpl.java
字号:
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.rmi.Naming;
import java.util.*;
/**
*
*/
/**
* @author Kee
*
*/
public class BankImpl extends UnicastRemoteObject implements Bank {
private Hashtable accounts = new Hashtable();
private Hashtable openAccounts = new Hashtable();
private Hashtable lookup = new Hashtable();
private int nextPin = 0;
BankImpl() throws RemoteException{
}
/* (non-Javadoc)
* @see Bank#createAccount(java.lang.String)
*/
@Override
synchronized public int createAccount(String name) throws RemoteException {
// TODO Auto-generated method stub
if (accounts.containsKey(name)){
return -1;
}
else{
Account account = new Account();
accounts.put(name, account);
account.setPin(++nextPin);
return nextPin;
}
}
/* (non-Javadoc)
* @see Bank#openAccount(Details)
*/
@Override
synchronized public int openAccount(Details d) throws RemoteException {
// TODO Auto-generated method stub
String suppliedName = d.getName();
if (accounts.containsKey(suppliedName)){
Account account = (Account)accounts.get(suppliedName);
int storedPin = account.getPin();
int suppliedPin = d.getPin();
if (storedPin == suppliedPin){
if (openAccounts.containsKey(suppliedPin))
return -1;
else{
int id = storedPin + 1000;
lookup.put(new Integer(id), suppliedName);
openAccounts.put(suppliedName, new Integer(id));
return id;
}
}
else
return -1;
}
else
return -1;
}
/* (non-Javadoc)
* @see Bank#incBalance(int, double)
*/
@Override
synchronized public boolean incBalance(int id, double amount) throws RemoteException {
// TODO Auto-generated method stub
Integer passedId = new Integer(id);
if (lookup.contains(passedId)){
String name = (String)lookup.get(passedId);
if (accounts.containsKey(name)){
Account a = (Account)accounts.get(name);
return a.incBalance(amount);
}
else
return false;
}
else
return false;
}
/* (non-Javadoc)
* @see Bank#decBalance(int, double)
*/
@Override
public boolean decBalance(int id, double amount) throws RemoteException {
// TODO Auto-generated method stub
Integer passedId = new Integer(id);
if (lookup.containsKey(passedId)){
String name = (String)lookup.get(passedId);
if (accounts.containsKey(name)){
Account a = (Account)accounts.get(name);
if (a.decBalance(amount))
return true;
else
return false;
}
else
return false;
}
return false;
}
/* (non-Javadoc)
* @see Bank#getBalance(int)
*/
@Override
synchronized public double getBalance(int id) throws RemoteException {
// TODO Auto-generated method stub
Integer passedId = new Integer(id);
if (lookup.containsKey(passedId)){
String name = (String)lookup.get(passedId);
if (accounts.containsKey(name)){
Account a = (Account)accounts.get(name);
return a.getBalance();
}
else
return -1;
}
else
return -1;
}
/* (non-Javadoc)
* @see Bank#close(int)
*/
@Override
public boolean close(int id) throws RemoteException {
// TODO Auto-generated method stub
Integer passedId = new Integer(id);
if (lookup.containsKey(passedId)){
String suppliedName = (String)lookup.get(passedId);
lookup.remove(passedId);
openAccounts.remove(suppliedName);
return true;
}
else
return false;
}
public static void main(String[] args) throws RemoteException{
String name = "TheBank";
System.out.println("Registering the bank object...");
Bank bank = new BankImpl();
try{
Naming.rebind(name, bank);
}
catch (Exception e){
System.out.println("Caught exception while registering: " + e);
}
System.out.println("Remote bank ready...");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -