I have a chatprogram which contains users and channels. My next quest is to get a list of channels which one user is in. How should this be done?
Here are the codes as of now:
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);
}
}
}
}
-
Can you expand your question to include the definition of the variable kanal please. The programme makes sense if kanal should actually be channel.Dan Hardiker– Dan Hardiker2012年01月19日 12:32:22 +00:00Commented Jan 19, 2012 at 12:32
-
oh, the code is usually in norwegian, but i translated(obviously just parts of it). The variable should be channel wherever it says kanal ofc :)sindrem– sindrem2012年01月19日 12:49:05 +00:00Commented Jan 19, 2012 at 12:49
-
I corrected the variable + changed the method to void, as i dont need returns reallysindrem– sindrem2012年01月19日 12:51:53 +00:00Commented Jan 19, 2012 at 12:51
2 Answers 2
I'd use a different data structure - but assuming that you wish to continue with this one (in order to answer the question):
public List<String> getChannelsForUsername(String username) {
List<String> userChannels = new ArrayList<String>();
for (String channel : channels.keySet()) {
if (channels.get(channel).contains(username)) {
userChannels.add(channel);
}
}
return userChannels;
}
3 Comments
public List<String> getChannelsForUsername(String username) { List<String> userChannels = new ArrayList<String>(); for (Enumeration e = channels.elements() ; e.hasMoreElements() ;) { if (channels.get(channels).contains(username)) { userChannels.add(channels); } } return userChannels; }
Shouldn't it be something like this? I'm getting an awful error tho. I'm not allowed to use .add(channels). But i can add username, which is kinda oddHave a map
private HashMap<Client, channlesList> clientsAndRooms;
private ArrayList channels = ArrayList <channel>();
I don't know why you have hashTable there, I would try to avoid it.
while connecting user to a channel
1) check if hasmap already has that user You can get keySet and do Contains. If there, get channelList and new channel to this list and save to map again. 2) If this is first channel,
channelList.add(channel);
clientAndRooms.put(userName,channelList);
Note: There may be syntax errors, I just typed it here.