- Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathPerson.java
More file actions
Latest commit
executable file
·118 lines (97 loc) · 2.73 KB
/
Copy pathPerson.java
File metadata and controls
executable file
·118 lines (97 loc) · 2.73 KB
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
packagetutorialquestions.questiond363.beforefixingandrefactoring;
importjava.util.StringTokenizer;
publicclassPerson {
privatefinalStringforenames;
privatefinalStringsurname;
privatefinalintdayOfBirth;
privatefinalintmonthOfBirth;
privatefinalintyearOfBirth;
privatefinalinthouseNumber;
privatefinalStringaddress1;
privatefinalStringaddress2;
privatefinalStringpostCode;
privatefinalStringnationalInsuranceNumber;
publicPerson(Stringforename, Stringsurname,
intdayOfBirth,
intmonthOfBirth,
intyearOfBirth,
inthouseNumber,
Stringaddress1,
Stringaddress2,
StringpostCode,
StringnationalInsuranceNumber) {
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;
}
publicbooleanisDateOfBirthValid() {
if (yearOfBirth < 0) {
returnfalse;
}
if (monthOfBirth < 1 || yearOfBirth > 12) {
returnfalse;
}
if (dayOfBirth < 1) {
returnfalse;
}
switch (monthOfBirth) {
case1:
case3:
case5:
case7:
case8:
case10:
case12:
if (dayOfBirth > 31) {
returnfalse;
}
break;
case4:
case6:
case9:
case11:
if (dayOfBirth > 30) {
returnfalse;
}
break;
default:
assertmonthOfBirth == 2;
if (dayOfBirth > (isLeapYear() ? 29 : 28)) {
returnfalse;
}
}
returntrue;
}
publicbooleansameAddress(Personother) {
returnhouseNumber == other.houseNumber
&& address1.equals(other.address1)
&& address2.equals(other.address2)
&& postCode.equals(other.postCode);
}
publicStringgetInitials() {
finalStringBuilderresult = newStringBuilder();
StringTokenizerstrTok = newStringTokenizer(forenames);
while (strTok.hasMoreTokens()) {
result.append(strTok.nextToken().charAt(0));
}
returnresult.toString() + surname.charAt(0);
}
privatebooleanisLeapYear() {
// Deliberately simplified version of
// leap year calculation
return (yearOfBirth % 4 == 0);
}
publicStringtoString() {
return"Name: " + forenames + " " + surname + "\n"
+ "DOB: " + dayOfBirth + "/" + monthOfBirth + "/" + yearOfBirth + "\n"
+ "Address: " + houseNumber + " " + address1 + ", " + address2 + ", " + postCode + "\n"
+ "NI: " + nationalInsuranceNumber;
}
}