package com.hzs.common.core.utils;

/**
 * @Description: 自动生成密码 暂时先不使用
 * @Author: sui q
 * @Time: 2022/9/9 14:52
 * @Classname: CreatePassword
 * @PackageName: com.hzs.common.core.utils
 */
public class CreatePassword {

    /**
    * @Description: 生成密码
     * @param len 密码长度
    * @return: String
    * @Author: sui q
    * @Date: 2022/9/9 14:53
    */
    public static String generatePassword(int len) {
        // 1、定义基本字符串baseStr和出参password
        StringBuilder password = null;
        String baseStr = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ~!@#$%^&*()_+{}|<>?";
        boolean flag = false;
        // 2、使用循环来判断是否是正确的密码
        while (!flag) {
            // 密码重置
            password = new StringBuilder();
            // 个数计数
            int a = 0, b = 0, c = 0, d = 0;
            for (int i = 0; i < len; i++) {
                int rand = (int) (Math.random() * baseStr.length());
                password.append(baseStr.charAt(rand));
                if (rand < 10) {
                    a++;
                }
                if (10 <= rand && rand < 36) {
                    b++;
                }
                if (36 <= rand && rand < 62) {
                    c++;
                }
                if (62 <= rand) {
                    d++;
                }
                if (a * b * c * d != 0) {
                    break;
                }
            }
            // 是否是正确的密码（4类中仅一类为0，其他不为0）
            flag = (a * b * c != 0 && d == 0) || (a * b * d != 0 && c == 0) || (a * c * d != 0 && b == 0)
                    || (b * c * d != 0 && a == 0);
        }
        return password.toString();
    }

    public static void main(String[] args) {
        System.out.println(generatePassword(6));
    }
}
