package com.seeyon.apps.src_hxinvoice.util;

import java.util.HashSet;
import java.util.Random;
import java.util.Set;

public class FourDigitUniqueIdGenerator {
    private static final int ID_LENGTH = 4;
//    private static final String CHARACTERS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    private static final String CHARACTERS = "0123456789";
    private static final Random random = new Random();
    private static final Set<String> generatedIds = new HashSet<>();

    public static String generateId() {
        String id;
        do {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < ID_LENGTH; i++) {
                int index = random.nextInt(CHARACTERS.length());
                sb.append(CHARACTERS.charAt(index));
            }
            id = sb.toString();
        } while (generatedIds.contains(id));
        generatedIds.add(id);
        return id;
    }

    public static void main(String[] args) {
        // 生成 10 个 4 位随机不重复值进行测试
        for (int i = 0; i < 100; i++) {
            String uniqueId = generateId();
            System.out.println("Generated ID: " + uniqueId);
        }
    }
}