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);
}
}
}
}
I’d use a different data structure – but assuming that you wish to continue with this one (in order to answer the question):