-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUser.java
More file actions
199 lines (173 loc) · 7.05 KB
/
User.java
File metadata and controls
199 lines (173 loc) · 7.05 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/** Class representing a user in a social network. Users are meant to be connected in pairs with links.
* @author Billy Barbaro
*/
public class User implements SocialNetworkObject {
private String id;
private String firstName;
private String middleName;
private String lastName;
private String email;
private String phoneNumber;
/** Constructor that creates a new invalid user */
public User() {
super();
}
/** Verifies that a string can be used to set a field
* @param check the string to be added to a field
* @param type a string specifying the method that called this. Customizes the exception message
* @throws NullPointerException called if the string is null
* @throws UninitializedObjectExcpetion called if the user is not valid
*/
private void verify(String check, String type) throws UninitializedObjectException{
SocialNetworkUtility.checkNull(check, type);
SocialNetworkUtility.checkValid(this, "User", type);
}
/** Sets a user's ID. Can only be done once and the ID may not be an empty string
* @param id a String representing the user's id
* @throws NullPointerException called if the id is being set to null
* @return boolean tells whether or not the user's ID was set
*/
public boolean setID(String id) throws NullPointerException {
SocialNetworkUtility.checkNull(id, "ID");
// Here we handle the cases in which the user is not permitted to set an ID.
if (this.isValid() || id.equals("")) {
return false;
}
this.id = id;
return true;
}
/** Gets the ID of the user.
* @return String The user's ID. If it has not yet been set, it is null.
*/
public String getID() {
return id;
}
/** Sets a user's first name. May not be an empty string. User must have an ID
* @param name a String representing the user's first name
* @throws NullPointerException called if first name is being set to null
* @throws UninitializedObjectExcpetion called if the user is not valid
* @return User the user who's first name was set.
*/
public User setFirstName(String name) throws UninitializedObjectException {
this.verify(name, "First Name");
this.firstName = name;
return this;
}
/** Gets the first name of the user.
* @return String The user's first name. If it has not yet been set, it is null.
*/
public String getFirstName() {
return firstName;
}
/** Sets a user's middle name. May not be an empty string. User must have an ID
* @param name a String representing the user's middle name
* @throws NullPointerException called if middle name is being set to null
* @throws UninitializedObjectExcpetion called if the user is not valid
* @return User the user who's middle name was set.
*/
public User setMiddleName(String name) throws UninitializedObjectException {
this.verify(name, "Middle Name");
this.middleName = name;
return this;
}
/** Gets the middle name of the user.
* @return String The user's middle name. If it has not yet been set, it is null.
*/
public String getMiddleName() {
return middleName;
}
/** Sets a user's last name. May not be an empty string. User must have an ID
* @param name a String representing the user's last name
* @throws NullPointerException called if last name is being set to null
* @throws UninitializedObjectExcpetion called if the user is not valid
* @return User the user who's last name was set.
*/
public User setLastName(String name) throws UninitializedObjectException {
this.verify(name, "Last Name");
this.lastName = name;
return this;
}
/** Gets the last name of the user.
* @return String The user's last name. If it has not yet been set, it is null.
*/
public String getLastName() {
return lastName;
}
/** Sets a user's email. May not be an empty string. User must have an ID
* @param email a String representing the user's email
* @throws NullPointerException called if email is being set to null
* @throws UninitializedObjectExcpetion called if the user is not valid
* @return User the user who's email was set.
*/
public User setEmail(String email) throws UninitializedObjectException{
this.verify(email, "Email");
this.email = email;
return this;
}
/** Gets the email of the user.
* @return String The user's emails. If it has not yet been set, it is null.
*/
public String getEmail() {
return email;
}
/** Sets a user's phone number. May not be an empty string. User must have an ID
* @param number a String representing the user's phone number
* @throws NullPointerException called if phoneNumber is being set to null
* @throws UninitializedObjectExcpetion called if the user is not valid
* @return User the user who's phone number was set.
*/
public User setPhoneNumber(String number) throws UninitializedObjectException{
this.verify(number, "Phone Number");
this.phoneNumber = number;
return this;
}
/** Gets the phone number of the user.
* @return String The user's phone number. If it has not yet been set, it is null.
*/
public String getPhoneNumber() {
return phoneNumber;
}
/** Tells if a user is valid
* @return boolean True if the user is valid. False if it is invalid.
*/
public boolean isValid() {
return id != null;
}
/** Gives a readable representation of the user
* @return String Tells user's information. If invalid, string says "Invlaid User"
*/
@Override
public String toString() {
String userString = new String();
// If the user is valid, we construct a string displaying their information
if (this.isValid())
userString = String.format("User: %s First Name: %s Middle Name: %s Last Name: %s Email: %s Phone Number: %s", this.getID(), this.getFirstName(), this.getMiddleName(), this.getLastName(), this.getEmail(), this.getPhoneNumber());
else
userString = "Invalid User: Uninitialized ID";
return userString;
}
/** Compares two users by their IDs
* @param o an object to have its equality tested with this User.
* @return boolean tells whether or not object is equal to the user.
*/
@Override
public boolean equals(Object o) {
// The object must be a User to be equal to a User
if (!(o instanceof User))
return false;
// Cast the object into a user so we can get its ID
User otherUser = (User)o;
String otherID = otherUser.getID();
// We first check that this user's ID is not null. The overloading of the && operator will prevent us from calling .equals on a null pointer.
return this.isValid() && this.getID().equals(otherID);
}
/** Generates a hash for the user based off ID so they can be stored in a HashSet
* @return int the hash for the user
*/
@Override
public int hashCode() {
if(!this.isValid())
return 0;
return this.getID().hashCode();
}
}