-
Notifications
You must be signed in to change notification settings - Fork 1
/
UserLoginSystem.java
47 lines (40 loc) · 1.57 KB
/
UserLoginSystem.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import java.util.*;
public class UserLoginSystem {
private static Map<String, String> users = new HashMap<>(); // Map to store username-password pairs
// Method to register a new user
public static void registerUser(String username, String password) {
users.put(username, password);
System.out.println("User registered successfully!");
}
// Method to check if a username exists
public static boolean usernameExists(String username) {
return users.containsKey(username);
}
// Method to validate user login
public static boolean validateLogin(String username, String password) {
if (users.containsKey(username)) {
String storedPassword = users.get(username);
return storedPassword.equals(password);
} else {
return false;
}
}
public static void main(String[] args) {
// Register some users (for demonstration purposes)
registerUser("user1", "password1");
registerUser("user2", "password2");
// User login process
Scanner scanner = new Scanner(System.in);
System.out.println("Enter username:");
String username = scanner.nextLine();
System.out.println("Enter password:");
String password = scanner.nextLine();
// Validate login
if (validateLogin(username, password)) {
System.out.println("Login successful!");
} else {
System.out.println("Invalid username or password.");
}
scanner.close();
}
}