1

I'm trying to make a system without MySQL database which will have accounts and accounts' data. But it didn't work

My accounts java file:

/*
* Instead of using 2 different ArrayList (for Username and Password) we used Hashmap
* HashMap stores keys and the keys' values
* In our HashMap keys refer to usernames values refer to passwords
*/
import java.util.HashMap; 
import javax.swing.JOptionPane;

public class Accounts {
    
    //I used static to reach list from another class suggest me another way please
    static HashMap AccountList = new HashMap<String, HashMap<String, String>>();
    
    public static void CreateAccount(String userID, String password, String email, String fullName, String department){
        
        //We have to check that there shouldn't be an account with the same username
        if (AccountList.containsKey(userID)){
            JOptionPane.showMessageDialog(null, "There is an account with that username");
        }
        else{
            HashMap Data = new HashMap<String, String>();
            Data.put("userID", userID);
            Data.put("password", password);
            Data.put("email", email);
            Data.put("fullName", fullName);
            Data.put("department", department);
            AccountList.put(userID, Data);
            JOptionPane.showMessageDialog(null, "Member created successfully");
        }
    }  
}

In my login.java I get an error on that line

boolean IsLoginSuccessful = false;

//We are getting username and password text/string from the form
String userID = username_field.getText();
String password = password_field.getText();

//Now check if username is in list then check if username's password equals to password
if (Accounts.AccountList.containsKey(userID)){
    String userPassword = Accounts.AccountList.get(userID).get("password");
    
    IsLoginSuccessful = true;
}

That second ".get" gives error like "cannot find symbol". Why does it take the hashmap as an object element? Also if you have another way to store these datas without hashtable and check the password etc. Show me please thank you.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
ceazo
  • 11
  • 2
  • Oh, that's mistype but it still gives the same syntax error – ceazo Jan 07 '22 at 23:54
  • 1
    You have raw types. Its `Map accountList = new HashMap<>();` - then it works fine. Not that this is good code, but we all have to start somewhere. – rzwitserloot Jan 08 '22 at 00:08

1 Answers1

0

To get the keys you would do

Set<String> keys = AccountList.keySet();

And you should declare your map as

Map<String, Map<String,String>> accountList = new HashMap<>();
Map<String,String> data = new HashMap<>();

But it would be best if you would declare a user class and put that in the map.

class User {
   private String username;
   private String password;
   ...

   public String getUsername() {
       return username;
   }
   public String getPassword() {
      return password;
   }
}

Map<String, User>  accountList = new HashMap<>();

You could then use defined getters (e.g. user.getPassword(), user.getUsername()) to retrieve the information.

Then it might be used as follows:

String username = get_from_console.
User user = accountList.get(username);
String password = user.getPassword();
WJS
  • 36,363
  • 4
  • 24
  • 39