Home > Blockchain >  Java hasmaps for login system
Java hasmaps for login system

Time:08-16

im creating a basic banking system using java swing and im using hashmaps to store login information the login system works perfectly but im wondereing if i can add account cash balances for each user(key) in the hashmap below is my id and passwords class im just wondering if there is anything i can add to achieve the result i want.


public class IdandPasswords {

    // create hashmap
    HashMap<String,String> logininfo = new HashMap<String,String>();
    
    IdandPasswords() {
        logininfo.put("admin", "admin");
        
    }
    
    
    protected HashMap getlogininfo() {
        return logininfo;
    }
    public void setidpass(String pass , String ID) {
        this.logininfo.put(pass, ID);
    }

}

CodePudding user response:

Define a class

When you have defined pieces of data that represent attributes of an entity, create a class to hold that data.

If the main purpose of a class is to communicate data transparently and immutably, define your class as a record. In a record, by default, you merely declare the name and type of each member field. The compiler implicitly creates constructor, getters, equals & hashCode, and toString.

record User ( String userName , String fullName , String password ) {}

Usage:

User u = new User ( "scott" , "Scott Brown" , "tiger" ) ;

Keep an object of this type around as a global variable, to know who is the current user. Generally we want to avoid global variable, but tracking the current user is a legitimate use.

Track users by name

As commented by rogue, collect those users by their username in a map if you need to look them up by name.

Map < String , User > usernameToUser = new HashMap<>() ;
…
usernameToUser.put( u.userName , u ) ;

Track balance per user

Similarly, to keep account balance per user, make another map.

Map < User , BigDecimal > userToBalance = new HashMap<>() ;
…
userToBalance.put( u , new BigDecimal( "0.0" ) ) ;
…
BigDecimal balance = userToBalance.get( u ) ;

Also as commented, in real work we would not be so cavalier with passwords. We would minimize the storage, viewing, and handling of passwords. But that is an entirely separate, lengthy discussion.

CodePudding user response:

public class IdandPasswords {
    
        HashMap<String,UserDetail> userInfo = new HashMap<String,UserDetail>();
        
        IdandPasswords() {
            UserDetail userDetail = new UserDetail("admin", "admin", 100.0);
            
            // IF YOU ARE USING "userName" AS THE KEY
            userInfo.put("admin", userDetail);
            
        }    
        
        class UserDetail {
            String userName;
            String pass;
            Double cashBalance;
            
            UserDetail(String userName, String pass, Double cashBalance){
                this.userName = userName;
                this.pass = pass;
                this.cashBalance = cashBalance;
            
            }
               
        }
    
    }
  • Related