Created
August 26, 2018 17:39
-
-
Save ThakurPriyanka/37b932160dedce1097e55313e589d5da to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.knoldus.user; | |
import javax.validation.ConstraintViolation; | |
import javax.validation.Validation; | |
import javax.validation.Validator; | |
import javax.validation.ValidatorFactory; | |
import java.util.Scanner; | |
import java.util.Set; | |
public class Login { | |
public static void main(String[] args) { | |
Scanner sc = new Scanner(System.in); | |
System.out.println("Please enter the username"); | |
String username = sc.nextLine(); | |
System.out.println("Please enter the password"); | |
String password = sc.nextLine(); | |
try { | |
ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); | |
Validator validator = factory.getValidator(); | |
User newUser = User.builder() | |
.username(username) | |
.password(password) | |
.build(); | |
Set<ConstraintViolation<User>> violations = validator.validate(newUser); | |
for (ConstraintViolation<User> violation : violations) { | |
if (!("".equals(violation.getMessage()))) { | |
throw new RuntimeException(violation.getMessage()); | |
} | |
} | |
System.out.println("User created"); | |
System.out.println(newUser.getUsername() + " Created\n"); | |
} catch (Exception ex) { | |
System.out.println(ex.getMessage()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment