-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContact.java
More file actions
111 lines (91 loc) · 3.14 KB
/
Copy pathContact.java
File metadata and controls
111 lines (91 loc) · 3.14 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
package m3;
public class Contact {
static final int NAME_NUMBER_ID_LENGTH = 10;
static final int ADDRESS_LENGTH = 30;
private String contactId;
private String firstName;
private String lastName;
private String phoneNumber;
private String address;
private static boolean validateNameNumberId(String nameNumberId) {
// method to validate name, phone number, or ID meet requirements
if (nameNumberId == null) { // requirement: ID cannot be null
return false;
} else if (nameNumberId.length() > NAME_NUMBER_ID_LENGTH) { // requirement: ID cannot exceed this length
return false;
} else {
return true;
}
}
private static boolean validateAddress(String address) {
// method to validate address meets requirements
if (address == null) { // requirement: address cannot be null
return false;
} else if (address.length() > ADDRESS_LENGTH) { // requirement: address cannot exceed this length
return false;
} else {
return true;
}
}
public Contact(String contactId, String firstName, String lastName, String phoneNumber, String address) {
if (validateNameNumberId(contactId)) {
this.contactId = contactId;
} else {
throw new IllegalArgumentException("Contact ID must not be null and must be no longer than " + NAME_NUMBER_ID_LENGTH + " characters");
}
this.setFirstName(firstName);
this.setLastName(lastName);
this.setPhoneNumber(phoneNumber);
this.setAddress(address);
}
public String getContactId() {
return contactId;
}
public String setContactId(String contactId) {
throw new IllegalAccessError("Contact ID cannot be changed"); // requirement: ID cannot be changed
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
// validates and sets first name
if (validateNameNumberId(firstName)) {
this.firstName = firstName;
} else {
throw new IllegalArgumentException("First name must not be null and must be no longer than " + NAME_NUMBER_ID_LENGTH + " characters");
}
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
// validates and sets last name
if (validateNameNumberId(lastName)) {
this.lastName = lastName;
} else {
throw new IllegalArgumentException("Last name must not be null and must be no longer than " + NAME_NUMBER_ID_LENGTH + " characters");
}
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
// validates and sets phone number
if (validateNameNumberId(phoneNumber)) {
this.phoneNumber = phoneNumber;
} else {
throw new IllegalArgumentException("Phone number must not be null and must be no longer than " + NAME_NUMBER_ID_LENGTH + " characters");
}
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
// validates and sets address
if (validateAddress(address)) {
this.address = address;
} else {
throw new IllegalArgumentException("Address must not be null and must be no longer than " + ADDRESS_LENGTH + " characters");
}
}
}