-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUser.java
More file actions
132 lines (106 loc) · 5.14 KB
/
User.java
File metadata and controls
132 lines (106 loc) · 5.14 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
package com.bankApp;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;
public class User {
private String username; // используется и как имя, и как логин
private String hashPassword;
private double balance;
private int moons;
private Role role;
private String atUser;
private String avatar;
private String description;
private String status;
private String gender;
private String email;
private final Set<User> friends = new HashSet<>();
private final List<Gift> receivedGifts = new ArrayList<>();
private final List<Post> posts = new ArrayList<>();
private final List<PetPair> petPairs = new ArrayList<>();
public User(String username, String password, Role role) {
this.username = username;
this.hashPassword = hash(password);
this.role = role;
this.atUser = "@" + username;
this.balance = 0;
this.moons = 0;
}
public User(String username, String hashPassword, double balance, int moons, Role role, boolean alreadyHashed) {
this.username = username;
this.hashPassword = alreadyHashed ? hashPassword : hash(hashPassword);
this.role = role;
this.atUser = "@" + username;
this.balance = balance;
this.moons = moons;
}
public static String hash(String password) {
try {
MessageDigest md = MessageDigest.getInstance("SHA-256");
byte[] bytes = md.digest(password.getBytes());
StringBuilder sb = new StringBuilder();
for (byte b : bytes) sb.append(String.format("%02x", b));
return sb.toString();
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}
public boolean checkPassword(String password) {
return hash(password).equals(this.hashPassword);
}
// ---------------- Банковские методы ----------------
public boolean deposit(double amount) {
if (amount <= 0) return false;
balance += amount;
return true;
}
public boolean withdraw(double amount) {
if (amount <= 0 || amount > balance) return false;
balance -= amount;
return true;
}
public boolean transfer(User to, double amount) {
if (to == null || amount <= 0 || this == to || balance < amount) return false;
if (!withdraw(amount)) return false;
return to.deposit(amount);
}
public int getMoons() { return moons; }
public void addMoons(int amount) { if(amount>0) moons+=amount; }
public boolean removeMoons(int amount) { if(amount>0 && moons>=amount){moons-=amount; return true;} return false; }
// ---------------- Социальные методы ----------------
public boolean addFriend(User friend) { return friend != null && friend != this && friends.add(friend); }
public boolean removeFriend(User friend) { return friends.remove(friend); }
public Set<User> getFriends() { return Collections.unmodifiableSet(friends); }
public User findFriendByAtUser(String atUser) {
for(User f : friends) if(f.getAtUser().equalsIgnoreCase(atUser)) return f;
return null;
}
public void receiveGift(Gift gift) { if(gift!=null) receivedGifts.add(gift); }
public List<Gift> getGifts() { return Collections.unmodifiableList(receivedGifts); }
public List<Gift> getGiftsBySender(String sender) {
List<Gift> result = new ArrayList<>();
for(Gift g : receivedGifts) if(g.getFrom()!=null && g.getFrom().equalsIgnoreCase(sender)) result.add(g);
return result;
}
public void addPost(Post post) { if(post!=null) posts.add(post); }
public List<Post> getPosts() { return Collections.unmodifiableList(posts); }
public void addPetPair(PetPair pair) { if(pair!=null) petPairs.add(pair); }
public List<PetPair> getPetPairs() { return Collections.unmodifiableList(petPairs); }
// ---------------- Геттеры и сеттеры ----------------
public String getUsername() { return username; }
public String getAtUser() { return atUser; }
public void setAtUser(String atUser) { if(atUser!=null && !atUser.isEmpty()) this.atUser = atUser; }
public String getAvatar() { return avatar; }
public void setAvatar(String avatar) { this.avatar = avatar; }
public String getDescription() { return description; }
public void setDescription(String description) { this.description = description; }
public String getStatus() { return status; }
public void setStatus(String status) { this.status = status; }
public String getGender() { return gender; }
public void setGender(String gender) { this.gender = gender; }
public String getEmail() { return email; }
public void setEmail(String email) { if(email!=null && !email.isEmpty()) this.email=email; }
public double getBalance() { return balance; }
public Role getRole() { return role; }
public String getHashPassword() { return hashPassword; }
}