JavaFX Как бросить сообщение об исключении в метку? - PullRequest
0 голосов
/ 26 марта 2020

Я пишу складское приложение и сначала написал код, который будет отображаться в консоли. Теперь я хочу отобразить его как окно в javafx. Как я могу выбросить его на ярлык javaFX? Ниже приведен код, который у меня есть в качестве валидаторов, и теперь я хочу добавить его в javafx. У меня также есть пользовательский сервис, который отправляет логин и пароль и проверяет его с помощью валидаторов. Я работаю в сценографе. Может быть, у кого-нибудь есть более простое решение для проверки логина, пароля и выдачи информации о неправильной длине?

Validator

public class UserValidator {

private UserDao userDao = UserDaoImpl.getInstance();

private static UserValidator instance = null;

public static UserValidator getInstance() {
    if(instance == null) {
        instance = new UserValidator();
    }
    return instance;
}

private final int LOGIN_MIN_LENGTH = 5;
private final int LOGIN_MAX_LENGTH = 20;
private final int PASSWORD_MIN_LENGTH = 8;
private final int PASSWORD_MAX_LENGTH = 20;

public boolean isValidateAddUser(User user) throws UserLoginIsExistException, UserLoginEnoughLengthException, UserPasswordEnoughLengthException,
        UserPasswordIsOneCharUpCaseException {
    if(isLoginEnoughLength(user.getLogin()))
        throw new UserLoginEnoughLengthException("Login must be minimum 5 and maximum 20 letters!");

        if(isPasswordEnoughLength(user.getPassword()))
            throw new UserPasswordEnoughLengthException("Password must be minimum 8 and maximum 20 letters!");

        if(isPasswordOneCharUpperCase(user.getPassword()))
            throw new UserPasswordIsOneCharUpCaseException("Password must have one uppercase letter!");

        if (isUserAlreadyExist(user.getLogin()))
            throw new UserLoginIsExistException("Login is exist!");

    return true;
}

public boolean isValidateUpdateUserPassword(String newPassword) throws UserPasswordEnoughLengthException,
        UserPasswordIsOneCharUpCaseException{

    if(isPasswordEnoughLength(newPassword)) {
        throw new UserPasswordEnoughLengthException("Password must be minimum 8 and maximum 20 letters!");
    }

    if(isPasswordOneCharUpperCase(newPassword)) {
        throw new UserPasswordIsOneCharUpCaseException("Password must have one uppercase letter!");
    }
    return true;
}

private boolean isLoginEnoughLength(String login) {
    return login.length() < LOGIN_MIN_LENGTH || login.length() > LOGIN_MAX_LENGTH;
}

private boolean isPasswordEnoughLength(String password) {
    return password.length() < PASSWORD_MIN_LENGTH || password.length() > PASSWORD_MAX_LENGTH;
}

private boolean isPasswordOneCharUpperCase(String password) {
    for(char checkUpperCase : password.toCharArray()) {
        if(Character.isUpperCase(checkUpperCase)) {
            return false;
        }
    }
    return true;
}

private boolean isUserAlreadyExist(String login) {
    List<User> users = null;
    users = userDao.getAllUsers();
    for(User user : users) {
        if(user.getLogin().equals(login)) {
            return true;
        }
    }
    return false;
}

}

UI

public class UserImpl implements UserService {

private UserValidator userValidator = UserValidator.getInstance();
private UserDao userDao = UserDaoImpl.getInstance();

private static UserImpl instance = null;

public static UserImpl getInstance() {
    if (instance == null) {
        instance = new UserImpl();
    }
    return instance;
}

public boolean addUser(User user) {
    try {
        if (userValidator.isValidateAddUser(user)) {
            userDao.addUser(user);
            return true;
        }
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
    return false;
}

Фасад пользовательского интерфейса

public class UserFacadeImpl implements UserFacade {
private static UserService userService = UserImpl.getInstance();
private static UserFacadeImpl instance = null;

public static UserFacadeImpl getInstance() {
    if(instance == null) {
        instance = new UserFacadeImpl();
    }
    return instance;
}

public boolean registerUser(User user) {
    return userService.addUser(user);
}

Контроллер регистра

public class RegisterControler {
String login, password, email;
private static UserFacade userFacade = UserFacadeImpl.getInstance();

@FXML
TextField fieldLogin;
@FXML
TextField fieldPassword;
@FXML
TextField fieldEmail;
@FXML
Label labelValidator;

public boolean isRegister()  {
    login = fieldLogin.getText();
    password = fieldPassword.getText();
    email = fieldEmail.getText();
    User user = new User(login, password, email);

    if (userFacade.registerUser(user)) {
            labelValidator.setText("Register Successfully!");
        }
    else {

    }

    return false;
    }


public void buttonRegister() {
    isRegister();
}
...