Memer Memer - 6 months ago 9
Java Question

Making a profile and storing it in a map for each user

Hello I was wondering if anyone could help me with my little problem, When i add a user using AddUser(Username1) it adds the user, but the next user i add with a diffrent username (username2) overrides the current profile causing there to be 2 profiles with the username of username2.
so instead of returning username1 and username2 it returns, username2 and username2.

(bot of these classes are simplified, the user class would ofc hold other info like id, balance ect..)

My User class:

public class TakaneUser {
String Username;

public TakaneUser(String user1){
this.Username = user1
}
public static String getUserName() {
return Username;
}


My Userhandler / adding users to map class:

static Map<String, TakaneUser> Usermap = new HashMap();
public static void adduser(String user){
Usermap.put(user, new TakaneUser(user));
}

Answer

In your code change the following:

private String Username;

And instead of using static use the following:

public Map<String, TakaneUser> Usermap = new HashMap();