-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHelloClient.java
146 lines (136 loc) · 7.01 KB
/
HelloClient.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.rmi.Naming;
import java.rmi.RemoteException;
import java.util.Scanner;
public class HelloClient {
public static void main(String[] argv) throws RemoteException {
Scanner scanner = new Scanner(System.in);
HelloInterface server = null;
int retries = 3; // Number of retries
int attempt = 0;
while (attempt < retries) {
try {
// Try to connect to the server
server = (HelloInterface) Naming.lookup("//localhost:5001/Hello");
System.out.println("Connected to the server successfully!");
break; // If connection is successful, exit the loop
} catch (Exception e) {
attempt++;
System.out.println("Failed to connect to the server. Attempt " + attempt + " of " + retries);
if (attempt >= retries) {
System.out.println("Max retries reached. Exiting...");
return; // Exit the client if max retries are reached
}
try {
Thread.sleep(2000); // Wait 2 seconds before retrying
} catch (InterruptedException ex) {
System.out.println("Retry interrupted");
return;
}
}
}
// If server connection is established, proceed with the operations
if (server != null) {
while (true) {
System.out.println("1. Add User");
System.out.println("2. Delete User");
System.out.println("3. Get User Details");
System.out.println("4. List Users");
System.out.println("5. Update User");
System.out.println("6. Shutdown Server");
System.out.println("7. Exit");
System.out.print("Choose an option: ");
int choice = scanner.nextInt();
scanner.nextLine(); // Consume newline
switch (choice) {
case 1 -> {
System.out.print("Enter firstname: ");
String firstname = scanner.nextLine();
System.out.print("Enter lastname: ");
String lastname = scanner.nextLine();
System.out.print("Enter birthdate: ");
String birthdate = scanner.nextLine();
System.out.print("Enter salary: ");
double salary = scanner.nextDouble();
scanner.nextLine(); // Consume newline
System.out.print("Enter gender (MALE/FEMALE/OTHER): ");
User.Gender gender = User.Gender.valueOf(scanner.nextLine().toUpperCase());
System.out.print("Enter division: ");
String division = scanner.nextLine();
System.out.print("Enter position: ");
String position = scanner.nextLine();
User user = new User(firstname, lastname, birthdate, salary, gender, division, position);
server.addUser(user);
System.out.println("User added.");
}
case 2 -> {
System.out.print("Enter firstname: ");
String firstname = scanner.nextLine();
System.out.print("Enter lastname: ");
String lastname = scanner.nextLine();
if (server.deleteUser(firstname, lastname)) {
System.out.println("User deleted.");
} else {
System.out.println("User not found.");
}
}
case 3 -> {
System.out.print("Enter firstname: ");
String firstname = scanner.nextLine();
System.out.print("Enter lastname: ");
String lastname = scanner.nextLine();
User user = server.getUser(firstname, lastname);
if (user != null) {
System.out.println(user);
} else {
System.out.println("User not found.");
}
}
case 4 -> {
server.listUsers().forEach(System.out::println);
}
case 5 -> {
System.out.print("Enter firstname of user to update: ");
String firstname = scanner.nextLine();
System.out.print("Enter lastname of user to update: ");
String lastname = scanner.nextLine();
System.out.print("Enter new firstname: ");
String newFirstname = scanner.nextLine();
System.out.print("Enter new lastname: ");
String newLastname = scanner.nextLine();
System.out.print("Enter new birthdate: ");
String newBirthdate = scanner.nextLine();
System.out.print("Enter new salary: ");
double newSalary = scanner.nextDouble();
scanner.nextLine(); // Consume newline
System.out.print("Enter new gender (MALE/FEMALE/OTHER): ");
User.Gender newGender = User.Gender.valueOf(scanner.nextLine().toUpperCase());
System.out.print("Enter new division: ");
String newDivision = scanner.nextLine();
System.out.print("Enter new position: ");
String newPosition = scanner.nextLine();
User updatedUser = new User(newFirstname, newLastname, newBirthdate, newSalary, newGender, newDivision, newPosition);
if (server.updateUser(firstname, lastname, updatedUser)) {
System.out.println("User updated.");
} else {
System.out.println("User not found.");
}
}
case 6 -> {
try {
server.shutdown(); // Send shutdown request to the server
System.out.println("Server is shutting down...");
return; // Exit client after shutting down server
} catch (RemoteException e) {
System.out.println("Error shutting down server: " + e.getMessage());
}
}
case 7 -> {
System.out.println("Goodbye!");
return; // Exit client
}
default -> System.out.println("Invalid option.");
}
}
}
}
}