Advertisement
Guest User

Untitled

a guest
Feb 9th, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.69 KB | None | 0 0
  1. package øving2;
  2. import java.util.Date;
  3.  
  4. public class Person {
  5. private String name;
  6. private String email;
  7. private char gender;
  8. private Date birthday;
  9.  
  10. private boolean isAlpha(String s) {
  11. for (int i = 0; i < s.length(); i++) {
  12. if (!(Character.isLetter(s.charAt(i)))) {
  13. return false;
  14. }
  15. }
  16. return true;
  17. }
  18.  
  19. private boolean isCountry(String tld) {
  20. return (tld.length() == 2 || tld.length() == 3);
  21. }
  22.  
  23. public String getName() {
  24. return name;
  25. }
  26.  
  27. public void setName(String name) {
  28. String[] names = name.split("\\s+");
  29. if (names.length != 2) {
  30. throw new IllegalArgumentException("Please input both your first and your last name, and remove middle names.");
  31. }
  32. else if (!((isAlpha(names[0])) && (isAlpha(names[1])))) {
  33. throw new IllegalArgumentException("Your name may only contain letters.");
  34. }
  35. else if (names[0].length() < 2 || names[1].length() < 2) {
  36. throw new IllegalArgumentException("Your names have to be longer than 2 chars.");
  37. }
  38. else {
  39. this.name = name;
  40. }
  41. }
  42.  
  43. public String getEmail() {
  44. return email;
  45. }
  46.  
  47. public void setEmail(String email) {
  48. String[] emailSplit = email.split("@");
  49. String[] domain;
  50. int numOfSubDomains;
  51.  
  52. String[] emailNames;
  53.  
  54. try {
  55. domain = emailSplit[1].split("\\.");
  56. numOfSubDomains = domain.length;
  57.  
  58. emailNames = emailSplit[0].split("\\.");
  59. }
  60. catch (Exception e) {
  61. throw new IllegalArgumentException("Names or email invalid. Wat.");
  62. }
  63.  
  64. String name = getName();
  65. String firstName = name.split("\\s+")[0].toLowerCase();
  66. String lastName = name.split("\\s+")[0].toLowerCase();
  67.  
  68. if (emailSplit.length != 2 && !(emailSplit.length == 0)) {
  69. throw new IllegalArgumentException("Please make sure you typed in your email address correctly.");
  70. }
  71. else if (!(isCountry(domain[numOfSubDomains-1])) || !(isAlpha(domain[numOfSubDomains-1]))) {
  72. throw new IllegalArgumentException("Please use a valid country code.");
  73. }
  74. else if (!(emailNames[0].toLowerCase().equals(firstName)) && !(emailNames[1].toLowerCase().equals(lastName))) {
  75. throw new IllegalStateException("Invalid name or email.");
  76. }
  77. else {
  78. this.email = email;
  79. }
  80. }
  81.  
  82. public Date getBirthday() {
  83. return birthday;
  84. }
  85.  
  86. public void setBirthday(Date birthday) {
  87. Date now = new Date();
  88. if (birthday.before(now)) {
  89. this.birthday = birthday;
  90. }
  91. else {
  92. throw new IllegalArgumentException("Invalid date");
  93. }
  94. }
  95.  
  96. public char getGender() {
  97. return gender;
  98. }
  99.  
  100. public void setGender(char gender) {
  101. if (gender == 'M' || gender == 'F' || gender == '\0') {
  102. this.gender = gender;
  103. }
  104. else {
  105. throw new IllegalArgumentException("Gender is either [m]ale or [f]emale.");
  106. }
  107. }
  108.  
  109. public static void main(String[] args) {
  110. Person p = new Person();
  111. @SuppressWarnings("deprecation")
  112. Date d = new Date(93,05,10);
  113.  
  114. p.setName("Ola Nordmann");
  115. p.setGender('M');
  116. p.setEmail("ola.nordmann@ntnu.no");
  117. p.setBirthday(d);
  118.  
  119. System.out.println(p.getName());
  120. System.out.println(p.getGender());
  121. System.out.println(p.getEmail());
  122. System.out.println(p.getBirthday());
  123.  
  124. }
  125.  
  126.  
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement