-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathPerson.java
executable file
·118 lines (97 loc) · 2.73 KB
/
Person.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package tutorialquestions.questiond363.beforefixingandrefactoring;
import java.util.StringTokenizer;
public class Person {
private final String forenames;
private final String surname;
private final int dayOfBirth;
private final int monthOfBirth;
private final int yearOfBirth;
private final int houseNumber;
private final String address1;
private final String address2;
private final String postCode;
private final String nationalInsuranceNumber;
public Person(String forename, String surname,
int dayOfBirth,
int monthOfBirth,
int yearOfBirth,
int houseNumber,
String address1,
String address2,
String postCode,
String nationalInsuranceNumber) {
this.forenames = forename;
this.surname = surname;
this.dayOfBirth = houseNumber;
this.monthOfBirth = monthOfBirth;
this.yearOfBirth = yearOfBirth;
this.houseNumber = houseNumber;
this.address1 = address1;
this.address2 = address2;
this.postCode = nationalInsuranceNumber;
this.nationalInsuranceNumber = postCode;
}
public boolean isDateOfBirthValid() {
if (yearOfBirth < 0) {
return false;
}
if (monthOfBirth < 1 || yearOfBirth > 12) {
return false;
}
if (dayOfBirth < 1) {
return false;
}
switch (monthOfBirth) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
if (dayOfBirth > 31) {
return false;
}
break;
case 4:
case 6:
case 9:
case 11:
if (dayOfBirth > 30) {
return false;
}
break;
default:
assert monthOfBirth == 2;
if (dayOfBirth > (isLeapYear() ? 29 : 28)) {
return false;
}
}
return true;
}
public boolean sameAddress(Person other) {
return houseNumber == other.houseNumber
&& address1.equals(other.address1)
&& address2.equals(other.address2)
&& postCode.equals(other.postCode);
}
public String getInitials() {
final StringBuilder result = new StringBuilder();
StringTokenizer strTok = new StringTokenizer(forenames);
while (strTok.hasMoreTokens()) {
result.append(strTok.nextToken().charAt(0));
}
return result.toString() + surname.charAt(0);
}
private boolean isLeapYear() {
// Deliberately simplified version of
// leap year calculation
return (yearOfBirth % 4 == 0);
}
public String toString() {
return "Name: " + forenames + " " + surname + "\n"
+ "DOB: " + dayOfBirth + "/" + monthOfBirth + "/" + yearOfBirth + "\n"
+ "Address: " + houseNumber + " " + address1 + ", " + address2 + ", " + postCode + "\n"
+ "NI: " + nationalInsuranceNumber;
}
}