【发布时间】:2012-01-19 12:20:35
【问题描述】:
我有一个包含用户和频道的聊天程序。我的下一个任务是获取一个用户所在频道的列表。应该如何完成?
以下是目前的代码:
ChatFrontImpl:
private Hashtable<String, ArrayList<String>> channels = new Hashtable<String, ArrayList<String>>();
private ArrayList<Client> clients;
public synchronized boolean registerClient(Client client, String password) throws RemoteException {
if(!u.logIn(client.findName(), password)){
System.out.println("Wrong username or password!");
return false;
}
if (!clients.contains(client)) {
try {
clients.add(client);
updateJlist();
System.out.println(client.findName() + " registered.");
}
catch (Exception e){
System.out.println("error in method registerClient(): " + e);
}
return true;
}else
return false;
}
public void connectChannel(String username, String channel) throws RemoteException{
if(isUserRegistered(username)){
if (!channels.containsKey(channel)) {
String message = "User " + username + " entered the channel";
channels.put(channel, new ArrayList<String>());
channels.get(channel).add(username);
notifyChannelSystem(channel, "SYSTEM", message);
notifySelf(username, "Write /? for avaliable commands");
}
else{
if(channels.get(channel).contains(username)){
}
else {
channels.get(channel).add(username);
String message = "User " + username + " just entered the channel";
notifyChannelSystem(channel, "SYSTEM", message);
}
}
}
}
【问题讨论】:
-
您能否扩展您的问题以包括变量 kanal 的定义。如果 kanal 实际上应该是 channel,则该程序是有意义的。
-
哦,代码通常是挪威语,但我翻译了(显然只是其中的一部分)。变量应该是通道,只要它说 kanal ofc :)
-
我更正了变量 + 将方法更改为 void,因为我真的不需要返回