PageRenderTime 117ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/src/main/java/agenda/model/base/Contact.java

https://bitbucket.org/sebinsteanu/dsir1862
Java | 142 lines | 110 code | 26 blank | 6 comment | 21 complexity | 16dda25b50836cc73bd634db267fd371 MD5 | raw file
  1. package agenda.model.base;
  2. import agenda.exceptions.InvalidFormatException;
  3. import java.util.regex.Matcher;
  4. import java.util.regex.Pattern;
  5. public class Contact {
  6. private String Name;
  7. private String Address;
  8. private String Telefon;
  9. private String Email;
  10. public static final Pattern VALID_EMAIL_ADDRESS_REGEX = Pattern.compile("^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,6}$", Pattern.CASE_INSENSITIVE);
  11. public static final Pattern VALID_PHONE_REGEX = Pattern.compile("^[\\+0][\\d]{5,10}$", Pattern.CASE_INSENSITIVE);
  12. public static final Pattern VALID_NAME_REGEX = Pattern.compile("^([\\s]|[^0-9]|-){3,50}$", Pattern.CASE_INSENSITIVE);
  13. public Contact(){
  14. Name = "";
  15. Address = "";
  16. Telefon = "";
  17. Email = "";
  18. }
  19. public Contact(String name, String address, String telefon, String email) throws InvalidFormatException{
  20. if (!validName(name)) throw new InvalidFormatException("Cannot convert", "Invalid name");
  21. if (!validAddress(address)) throw new InvalidFormatException("Cannot convert", "Invalid address");
  22. if (!validTelefon(telefon)) throw new InvalidFormatException("Cannot convert", "Invalid phone number");
  23. if (!validEmail(email)) throw new InvalidFormatException("Cannot convert", "Invalid email address");
  24. Name = name;
  25. Address = address;
  26. Telefon = telefon;
  27. Email = email;
  28. }
  29. public static Contact fromString(String str, String delim) throws InvalidFormatException
  30. {
  31. String[] s = str.split(delim);
  32. if (s.length!=4) throw new InvalidFormatException("Cannot convert", "Invalid data");
  33. if (!validName(s[0])) throw new InvalidFormatException("Cannot convert", "Invalid name");
  34. if (!validTelefon(s[2])) throw new InvalidFormatException("Cannot convert", "Invalid phone number");
  35. if (!validAddress(s[1])) throw new InvalidFormatException("Cannot convert", "Invalid address");
  36. return new Contact(s[0], s[1], s[2], s[3]);
  37. }
  38. @Override
  39. public String toString() {
  40. StringBuilder sb = new StringBuilder();
  41. sb.append(Name);
  42. sb.append("#");
  43. sb.append(Address);
  44. sb.append("#");
  45. sb.append(Telefon);
  46. sb.append("#");
  47. sb.append(Email);
  48. sb.append("#");
  49. return sb.toString();
  50. }
  51. private static boolean validName(String str)
  52. {
  53. if(str!="") {
  54. String[] s = str.split("[\\p{Punct}\\s]+");
  55. if (s.length > 3) return false;
  56. }
  57. Matcher matcher = VALID_NAME_REGEX .matcher(str);
  58. return matcher.find();
  59. }
  60. private static boolean validAddress(String str)
  61. {
  62. return (str != null && !str.equals("") && str.length() <= 50);
  63. }
  64. private static boolean validTelefon(String tel)
  65. {
  66. Matcher matcher = VALID_PHONE_REGEX .matcher(tel);
  67. return matcher.find();
  68. //
  69. // String[] s = tel.split("[\\p{Punct}\\s]+");
  70. // if (tel.charAt(0) == '+' && s.length == 2 ) return true;
  71. // if (tel.charAt(0) != '0')return false;
  72. // if (s.length != 1) return false;
  73. // return true;
  74. }
  75. public static boolean validEmail(String emailStr) {
  76. Matcher matcher = VALID_EMAIL_ADDRESS_REGEX .matcher(emailStr);
  77. return matcher.find();
  78. }
  79. @Override
  80. public boolean equals(Object obj) {
  81. if (! (obj instanceof Contact)) return false;
  82. Contact o = (Contact)obj;
  83. if (Name.equals(o.Name) && Address.equals(o.Address) &&
  84. Telefon.equals(o.Telefon) && Email.equals(o.Email))
  85. return true;
  86. return false;
  87. }
  88. public String getName() {
  89. return Name;
  90. }
  91. public void setName(String name) throws InvalidFormatException {
  92. if (!validName(name)) throw new InvalidFormatException("Cannot convert", "Invalid name");
  93. Name = name;
  94. }
  95. public String getAddress() {
  96. return Address;
  97. }
  98. public void setAddress(String address) throws InvalidFormatException {
  99. if (!validAddress(address)) throw new InvalidFormatException("Cannot convert", "Invalid address");
  100. Address = address;
  101. }
  102. public String getTelefon() {
  103. return Telefon;
  104. }
  105. public void setTelefon(String telefon) throws InvalidFormatException {
  106. if (!validTelefon(telefon)) throw new InvalidFormatException("Cannot convert", "Invalid phone number");
  107. Telefon = telefon;
  108. }
  109. public String getEmail() {
  110. return Email;
  111. }
  112. public void setEmail(String email) throws InvalidFormatException{
  113. if(!validEmail(email)) throw new InvalidFormatException("Cannot convert", "Invalid email address");
  114. Email = email;
  115. }
  116. }