🔒 Closed Hi Phc bot

Status
Not open for further replies.
Certainly! Below is a sample Java code that demonstrates how you can store a user's login and password using a simple data structure like a HashMap:

Java:
import java.util.HashMap;
import java.util.Scanner;

public class UserLoginSystem {
    private HashMap<String, String> userCredentials;
    
    public UserLoginSystem() {
        userCredentials = new HashMap<>();
    }
    
    public void registerUser(String username, String password) {
        userCredentials.put(username, password);
        System.out.println("User registered successfully!");
    }
    
    public boolean loginUser(String username, String password) {
        if (userCredentials.containsKey(username)) {
            if (userCredentials.get(username).equals(password)) {
                System.out.println("Login successful!");
                return true;
            }
        }
        System.out.println("Invalid username or password.");
        return false;
    }
    
    public static void main(String[] args) {
        UserLoginSystem loginSystem = new UserLoginSystem();
        
        // Register a user
        loginSystem.registerUser("john", "pass123");
        
        // Prompt user to login
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter username: ");
        String username = scanner.nextLine();
        System.out.print("Enter password: ");
        String password = scanner.nextLine();
        
        // Login user
        loginSystem.loginUser(username, password);
        
        scanner.close();
    }
}

In this code, we have a class UserLoginSystem which holds a HashMap called userCredentials. The registerUser method allows you to register a new user by storing their username and password in the userCredentials map. The loginUser method checks if the provided username and password match the stored values.

In the main method, we create an instance of UserLoginSystem and register a user with the username "john" and password "pass123". Then, the program prompts the user to enter their username and password, and attempts to log them in using the loginUser method.

Please note that this is a simplified example and not intended for real-world use. In practice, it is advisable to use more secure methods for storing passwords, such as hashing and salting.
 
Status
Not open for further replies.

About this Thread

  • 2
    Replies
  • 294
    Views
  • 1
    Participants
Last reply from:
Kuromi

Online now

Members online
1,227
Guests online
5,727
Total visitors
6,954

Forum statistics

Threads
2,350,733
Posts
29,368,415
Members
1,087,835
Latest member
manutd112233
Back
Top