📄 serverimpl.java
字号:
import ex5.*;
import org.omg.CORBA.*;
import java.rmi.RemoteException;
import java.text.SimpleDateFormat;
import java.util.*;
public class ServerImpl extends ServicePOA{
private ORB orb;
private static Hashtable users = new Hashtable();
private static Vector<todolist> lists = new Vector<todolist>();
todolist l;
private int counter = 0;
private int deleted = 0;
/*构造函数*/
/**
* 构造函数好像没有运行到这里,这个方法实现了ServicePOA接口
*/
public void ServerImpl()
{
users = new Hashtable();
lists = new Vector<todolist>();
}
//建立ORB
public void setORB(ORB orb_val)
{
orb = orb_val;
}
//用户登陆
public boolean login(String username, String password)
{
if (!users.containsKey(username)) {
return false;
}
String p = (String)users.get(username);
if (p.equals(password))
return true;
return false;
}
//用户注册
public boolean register(String username, String password)
{
if (users.containsValue(username))
return false;
if (password == null)
return false;
users.put(username, password);
return true;
}
//添加一个Item
public String addItems(String username, String password,String label ,String start, String end){
try{
if (!login(username, password))
return null;
Date s = new CORBAClient().changeDate(start);
Date e = new CORBAClient().changeDate(end);
counter++;
lists.add(new todolist(counter,username, password, label,
s, e));
return "success";
}catch (Exception e1){
e1.printStackTrace(System.out);
return null;}
}
//列出时间范围的item
public String queryItems(String username, String password,String start, String end){
if (!login(username, password))
return null;
String result = "";
todolist list;
String u ;
String p;
Date s;
Date e;
Vector<todolist> v = new Vector<todolist>();
SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy-MM-dd");
for (Iterator it = lists.iterator(); it.hasNext();) {
list = (todolist) it.next();
u = list.getUsername();
p = list.getUserPassword();
if (u.equals(username) && p.equals(password)) {
s = list.getStart();
e = list.getEnd();
if (!(s.after(new CORBAClient().changeDate(start))
&& e.before(new CORBAClient().changeDate(end))))
v.add(list);
result +="Id: " + list.getId()
+" label: "+list.getLabel()+"\r\n"
+"From: "+bartDateFormat.format(list.getStart())+"\r\n"
+"To: "+bartDateFormat.format(list.getEnd())+"\r\n";
}
}
return result;
}
//删除一个item
public String deleteItems(String username, String password,int id){
try{
if (!login(username, password))
return null;
todolist l;
int i;
String u;
String p;
for (int j = 0; j < lists.size(); j++) {
l = lists.get(j);
i = l.getId();
u = l.getUsername();
p = l.getUserPassword();
if (i == id && u.equals(username) && p.equals(password)) {
lists.remove(j);
j--;
deleted++;
return "success";
}
}
return "failed";
}catch (Exception e){}
return null;
}
//清除所有的items
public String clearItems(String username, String password){
try{
if (!login(username, password))
return null;
todolist l;
String u;
String p;
for (int i = 0; i < lists.size(); i++) {
l = lists.get(i);
u = l.getUsername();
p = l.getUserPassword();
if (u.equals(username) && p.equals(password)) {
lists.remove(i);
i--;
deleted++;
}
}
return "success";
}catch (Exception e){}
return "failed";
}
public void shutdown(){
orb.shutdown(false);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -