File size: 1,408 Bytes
4465cb6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
package com.example.service;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

/**
 * Manages user accounts: lookup, registration, and password updates.
 */
public class UserService {

    private final Map<String, String> usersByEmail = new HashMap<>();

    public Optional<String> findUserIdByEmail(String email) {
        if (email == null || email.isBlank()) {
            return Optional.empty();
        }
        String normalized = email.trim().toLowerCase();
        return Optional.ofNullable(usersByEmail.get(normalized));
    }

    public boolean registerUser(String email, String userId) {
        if (email == null || userId == null || email.isBlank() || userId.isBlank()) {
            return false;
        }
        String normalized = email.trim().toLowerCase();
        if (usersByEmail.containsKey(normalized)) {
            return false;
        }
        usersByEmail.put(normalized, userId);
        return true;
    }

    public boolean updatePassword(String email, String newPasswordHash) {
        if (email == null || newPasswordHash == null || newPasswordHash.isBlank()) {
            return false;
        }
        String normalized = email.trim().toLowerCase();
        if (!usersByEmail.containsKey(normalized)) {
            return false;
        }
        // In a real app this would persist to a credential store.
        return true;
    }
}