-
Notifications
You must be signed in to change notification settings - Fork 0
/
Contact.java
117 lines (102 loc) · 3.2 KB
/
Contact.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
//*********************************************************************
// File: Contact.java
// Name: David France
// Date: 7/31/23
// Description: Contact class for holding task ID, first name, last name,
// address, and phone number. To be used with TaskService class.
public class Contact {
private String contactId;
private String firstName;
private String lastName;
private String phoneNumber;
private String address;
/**
* Constructor - unique id is passed from ContactService. Parameters are
* validated before successful construction
* @param id - String
* @param firstName - String
* @param lastName - String
* @param phone - String
* @param address - String
*/
public Contact(String id, String firstName, String lastName, String phone, String address) {
if (checkString(id, 10)) {
throw new IllegalArgumentException("Id too long.");
}
if (checkString(firstName, 10)) {
throw new IllegalArgumentException("Invalid first name");
}
if (checkString(lastName, 10)) {
throw new IllegalArgumentException("Invalid last name");
}
if (checkPhone(phone)) {
throw new IllegalArgumentException("Invalid phone number");
}
if (checkString(address, 30)) {
throw new IllegalArgumentException("Invalid address");
}
contactId = id;
this.firstName = firstName;
this.lastName = lastName;
phoneNumber = phone;
this.address = address;
}
// Getters and setters. No setter for Id means it can't be updated.
public String getId() {
return this.contactId;
}
public void setFirstName(String firstName) {
if (checkString(firstName, 10)) {
throw new IllegalArgumentException("Invalid first name");
}
this.firstName = firstName;
}
public String getFirstName() {
return this.firstName;
}
public void setLastName(String lastName) {
if (checkString(lastName, 10)) {
throw new IllegalArgumentException("Invalid last name");
}
this.lastName = lastName;
}
public String getLastName() {
return this.lastName;
}
public void setPhoneNumber(String number) {
if (checkPhone(number)) {
throw new IllegalArgumentException("Invalid phone number");
}
phoneNumber = number;
}
public String getPhoneNumber() {
return this.phoneNumber;
}
public void setAddress(String address) {
if (checkString(address, 30)) {
throw new IllegalArgumentException("Invalid first name");
}
this.address = address;
}
public String getAddress() {
return this.address;
}
// Check if a string is null or greater than desired length.
public boolean checkString(String string, int length) {
if (string == null ||string.length() > length) {return true;}
return false;
}
// Checks that phone length isn't null, is exactly 10 characters, and is
// only digits.
public boolean checkPhone(String phoneNumber) {
if(phoneNumber == null || phoneNumber.length() != 10) {return true;}
// Method for checking numeric from Baeldung -
// url: https://www.baeldung.com/java-check-string-number
for(int i = 0; i < 10; ++i) {
if (!Character.isDigit(phoneNumber.charAt(i))) {
return true;
}
}
return false;
}
}