-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAccountManager.java
More file actions
205 lines (192 loc) · 5.16 KB
/
AccountManager.java
File metadata and controls
205 lines (192 loc) · 5.16 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
200
201
202
203
204
205
import java.util.Hashtable;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JOptionPane;
/**
* This class stores all of the UserProfile objects, and checks for validity.
*
* @author Jorah Hinman, Phil Gore, Chandra Adhikari
*
*/
public class AccountManager {
private Hashtable<String, UserProfile> userAccounts;
/**
* creates new AccountManager object
*/
public AccountManager() {
userAccounts = new Hashtable<String, UserProfile>();
}
public void put(UserProfile account) {
if(account == null) return;
userAccounts.put(account.getUserName(), account);
}
public UserProfile get(String username) {
return userAccounts.get(username);
}
public UserProfile delete(String username) {
return userAccounts.remove(username);
}
/**
* Returns 'true' if username is found in AccountManager
* @param username - String
* @return found - boolean
*/
public boolean containsUsername(String userName){
if(userName == null){
return false;
}
return userAccounts.containsKey(userName);
}
/**
* Returns 'true' if username is a valid username. Returns 'false' is username is
* invalid.
*
* @param userName - String
* @return valid - boolean
*/
public boolean verifyUserName(String userName){
if(!containsUsername(userName)){
return false;
}
if(userName.length() > 12){
return false;
}
if(userName.trim() == "") {
return false;
}
return true;
}
/**
* Returns 'true' if username is a valid new username. Returns 'false' is username is
* invalid.
*
* @param userName - String
* @return valid - boolean
*/
private boolean validNewUserName(String userName){
if(containsUsername(userName)){
JOptionPane.showMessageDialog(null, "Username Taken", null, JOptionPane.PLAIN_MESSAGE);
return false;
}
if(userName.length() > 12){
JOptionPane.showMessageDialog(null, "Invalid Username", null, JOptionPane.PLAIN_MESSAGE);
return false;
}
if(userName.trim() == "") {
return false;
}
return true;
}
/**
* Retuns true if email is valid. Returns false if email is invalid.
* @param email - String
* @return valid - boolean
*/
public boolean verifyEmailFormat(String email){
if (email == null) {
return false;
} else if (email.trim() == "") {
return false;
}
Pattern p = Pattern.compile("^[^@](.+)@(.+)$");
Matcher m = p.matcher(email);
if (!m.find()) {
return false;
}
return true;
}
/**
* Return true is password entered matches password associated with the
* provided username
* @param userName - String
* @param password - String
* @return
*/
public boolean verifyPassword(String userName, String password) {
if(userName == null || password == null){
return false;
}
if (!validPassword(password)) {
return false;
}
if (!this.containsUsername(userName)) {
return false;
}
String storedPassword = userAccounts.get(userName).getPassword();
return storedPassword != null && storedPassword.equals(password);
}
/**
* Return true if the password passes all the necessary tests.
*
* @param password - String
*
* @return
*/
public boolean validPassword(String password) {
if (password.trim() == "") {
return false;
}
if (password.length() < 6) {
return false;
}
if (!password.matches(".*\\d+.*")) {
System.out.println("Returning False, no digit.");
System.out.println(password);
return false;
}
Pattern p = Pattern.compile("[^a-z0-9 ]", Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(password);
if (!m.find()) {
System.out.println("Returning False.");
return false;
}
return true;
}
/**
* Returns true if profile can be created. Returns false if profile cannot be
* created.
*
* @param user - UserProfile
* @return canCreate - boolean
*/
public boolean verifyCreateProfile(UserProfile user, String username){
if (user == null) {
return false;
}
if (!username.equals(user.getUserName())) {
if(!validNewUserName(user.getUserName())){
return false;
}
}
if(!verifyEmailFormat(user.getEmail())){
JOptionPane.showMessageDialog(null, "Invalid Email", null, JOptionPane.PLAIN_MESSAGE);
return false;
}
if(!validPassword(user.getPassword())){
JOptionPane.showMessageDialog(null, "Invalid Password must be at least 6 characters long and contain a number"
+ " and a special character", null, JOptionPane.PLAIN_MESSAGE);
return false;
}
return true;
}
/**
* Returns true if username exists in account manager and password matches the
* password saved to that username. Returns false if username doesn't exist in
* account manager or password doesn't match.
* @param userName
* @param password
* @return
*/
public boolean verifyLogin(String userName, String password){
if(!verifyUserName(userName)){
return false;
}
if(!verifyPassword(userName, password)){
return false;
}
if (userAccounts.containsKey(userName) && userAccounts.get(userName).getUserName().equals(userName)) {
return true;
}
return false;
}
}