-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUser.java
More file actions
116 lines (112 loc) · 3.43 KB
/
Copy pathUser.java
File metadata and controls
116 lines (112 loc) · 3.43 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
import java.util.ArrayList;
public class User {
private String username;
private String password;
private String email;
private String first;
private String last;
private double funds;
private ArrayList<String> reviews;
private double stars;
private ArrayList<Post> posts;
public User(String u, String p, String e, String f, String l, ArrayList<String> r, double s, ArrayList<Post> posts) {
this.username = u;
this.password = p;
this.email = e;
this.first = f;
this.last = l;
this.funds = checkFunds();
this.reviews = r;
this.stars = s;
this.posts = posts == null ? new ArrayList<Post>() : posts;
}
private String[] getCredentials() {
String[] creds = {username, email};
return creds;
}
public boolean createAccount(String u, String p, String e, String f, String l) {
if(isValidUsername(u) && isValidPassword(p) && isValidEmail(e) && f != null && l != null)
DataBase.addUser(new User(u,p,e,f,l,null,0.0, null));
else
return false;
return true;
}
public boolean isValidUsername(String u) {
if(u != null && u.length() > 5) {
for(char c : u.toCharArray()) {
if(c <= 32 || c > 125)
return false;
}
if(!DataBase.containsUsername(u))
return true;
}
return false;
}
public boolean isValidPassword(String p) {
if(p != null && p.length() > 5) {
boolean spec, cap, num;
spec = cap = num = false;
for(char c : p.toCharArray()) {
if(c <= 32 || c > 125)
return false;
if((c >= 33 && c <= 47) || (c >= 58 && c <= 64) || (c >= 91 && c <= 96))
spec = true;
if(c >= 65 && c <= 90)
cap = true;
if(c >= 48 && c <= 57)
num = true;
}
if(spec && cap && num)
return true;
}
return false;
}
public enum Email {
GMAIL,
ICLOUD,
OUTLOOK,
YAHOO
}
public boolean isValidEmail(String e) {
boolean domain = false;
for(Email i : Email.values()) {
if(i.equals(e)) {
domain = true;
break;
}
}
if(domain && !DataBase.containsEmail(e))
return Mailer.verifyEmail(e);
}
public void review(double stars, String feedback) {
this.stars = (this.stars * this.reviews.size() + stars) / (this.reviews.size() + 1);
this.reviews.add(feedback);
}
public void addPost(Post p) {
this.posts.add(p);
}
public double withdraw(double amount) {
this.funds -= amount;
return amount;
}
public void deposit(double amount) {
funds += amount;
}
public double checkFunds() {
// TODO figure ts out
return funds;
}
public boolean equals(User other) {
if(other == null)
return false;
String[] creds = getCredentials();
String[] otherCreds = other.getCredentials();
for(int i = 0; i < creds.length; ++i)
if(!creds[i].equals(otherCreds[i]))
return false;
return true;
}
public boolean checkPassword(String attempt) {
return attempt.equals(this.password);
}
}