-
Notifications
You must be signed in to change notification settings - Fork 0
/
Account.java
146 lines (122 loc) · 5.4 KB
/
Account.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
137
138
139
140
141
142
143
144
145
146
import java.util.HashMap;
import java.util.Arrays;
import java.io.File;
import java.lang.StringBuilder;
public class Account {
private String first, last, email, username;
private int id; // user id
private String password; // hexadecimal representation of hashed bytes of String password
private static HashMap<Integer, Account> idToAcc = new HashMap<Integer, Account>();
private static HashMap<String, String> loginInfo = new HashMap<String, String>();
private static HashMap<String, Integer> userToID = new HashMap<String, Integer>();
// constructor
public Account(String first, String last, String email, String username, String password) {
this.first = first;
this.last = last;
this.email = email;
this.username = username;
this.password = password;
this.id = idToAcc.size(); // assign unique id to this account
}
// creates new account
public static void create(String f, String l, String e, String u, String p) {
// check if arguments are allowed
try {
checkProfanity(f);
checkProfanity(l);
checkProfanity(u);
checkUserValidity(u);
checkEmailValidity(e);
checkPasswordLength(p);
}
catch (IllegalArgumentException x) {
System.out.println("Error.");
}
String pass = Encrypt.encrypt(p);
Account newUser = new Account(f, l, e, u, pass);
idToAcc.put(newUser.id, newUser);
loginInfo.put(newUser.email, newUser.password);
userToID.put(newUser.username, newUser.id);
}
// throws error if username contains profanity
private static void checkProfanity(String m) {
if (m.contains("Fuck") || m.contains("fuck") || m.contains("Shit") || m.contains("shit") || m.contains("Bitch") || m.contains("bitch")
|| m.contains("Cunt") || m.contains("cunt") || m.contains("Nigger") || m.contains("nigger") || m.contains("Ass") || m.contains("ass")
|| m.contains("Dick") || m.contains("dick") || m.contains("Vagina") || m.contains("vagina") || m.contains("Nigga") || m.contains("nigga"))
throw new IllegalArgumentException("Please pick an appropriate username.");
}
// throws error if not a valid email
private static void checkEmailValidity(String e) {
// check if username and email are available
if (!loginInfo.isEmpty()) {
if (loginInfo.containsKey(e))
throw new IllegalArgumentException("Email already taken!");
}
}
private static void checkPasswordLength(String p) {
if (p.length() < 7)
throw new IllegalArgumentException("Password must be over 6 characters!");
}
// throws error if username contains non-valid characters
private static void checkUserValidity(String m) {
if (m.contains("~") || m.contains("!") || m.contains("@") || m.contains("#") || m.contains("$") || m.contains("%")
|| m.contains("^") || m.contains("&") || m.contains("*") || m.contains("(") || m.contains(")") || m.contains("-")
|| m.contains("+") || m.contains("=") || m.contains("[") || m.contains("]") || m.contains("{") || m.contains("}")
|| m.contains("?") || m.contains("/") || m.contains(";") || m.contains(":") || m.contains("<") || m.contains(">")
|| m.contains(","))
throw new IllegalArgumentException("Please pick an appropriate username.");
if (m.length() >= 16)
throw new IllegalArgumentException("Username must be under 16 characters!");
if (m.length() <= 3)
throw new IllegalArgumentException("Username must be at least 4 characters!");
if (userToID.get(m) != null)
throw new IllegalArgumentException("Username is taken!");
}
// change email
public static void changeEmail(int accID, String newEmail) {
Account acc = idToAcc.get(accID);
loginInfo.remove(acc.email);
acc.email = newEmail;
loginInfo.put(newEmail, acc.password);
idToAcc.put(acc.id, acc);
}
// change username
public static void changeUsername(int accID, String newUser) {
Account acc = idToAcc.get(accID);
userToID.remove(acc.username);
acc.username = newUser;
userToID.put(newUser, acc.id);
idToAcc.put(acc.id, acc);
}
// change password
public static void changePassword(int accID, String newPass) {
Account acc = idToAcc.get(accID);
acc.password = Encrypt.encrypt(newPass);
loginInfo.put(acc.email, acc.password);
idToAcc.put(acc.id, acc);
}
public static String showUser(int accID) {
Account acc = idToAcc.get(accID);
return acc.username;
}
public static String showFirstName(int accID) {
Account acc = idToAcc.get(accID);
return acc.first;
}
public static String showLastName(int accID) {
Account acc = idToAcc.get(accID);
return acc.last;
}
public static String showPass(int accID) {
Account acc = idToAcc.get(accID);
return acc.password;
}
public static String showEmail(int accID) {
Account acc = idToAcc.get(accID);
return acc.email;
}
public static int showID(int accID) {
Account acc = idToAcc.get(accID);
return acc.id;
}
}