Advertisement
Guest User

Untitled

a guest
Mar 2nd, 2017
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. public class User {
  2.  
  3. private static final Pattern VALID_EMAIL_ADDRESS_REGEX =
  4. Pattern.compile("^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,6}$", Pattern.CASE_INSENSITIVE);
  5. private static final Pattern VALID_PASSWORD_REGEX =
  6. Pattern.compile("(?=.*[0-9])(?=.*[A-Z])(?=\\S+$).{8,}");
  7. // at least one digit,at least one upper case letter, at least 8 characters, no whitespaces
  8.  
  9. private String userName;
  10. private int age;
  11. private String country;
  12. private String email;
  13. private String password;
  14. private TreeMap<String, Show> myFollowing;
  15.  
  16. public User(String userName, int age, String country, String email, String password) {
  17. this.setName(userName);
  18. this.setAge(age);
  19. this.setCountry(country);
  20. this.setEmail(email);
  21. this.setPassword(password);
  22. this.myFollowing = new TreeMap<String, Show>();
  23. }
  24.  
  25.  
  26. private void setName(String userName) {
  27. if(isNullOrEmpty(userName)) {
  28. this.userName = "JhonDoe";
  29. return;
  30. }
  31. this.userName = userName;
  32. }
  33.  
  34. private void setAge(int age) {
  35. if(age < 0 || age > 100) {
  36. this.age = 18;
  37. return;
  38. }
  39. this.age = age;
  40. }
  41.  
  42. private void setCountry(String country) {
  43. if(isNullOrEmpty(country)) {
  44. this.country = "Unknown";
  45. return;
  46. }
  47. this.country = country;
  48. }
  49.  
  50. private void setEmail(String email) {
  51. if(!validateEmail(email)) {
  52. this.email = "johnDoe@abv.bg";
  53. return;
  54. }
  55. this.email = email;
  56. }
  57.  
  58. private void setPassword(String password) {
  59. if(!validatePassword(password)) {
  60. System.out.println("Invalid password, valid one will be assigned and send to your email.");
  61. this.password = "12345678";
  62. return;
  63. }
  64. this.password = password;
  65. }
  66.  
  67. private boolean validateEmail(String email) {
  68. Matcher matcher = VALID_EMAIL_ADDRESS_REGEX.matcher(email);
  69. return matcher.find();
  70. }
  71.  
  72. private boolean validatePassword(String password) {
  73. Matcher matcher = VALID_PASSWORD_REGEX.matcher(password);
  74. return matcher.find();
  75. }
  76.  
  77. private boolean isNullOrEmpty(String text) {
  78. return text.isEmpty() || text == null;
  79. }
  80.  
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement