-
Notifications
You must be signed in to change notification settings - Fork 0
/
Client.java
323 lines (289 loc) · 11 KB
/
Client.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
import java.io.*;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Scanner;
/**
* A program Team Project
*
* <p>Purdue University -- CS18000 -- Spring 2024 -- Team Project-- </p>
*
* @author Purdue CS
* @version Mar 31, 2024
*/
public class Client {
private static final String HOST = "localhost";
private static final int PORT = 8081;
private static Socket socket;
private static PrintWriter out;
private static BufferedReader in;
private static Scanner scanner;
static String loggedInUser;
public Client() {
}
public static void main(String[] args) {
try {
socket = new Socket(HOST, PORT);
out = new PrintWriter(socket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
scanner = new Scanner(System.in);
System.out.println("Connected to server.");
showMenu();
} catch (IOException e) {
System.err.println("Error connecting to server: " + e.getMessage());
e.printStackTrace();
}
}
private static void showMenu() {
String choice;
do {
System.out.println("\n=== Login ===");
System.out.println("1. Register");
System.out.println("2. Login");
System.out.println("0. Exit");
System.out.print("Enter your choice: ");
choice = scanner.nextLine();
handleChoice(choice);
} while (!choice.equals("0"));
try {
socket.close();
out.close();
in.close();
} catch (IOException e) {
System.err.println("Error closing resources: " + e.getMessage());
e.printStackTrace();
}
}
private static void handleChoice(String choice) {
try {
switch (choice) {
case "1":
register();
break;
case "2":
login();
break;
case "0":
System.out.println("Exiting...");
break;
default:
System.out.println("Invalid choice.");
}
} catch (IOException | PostIncompleteException e) {
System.err.println("Error handling choice: " + e.getMessage());
e.printStackTrace();
}
}
private static void register() throws IOException {
while (true) {
System.out.println("Enter username: ");
String username = scanner.nextLine();
System.out.println("Enter password: ");
String password = scanner.nextLine();
out.println("REGISTER");
out.println(username);
out.println(password);
String response = in.readLine();
if (response != null && response.equals("Registration successful!")) {
System.out.println(response);
createProfile(username);
break;
} else {
System.out.println("Username already exists. Please try again.");
}
}
}
private static void login() throws IOException, PostIncompleteException {
System.out.println("Enter username: ");
String username = scanner.nextLine();
System.out.println("Enter password: ");
String password = scanner.nextLine();
out.println("LOGIN");
out.println(username);
out.println(password);
String response = in.readLine();
if (response.equals("true")) {
loggedInUser = username;
System.out.println("Login successful!");
showSocialMediaMenu(); // Directly call the method to show the social media menu
} else {
System.out.println("Login failed. Please try again.");
showMenu(); // If login fails, show the login menu again
}
}
private static void handleSearchUserResponse(String response) {
System.out.println(response);
}
private static void showSocialMediaMenu() throws IOException,
PostIncompleteException {
String choice;
do {
System.out.println("\n=== Social Media Platform Menu ===");
System.out.println("1. View News Feed");
System.out.println("2. Create Post");
System.out.println("3. Comment on Post");
System.out.println("4. Search User");
System.out.println("5. Upvote Post");
System.out.println("6. Downvote Post");
System.out.println("0. Logout");
System.out.print("Enter your choice: ");
choice = scanner.nextLine();
handleSocialMediaChoice(choice);
} while (!choice.equals("0")); // Loop until user chooses to logout
}
static void handleSocialMediaChoice(String choice) throws IOException,
PostIncompleteException {
switch (choice) {
case "1":
viewNewsFeed();
break;
case "2":
createPost();
break;
case "3":
commentOnPost();
break;
case "4":
String usernameToSearch = scanner.nextLine();
out.println("SEARCH_USER");
out.println(usernameToSearch);
String response = in.readLine();
handleSearchUserResponse(response);
break;
case "5":
upvotePost();
break;
case "6":
downvotePost();
break;
case "0":
System.out.println("Logging out...");
break;
default:
System.out.println("Invalid choice.");
}
}
private static void upvotePost() throws IOException {
System.out.println("Enter the title of the post you want to upvote: ");
String postTitle = scanner.nextLine();
out.println("UPVOTE_POST");
out.println(postTitle);
String response = in.readLine();
System.out.println(response);
}
private static void downvotePost() throws IOException {
System.out.println("Enter the title of the post you want to downvote: ");
String postTitle = scanner.nextLine();
out.println("DOWNVOTE_POST");
out.println(postTitle);
String response = in.readLine();
System.out.println(response);
}
private static void searchUser() throws IOException {
System.out.println("Enter the username to search for: ");
String username = scanner.nextLine();
out.println("SEARCH_USER");
out.println(username);
String response = in.readLine();
System.out.println(response);
}
private static void commentOnPost() throws IOException {
System.out.println("Enter the username whose post you want to comment on: ");
String postAuthor = scanner.nextLine();
System.out.println("Enter the title of the post you want to comment on: ");
String postTitle = scanner.nextLine();
System.out.println("Enter your comment: ");
String comment = scanner.nextLine();
out.println("COMMENT_ON_POST");
out.println(postAuthor);
out.println(postTitle);
out.println(loggedInUser);
out.println(comment);
String response = in.readLine();
System.out.println(response);
}
private static void handleLogout() {
// Logging out the user
if (loggedInUser == null) {
System.out.println("No user is currently logged in.");
} else {
// Close the resources
try {
socket.close();
out.close();
in.close();
System.out.println("User " + loggedInUser + " logged out successfully.");
} catch (IOException e) {
System.out.println("Failed to close resources for " +
loggedInUser + ": " + e.getMessage());
}
loggedInUser = null; // Clear the user session information
}
}
// Inside handleSocialMediaChoice("2") method
private static void createPost() throws IOException, PostIncompleteException {
// Prompt user to enter post details
System.out.println("Enter post title:");
String title = scanner.nextLine();
System.out.println("Enter post content:");
String content = scanner.nextLine();
System.out.println("Enter image URL (press Enter to skip):");
String imageURL = scanner.nextLine();
// Create Post object
Post newPost = new Post(title, content, loggedInUser,
false, imageURL, 0, 0);
// Send post data to server
out.println("CREATE_POST");
out.println(newPost.getTitle());
out.println(newPost.getContent());
out.println(newPost.getAuthor());
out.println(newPost.getImageURL());
}
private static void createProfile(String username) throws IOException {
System.out.println("Enter about:");
String about = scanner.nextLine();
System.out.println("Enter experience:");
String experience = scanner.nextLine();
System.out.println("Enter education:");
String education = scanner.nextLine();
System.out.println("Enter awards:");
String awards = scanner.nextLine();
System.out.println("Enter skills:");
String skills = scanner.nextLine();
System.out.println("Enter status:");
String status = scanner.nextLine();
out.println("CREATE_PROFILE");
out.println(username); // Send the username to associate the profile with the user
out.println(about);
out.println(experience);
out.println(education);
out.println(awards);
out.println(skills);
out.println(status);
String response = in.readLine();
System.out.println(response);
}
private static void viewNewsFeed() throws IOException,
PostIncompleteException {
out.println("VIEW_NEWS_FEED"); // Send request to the server to retrieve the news feed
String newsFeedResponse;
boolean isNewsFeedEnded = false;
int consecutiveEmptyLines = 0;
// Read and print each line of the news feed response until two consecutive empty lines are encountered
while (!isNewsFeedEnded && (newsFeedResponse = in.readLine()) != null) {
if (newsFeedResponse.isEmpty()) {
consecutiveEmptyLines++;
if (consecutiveEmptyLines == 2) {
System.out.println("\n");
;
}
if (consecutiveEmptyLines > 2) {
isNewsFeedEnded = true;
}
} else {
System.out.println(newsFeedResponse);
consecutiveEmptyLines = 0;
}
}
showSocialMediaMenu(); // Show the social media menu after printing the news feed
}
}