-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.java
More file actions
85 lines (75 loc) · 2.16 KB
/
Copy pathServer.java
File metadata and controls
85 lines (75 loc) · 2.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
import java.util.ArrayList;
import java.util.HashMap;
public class Server
{
private final HashMap<String, Integer> userCredentials;
private final ArrayList<User> userList;
public Server() {
userCredentials = new HashMap<>();
userList = new ArrayList<>();
}
public void addUserMap(User user)
{
userCredentials.put(user.getName(), user.getPassword());
}
public void addUserArray(User user)
{
userList.add(user);
}
public String leaderboard()
{
String x = "";
for(User e : userList)
{
x += e.getName() + " " + e.getWins() + "\n";
}
return x;
}
public int findMaxIndex(int min, int max)
{
int maxIndex = min;
for (int i = min; i < userList.size(); i++)
{
if(userList.get(i).getWins() > userList.get(maxIndex).getWins())
{
maxIndex = i;
}
}
return maxIndex;
}
public void swap(int a, int b)
{
User temp;
temp = userList.get(a);
userList.set(a, userList.get(b));
userList.set(b, temp);
}
public void sort()
{
for(int i = 0; i < userList.size(); i++)
{
int maxIndex = findMaxIndex(i, userList.size());
if(userList.get(i).getWins() != userList.get(maxIndex).getWins())
{
swap(i, maxIndex);
}
}
}
public User validateLogin(String username, int password) {
if (userCredentials.containsKey(username) && userCredentials.get(username) == password) {
return new User(username, password); // Create a new User object for simplicity
}
return null;
}
public void updateUser(User updatedUser) {
// Update the user in the map
userCredentials.put(updatedUser.getName(), updatedUser.getPassword());
// Update the user in the array/list
for (int i = 0; i < userList.size(); i++) {
if (userList.get(i).getName().equals(updatedUser.getName())) {
userList.set(i, updatedUser);
break;
}
}
}
}