⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 messagecenterimpl.java

📁 SSD8的exam2答案
💻 JAVA
字号:
/*
 * this is MessageCenterImpl class 
 * @author: Chen Yao
 * @studentID:200532580247
 * @date:6.14.2008
 */
import java.rmi.*;
import java.util.Date;
public class MessageCenterImpl extends java.rmi.server.UnicastRemoteObject implements MessageCenter{
	User user[] = new User[999];
	int usertop = 0;//the total number of users.
	String show;//used to keep the methods' return value.
	
	//A constructor.
	public MessageCenterImpl()throws RemoteException{
		
	}
	
	//implement method register.
	public String register(String userName, String password) throws RemoteException{
		//make sure every user is unique.
		for(int i = 0; i < usertop; i++){
			if(user[i].userName.equals(userName)){
				show = "This user has existed, please change your register name.";
				return show;
			}
		}
		
		User aUser = new User(userName, password);//create a new user.
		user[usertop] = aUser;
		usertop++;//add one to the total number of users.

		show = "Register Successful";
		System.out.println( userName + " has  registered");
		return show;
	}
	
	//implement method showUsers.
public String showUsers()throws RemoteException{
 show="";
		if(usertop == 0){//if there are no users.
      
      show = " There are no users utill now.";
    
		}else{//show all the users who have registered.

      show = "users :" + "\n";
			for(int i=0;i<usertop;i++){

				show +=(i+1) + " : " + user[i].userName + "\n";
			}
		}
		return show;
	}
	
//implement method checkMessage.
	public String checkMessage(String uName,String psw)throws RemoteException{
       show="";
		if(usertop != 0){

			for(int i = 0; i < usertop; i++){//only the registered users can check their messages.

				if(user[i].userName.equals(uName) && user[i].password.equals(psw)){

                   int j = user[i].MSGtop;//the user's total message number.
                   if(j == 0){//if there are no messages for the user.
          	
          	       show = "No message for you";
          
                   }else{//show all the message for the user.
          	
          	           show = "There are Messages :"+"\n";

						for(int k = 0; k < j; k++){

							show += " From : " + user[i].msg[k].sendUser + " Message :"+user[i].msg[k].messageTexts + " Time: "+user[i].msg[k].date+"\n";

						}
						
                   }
	            }
			}if(show==""){//if the user's name or password is wrong.
				
				show = "Wrong userName or password";
			}
		}else{//if there is no users.
			show = "There are no users.";
		}
		return show;
	} 
	
	//implement the method leaveMessage.
	public String leaveMessage(String sender,String receiver,String message) throws RemoteException{
		show="";
    
		for(int i = 0; i < usertop; i++){

			if(user[i].userName.equals(sender)){

				for(int j = 0; j < usertop; j++){

					if(user[j].userName.equals(receiver)){
                        Date d=new Date();//the date and time the message was left.
						Message ml=new Message(sender, receiver, message ,d);//create a new message.
						user[j].msg[user[j].MSGtop] = ml;
						user[j].MSGtop++;//add one to the user's total message number.
						show = "Leave Message";
						}
				}if(show==""){//if the receiver do not exist.
					show="the receiver do not exist.";
				}
			}
		}if(show==""){//if the sender have not registered.
			show="You have to register first!";
		}
  
    return show;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -