-
Notifications
You must be signed in to change notification settings - Fork 0
/
User.java
136 lines (115 loc) · 4 KB
/
User.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
* for the Vehicle Rental System TCSS 305 - Rentz
*/
package model;
import java.util.Objects;
/**
* Represents a single user for registration or sign-in. User is an immutable object.
*
* Constructors and methods of this class throw NullPointerException if required parameters are
* null.
*
* @author hphobbs
* @version Winter 2020
*/
public final class User {
/** Represent the user's user name. */
private String myName;
/** Represent the user's password. */
private String myPassword;
/** Represent the user's VIP status. */
private boolean myVIPStatus;
/**
* This parameterized constructor is called when an User object is created only with user
* name and password. VIPStatus default value is false
*
* @param theName entered user name
* @param thePassword entered password
*/
public User(final String theName, final String thePassword) {
this(theName, thePassword, false); // call into the next constructor to use the logic
}
/**
* Parameterized constructor with user name, password and VIPStatus. It throws exception is
* name or password is empty
*
* @param theName entered user name
* @param thePassword entered password
* @param theVIPStatus entered VIP status
* @throws NullPointerException for null parameters
* @throws IllegalArgumentException();
*/
public User(final String theName, final String thePassword, final Boolean theVIPStatus) {
myName = Objects.requireNonNull(theName);
myPassword = Objects.requireNonNull(thePassword);
myVIPStatus = Objects.requireNonNull(theVIPStatus);
if (theName.isEmpty() || thePassword.isEmpty()) { // isEmpty(): returns true if the
// string length is 0
throw new IllegalArgumentException();
}
}
/** @return the user name. */
public String getMyName() {
return myName;
}
/** @return the password. */
public String getMyPassword() {
return myPassword;
}
/** @return the VIPStatus. */
public Boolean getMyVIPStatus() {
return myVIPStatus;
}
@Override
/**
* @return String representation of user object
*/
public String toString() {
final StringBuilder sb = new StringBuilder();
final String comma = ", ";
sb.append(getClass().getSimpleName());
sb.append("(");
sb.append(this.getMyName());
sb.append(comma);
sb.append(this.getMyPassword());
sb.append(comma);
sb.append(this.getMyVIPStatus());
sb.append(")");
return sb.toString();
}
@Override
/**
* check the state of the two objects to see if they're equal return true if they match,
* false if they don't
*
* @param theOtherObject another user
* @return if user is equal to other user
*/
public boolean equals(final Object theOtherObject) { // the parameter cannot be other than
// object type
// one return statement instead of three
boolean result = false;
if (this == theOtherObject) {
result = true;
} else if (theOtherObject == null) {
result = false;
} else if (this.getClass() != theOtherObject.getClass()) {
result = false;
} else {
final User other = (User) theOtherObject;
result = Objects.equals(myName, other.myName)
&& Objects.equals(myPassword, other.myPassword)
&& Objects.equals(myVIPStatus, other.myVIPStatus);
}
return result;
}
/**
* Create the hash code to get the user name, password and VIPStatus.
*
* @return hash code of the user
*/
@Override
public int hashCode() {
return Objects.hash(myName, myPassword, myVIPStatus);
}
}