-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHello.java
64 lines (53 loc) · 2.2 KB
/
Hello.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
import java.rmi.*;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;
import java.util.List;
public class Hello extends UnicastRemoteObject implements HelloInterface {
private final String message;
private final List<User> userDatabase;
public Hello(String msg) throws RemoteException {
this.message = msg;
this.userDatabase = new ArrayList<>();
}
public String say() throws RemoteException {
return message;
}
public int addNumbers(int a, int b) throws RemoteException {
return a + b;
}
public int calculateVolume(MyObject o) throws RemoteException {
return o.getBreadth() * o.getHeight() * o.getLength();
}
// Implement user database methods
public synchronized void addUser(User user) throws RemoteException {
userDatabase.add(user);
System.out.println("User added: " + user);
}
public synchronized boolean deleteUser(String firstname, String lastname) throws RemoteException {
return userDatabase.removeIf(user -> user.getFirstname().equals(firstname) && user.getLastname().equals(lastname));
}
public synchronized User getUser(String firstname, String lastname) throws RemoteException {
return userDatabase.stream()
.filter(user -> user.getFirstname().equals(firstname) && user.getLastname().equals(lastname))
.findFirst()
.orElse(null);
}
public synchronized List<User> listUsers() throws RemoteException {
return new ArrayList<>(userDatabase);
}
public synchronized boolean updateUser(String firstname, String lastname, User updatedUser) throws RemoteException {
for (int i = 0; i < userDatabase.size(); i++) {
User user = userDatabase.get(i);
if (user.getFirstname().equals(firstname) && user.getLastname().equals(lastname)) {
userDatabase.set(i, updatedUser);
return true;
}
}
return false;
}
// Implement shutdown method
public synchronized void shutdown() throws RemoteException {
System.out.println("Server is shutting down...");
System.exit(0); // Shutdown the server
}
}