📄 bankinterfaceimpl.java
字号:
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.RMISecurityManager;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.rmi.*;
import java.util.*;
/*
* 创建日期 2005-9-14
*
* TODO 要更改此生成的文件的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
public class BankInterfaceImpl extends UnicastRemoteObject implements
BankInterface{
private static Vector v;
private Set clients = new HashSet();
public BankInterfaceImpl() throws RemoteException {
super();
}
public Data login(int accountNumber, String password) {
//System.out.println(accountNumber+" "+password);
int i =v.size();
for(int j=0;j<i;j++){
Data da=(Data)v.elementAt(j);
if(da.getAccountNumber()==accountNumber&&da.getPassword().equals(password)){
return da;
}
}
return new Data(-1,null,null,null);
}
// Add method to withdraw cash from this account
public Data registe(String name, String residence, String password) {
Data da =(Data)v.lastElement();
System.out.print(da.getAccountNumber());
da=new Data(da.getAccountNumber()+1,password,name,residence);
v.add(da);
return da;
}
public void remit(int from, int to, int amount) {
int i =v.size();
for(int j=0;j<i;j++){
Data da=(Data)v.elementAt(j);
if(da.getAccountNumber()==from){
System.out.println("transfering money");
da.paysTo(to,amount);
System.out.println("transfered money");
postMessage(da);
}
}
for(int j=0;j<i;j++){
Data da=(Data)v.elementAt(j);
if(da.getAccountNumber()==to){
System.out.println("receiving money");
da.receives(to,amount);
System.out.println("received money");
postMessage(da);
}
}
}
public void logOff(BankUserInterface b) {
synchronized(clients){
clients.remove(b);
}
System.out.println("Unregistered Client:" + b);
}
public void registerClient(BankUserInterface b){
synchronized(clients){
clients.add(b);
}
System.out.println("Registered Client:" + b);
}
public void postMessage(Data d){
Iterator iterator = null;
synchronized(clients){
iterator = new HashSet(clients).iterator();
}
while(iterator.hasNext()){
BankUserInterface bankuser = (BankUserInterface)iterator.next();
try{
bankuser.display(d);
}
catch(Exception e){
System.err.println("Unregistering absent client");
logOff(bankuser);
}
}
}
public static void main(String args[]) {
if (System.getSecurityManager() == null) {
System.setSecurityManager(new RMISecurityManager());
}
BankInterfaceImpl bm = null;
Data d = new Data();
v= new Vector();
v.add(d);
try {
// Create a BankManager object
bm = new BankInterfaceImpl();
// Export it to RMI
UnicastRemoteObject.exportObject(bm);
} catch (RemoteException remoteException) {
System.err.println("Failure during object export to RMI: "
+ remoteException);
}
// Register an external name for the service
try {
Naming.rebind("//localhost/Bank", bm);
} catch (RemoteException remoteException) {
System.err.println("Failure during Name registration: "
+ remoteException);
} catch (MalformedURLException malformedException) {
System.err.println("Failure during Name registration: "
+ malformedException);
}
System.out.println("Server started.");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -