Advertisement
Guest User

Untitled

a guest
Oct 4th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.15 KB | None | 0 0
  1. public static void main(String [] strings){
  2.  
  3. ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
  4. Validator validator = validatorFactory.getValidator();
  5.  
  6. Set<ConstraintViolation<User>> constraints = validator.validate(new User("Stas","StasPassword","Stas.Stanis88@gmail.com"));
  7.  
  8. for (ConstraintViolation<User> constraint:constraints) {
  9.  
  10. System.out.println(constraint.getMessage());
  11.  
  12. }
  13.  
  14. public class User {
  15.  
  16. public String getFirstName() {
  17. return firstName;
  18. }
  19.  
  20. private void setFirstName(String firstName) {
  21. this.firstName = firstName;
  22. }
  23.  
  24. public String getLastName() {
  25. return lastName;
  26. }
  27.  
  28. private void setLastName(String lastName) {
  29. this.lastName = lastName;
  30. }
  31.  
  32. public String getPassword() {
  33. return password;
  34. }
  35.  
  36. private void setPassword(String password) {
  37. this.password = password;
  38. }
  39.  
  40. public String getEmail() {
  41. return email;
  42. }
  43.  
  44. private void setEmail(String email) {
  45. this.email = email;
  46. }
  47.  
  48. public String getCustomerType() {
  49. return customerType;
  50. }
  51.  
  52. private void setCustomerType(String customerType) {
  53. this.customerType = customerType;
  54. }
  55.  
  56. public String getNickName() {
  57. return nickName;
  58. }
  59.  
  60. private void setNickName(String nickName) {
  61. this.nickName = nickName;
  62. }
  63.  
  64. public User(){}
  65.  
  66. public User(String nickName,String password, String email){
  67.  
  68. setEmail(email);
  69. setNickName(nickName);
  70. setPassword(password);
  71. }
  72.  
  73. public User(String firstName, String lastName, String nickName ,String email, String password, String customerType) {
  74.  
  75. setCustomerType(customerType);
  76. setEmail(email);
  77. setFirstName(firstName);
  78. setLastName(lastName);
  79. setNickName(nickName);
  80. setPassword(password);
  81. }
  82.  
  83. @Override
  84. public String toString() {
  85. return "User{" +
  86. "firstName='" + firstName + ''' +
  87. ", lastName='" + lastName + ''' +
  88. ", password='" + password + ''' +
  89. ", email='" + email + ''' +
  90. ", customerType='" + customerType + ''' +
  91. ", nickName='" + nickName + ''' +
  92. '}';
  93. }
  94.  
  95. @Override
  96. public boolean equals(Object o) {
  97. if (this == o) return true;
  98. if (o == null || getClass() != o.getClass()) return false;
  99.  
  100. User user = (User) o;
  101.  
  102. if (!getFirstName().equals(user.getFirstName())) return false;
  103. if (!getLastName().equals(user.getLastName())) return false;
  104. if (!getPassword().equals(user.getPassword())) return false;
  105. if (!getEmail().equals(user.getEmail())) return false;
  106. if (!getCustomerType().equals(user.getCustomerType())) return false;
  107. return getNickName().equals(user.getNickName());
  108.  
  109. }
  110.  
  111. @Override
  112. public int hashCode() {
  113. 107: int result = getFirstName().hashCode(); // Проблемма возникает здесь!
  114. result = 31 * result + getLastName().hashCode();
  115. result = 31 * result + getPassword().hashCode();
  116. result = 31 * result + getEmail().hashCode();
  117. result = 31 * result + getCustomerType().hashCode();
  118. result = 31 * result + getNickName().hashCode();
  119. return result;
  120. }
  121.  
  122. @NotEmpty(message = "This field can't be empty")
  123. @NotBlank(message = "This field can't be empty")
  124. @Length(min = 0,max = 20,message = "The length of this field can be only 0-20")
  125. private String firstName;
  126.  
  127. @NotEmpty(message = "This field can't be empty")
  128. @NotBlank(message = "This field can't be empty")
  129. @Length(min = 0,max = 20,message = "The length of this field can be only 0-20")
  130. private String lastName;
  131.  
  132. @NotEmpty(message = "This field can't be empty")
  133. @NotBlank(message = "This field can't be empty")
  134. @Length(min = 8,max = 16,message = "Wrong password length: length of this field can be only 8-16")
  135. private String password;
  136.  
  137. @NotEmpty(message = "This field can't be empty")
  138. @NotBlank(message = "This field can't be empty")
  139. @Email(message = "Wrong format for email field!" , regexp = "[A-Za-z.0-9]+@[A-Za-z.]+\.[a-z]{2,4}")
  140. private String email;
  141.  
  142. @NotEmpty(message = "This field can't be empty")
  143. @NotBlank(message = "This field can't be empty")
  144. private String customerType;
  145.  
  146. @NotEmpty(message = "This field can't be empty")
  147. @NotBlank(message = "This field can't be empty")
  148. @Length(min = 4,max = 10,message = "Wrong nickName: The length of this field can be only between 4-10")
  149. private String nickName;
  150.  
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement