Все требуемые символы согласно условию в пароль добавляются. Что его не устраивает?
package com.javarush.task.task32.task3204;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
/*
Генератор паролей
*/
public class Solution {
public static void main(String[] args) throws IOException {
ByteArrayOutputStream password = getPassword();
System.out.println(password.toString());
}
public static ByteArrayOutputStream getPassword() throws IOException {
byte numMaxCount = 6;
byte lowerCaseCharMaxCount = 6;
byte upperCaseCharMaxCount = 6;
byte totalCount = 0;
ByteArrayOutputStream pass = new ByteArrayOutputStream();
String test = "";
while(totalCount < 8) {
int type = (int) (Math.random() * 3);
switch (type) {
case 0 :
if (numMaxCount > 0) {
char next = (char) (Math.random() * 10 + 48);
String nextStr = "" + next;
test +=nextStr;
pass.write(nextStr.getBytes());
numMaxCount--;
totalCount++;
}
break;
case 1 :
if (lowerCaseCharMaxCount > 0) {
char next = (char) (Math.random() * 26 + 97);
String nextStr = "" + next;
test +=nextStr;
pass.write(nextStr.getBytes());
lowerCaseCharMaxCount--;
totalCount++;
}
break;
case 2 :
if (upperCaseCharMaxCount > 0) {
char next = (char) (Math.random() * 26 + 65);
String nextStr = "" + next;
test +=nextStr;
pass.write(nextStr.getBytes());
upperCaseCharMaxCount--;
totalCount++;
}
break;
}
}
// System.out.println(test);
return pass;
}
}