📄 rmiimpl.java
字号:
import java.rmi.server.UnicastRemoteObject;
import java.util.Iterator;
import java.util.Vector;
import java.util.Date;
import java.util.HashMap;
import java.text.SimpleDateFormat;
/**
* Class RMIImpl implenments all the
* interface RMIIntf's abstract method.
*
* @version 1.0 9 June 2007
* @author Zhao Xiaoyu
*/
public class RMIImpl extends UnicastRemoteObject implements RMIIntf{
final static long serialVersionUID = 1564646262L;
/** Define a static hashmap to store username-password pairs */
private static HashMap<String , String> user = new HashMap<String, String>();
/** Define a static vector to store message items */
private static Vector<MessageContent> messageList = new Vector<MessageContent>();
/** The variable sdf define time display format of my style */
private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
/** Constructor which extends father method default constructor */
public RMIImpl()throws java.rmi.RemoteException{
super();
}
/** Implement the functionality of register users
*
* @para u the u express the usersname
* @para p the p express the password
* @return return the error message while register-process.
*/
public String register(String u,String p){
if(!isUserExisted(u)){
user.put(u, p);
return "\nRegister Successfully!\n";
}else
return "\nUsername Already Existed, Please select another!\n";
}
/** Implement the functionality of show users
*
* @return return a list of username or the error message.
*/
public String showusers(){
String returnList = "";
if(!user.isEmpty()){
Iterator i = user.keySet().iterator();
returnList += "\nAll usernames are listing below :\n";
while(i.hasNext()){
returnList += (String)i.next() + "\n";
}
return returnList;
}else
return "\nSorry, There is no user registered now!\n";
}
/** Implement the functionality of check message.
*
* @para u the u express the usersname
* @para p the p express the password
* @return return the error hint or alist of message.
*/
public String checkmessages(String u, String p){
int index = 0;
String returnMessage = "";
if(isUserExisted(u)){
if(canLogin(u, p)){
if(!isUserHasNoMessage(u)){
Iterator i = messageList.iterator();
returnMessage +="\nYour messages are listing below :\n";
while(i.hasNext()){
MessageContent m = (MessageContent)i.next();
if(m.getReceiver().equals(u)){
index++;
returnMessage += "Mail " + index + " :";
returnMessage += m.toString();
}
}
}else
return "\nSorry, There is no message for you!\n";
}else
return "\nYour Password Wrong, Input again!\n";
}else{
return "\nYou haven't registered yet, Please register first\n";
}
return returnMessage;
}
/** Implement the functionality of leave message.
*
* @para u the u express the usersname
* @para r the r express the receiver names.
* @para mt the mt express the message content
* @return return the error message.
*/
public String leavemessage(String u, String r, String mt){
String sendDate = "";
if(isUserExisted(u)){
if(!u.equals(r)){
Date now = new Date();
sendDate = sdf.format(now);
MessageContent mc = new MessageContent(u, r, sendDate, mt);
messageList.add( mc );
return "\nLeave message successfully!\n";
}else
return "\nYou shouldn't send message to yourself\n";
}else
return "\nLogin failed!\n";
}
/** Implement the functionality of count logic value.
*
* @para u the u express the usersname
* @return return a boolean to express whether there
* there is no message for the user.
*/
public boolean isUserHasNoMessage(String u){
Iterator i = messageList.iterator();
while(i.hasNext()){
if(((MessageContent)i.next()).getReceiver().equals(u))
return false;
}
return true;
}
/** Implement the functionality of count logic value.
*
* @para u the u express the usersname
* @para p the p express the password
* @return return a boolean to judge whether the user can login.
*/
public boolean canLogin(String u, String p){
if(isUserExisted(u)){
if(user.get(u).equals(p))
return true;
}
return false;
}
/** Implement the functionality of count logic value.
*
* @para u the u express the usersname
* @return return a boolean to judge whether the username existed.
*/
public boolean isUserExisted(String u){
return user.containsKey(u);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -