Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is my code correct? 2

Status
Not open for further replies.

MissouriTiger

Programmer
Oct 10, 2000
185
US
Is anything visibly wrong with this code? I'm getting errors I can't figure out.


package com.gregnorris.messaging;
/**
* File: IMCommunity
* Author: Greg Norris
* Description: IMCommunity object serves as a container to hold the users.
* Each user is identifiable by a unique username.
*
*/


public class IMCommunity
{
public IMCommunity()
{
Map Community = new HashMap();
Community.putAll(Map t);
}

public void addUser(String username, IMUser user)
{
Community.put(username, user);
}

public IMUser getUser(String username, IMUser user)
{
return (Community.get(username));
}

public void deleteUser(String username)
{
Community.remove(username);
}

}
 
it would be helpful if you told us what errors you are getting but:

Community.putAll(Map t);

where is Map t coming from??? its not defined as a member variable nor is it passed in anywhere as a parameter??
 
An even bigger problem is that Community is declared in your CTOR and so does not exist in any of your methods.What you need to do is this:


public class IMCommunity
{
Map Community;
public IMCommunity(Map t)
{
Community = new HashMap();
Community.putAll(t);
}

public void addUser(String username, IMUser user)
{
Community.put(username, user);
}

public IMUser getUser(String username, IMUser user)
{
return (Community.get(username));
}

public void deleteUser(String username)
{
Community.remove(username);
}

}

This fixes both problems.
 
You are both correct. I also had more errors than I thought. One thing I still don't understand though. I had to change all references to IMUser to MessageSystemUser, which is the interface that IMUser is based on. That's the only way it will compile, and it's happened to me before. The compiler complains that it can't find the IMUser class, but I guarantee that my classpath points to it. Heck, it resides in the same package as this class.

Can anyone suggest why the compiler doesn't like my IMUser class? Here's my new code, although you probably don't need it.

package com.gregnorris.messaging;
import com.gregnorris.messaging.*;
import cis299j.messaging.*;
import java.util.Map;
import java.util.HashMap;
/**
* File: IMCommunity
* Author: Greg Norris
* Last mod: July, 2001
* Description: The IMCommunity object serves as a container to hold the
* user objects. Each user is identifiable by a unique username.
*
* @author Greg Norris
*/

public class IMCommunity
{
Map Community;

public IMCommunity(Map t)
{
Community = new HashMap();
Community.putAll(t);
}

public void addUser(String username, MessageSystemUser user)
{
Community.put(username, user);
}

public MessageSystemUser getUser(String username)
{
return ((MessageSystemUser)(Community.get(username)));
}

public void deleteUser(String username)
{
Community.remove(username);
}

}

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top