-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgroupProject.java
More file actions
593 lines (430 loc) · 18.9 KB
/
groupProject.java
File metadata and controls
593 lines (430 loc) · 18.9 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
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
package groupProject;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.Date;
import java.sql.*;
public class GroupProject {
public static void main(String[] args){
getConnection();
String menu_input = "";
Scanner input = new Scanner(System.in);
while(!menu_input.equalsIgnoreCase("Q")){
while(!loggedOn){
printGuestMenu();
menu_input = input.nextLine();
if(menu_input.equals("1")){
createAccount(currentUser);
}
else if(menu_input.equals("2")){
login();
}
else if(menu_input.equals("3")){
postMessage();
}
else if(menu_input.equals("4")){
showPublicMessages();
}
else if(menu_input.equals("5")){
System.out.println("Please enter the name of the user you would like to search.");
String searchedUserName = input.nextLine();
searchUser(searchedUserName);
System.out.println("Would you like to:");
System.out.println("1: View this user's profile description");
System.out.println("2: View this user's messages?");
String menu_input2 = input.nextLine();
if(menu_input2.equals("1")){
viewDescription(searchedUserName);
}else if(menu_input2.equals("2")){
viewMessagesFromUser(searchedUserName);
}else{
System.out.println("Please enter one of the choices specified above.");
}
}
else if(menu_input.equalsIgnoreCase("Q")){
System.out.println("Have an evil day!");
break;
}else{
System.out.println("Please enter one of the choices specified above.");
}//end else
}//end while not logged in
while(loggedOn){
printUserMenu();
menu_input = input.nextLine();
if(menu_input.equals("1")){
currentUser.changePassword(stat);
}
else if(menu_input.equals("2")){
loggedOn = false;
currentUser.setName("");
currentUser.setPass("");
}
else if(menu_input.equals("3")){
postMessage();
}
else if(menu_input.equals("4")){
showMessagesFromSubscriptions();
}
else if(menu_input.equals("5")){
System.out.println("Please enter the name of the user you would like to search.");
String searchedUserName = input.nextLine();
searchUser(searchedUserName);
System.out.println("Would you like to:");
System.out.println("1: Subscribe to this user");
System.out.println("2: View this user's profile description");
System.out.println("3: View this user's messages?");
String menu_input2 = input.nextLine();
if(menu_input2.equals("1")){
subscribeToUser(searchedUserName);
}else if(menu_input2.equals("2")){
viewDescription(searchedUserName);
}else if(menu_input2.equals("3")){
viewMessagesFromUser(searchedUserName);
}else{
System.out.println("Please enter one of the choices specified above.");
}
}else if(menu_input.equals("6")){
createDescription();
}else if(menu_input.equals("7")){
viewMessagesFromUser(currentUser.getName());
}
else if(menu_input.equals("8")){
viewDescription(currentUser.getName());
}
else if(menu_input.equals("9")){
showSubscribers();
}
else if(menu_input.equals("10")){
showUserSubscriptions();
}
else if(menu_input.equalsIgnoreCase("Q")){
System.out.println("Have an evil day!");
break;//breaks out of this while, should take the user to the not-logged-in menu
}else{
System.out.println("Please enter one of the choices specified above.");
}//end else
}//end while logged in
}//end while not q
}//end main
///////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////
public static void getConnection(){
try{
con = DriverManager.getConnection("jdbc:mysql://localhost/users","root", "CPSC330");
stat = (Statement) con.createStatement();
}catch(Exception e){
System.out.println(e);
}
}
///////////////////////////////////////////////////////////////////////////////////////////////
public static void printGuestMenu(){
System.out.println("Welcome to GoSin, the network for the infamous Gotham Crime Syndicate! Enter one of these options to get started!");
System.out.println("1: Register");
System.out.println("2: Log in");
System.out.println("3: Post anonymous message");
System.out.println("4: Browse messages as guest");
System.out.println("5: Search for a user");
System.out.println("Q: Quit");
}
///////////////////////////////////////////////////////////////////////////////////////////////
public static void printUserMenu(){
System.out.println("You are now logged on as " + currentUser.getName() + ".");
System.out.println("1: Change Password");
System.out.println("2: Log off");
System.out.println("3: Post message");
System.out.println("4: Browse subscribed messages");
System.out.println("5: Search for a user");
System.out.println("6: Create/edit a profile description");
System.out.println("7: View my messages");
System.out.println("8: View my profile description");
System.out.println("9: View subscribers");
System.out.println("10: View your subscriptions");
System.out.println("Q: Quit");
}
///////////////////////////////////////////////////////////////////////////////////////////////
public static void login(){
Scanner input = new Scanner(System.in);
if(!loggedOn){
while(!loggedOn){
System.out.println("Please enter your username");
String new_username = input.nextLine();
if(currentUser.doesUsernameExist(new_username, stat)){ //check if name matches a username in the system
System.out.println("Please enter your password ");
String new_password = input.nextLine();
if(currentUser.isThisTheCorrectPassword(new_username, new_password, stat)){
currentUser = new UserAccount(new_username, new_password);
loggedOn = true;
System.out.println("Woo! You logged on successfully.");
}else{
System.out.println("You entered the wrong password; please try again.");
}
}else{
System.out.println("You entered the wrong username; please try again.");
}
}
}else{//if the user is already logged on
//this shouldn't ever happen however, as users aren't given this option when they are logged on
System.out.println("You are already logged in as " + currentUser.getName() + " . If you want to switch users, please log out first.");
}
}
///////////////////////////////////////////////////////////////////////////////////////////////
public static void createAccount(UserAccount newAccount){
Scanner input = new Scanner(System.in);
String new_username = "";
String new_password = "";
boolean added = false;
while(!added){
System.out.println("Please enter an original username, be creative! ");
new_username = input.nextLine();
if(newAccount.doesUsernameExist(new_username, stat)){ //check if name is taken
System.out.println("This username is taken, please try again.");
}else{
System.out.println("Please enter a password, don't forget it! ");
new_password = input.next();
newAccount = new UserAccount(new_username, new_password);
loggedOn = true;
System.out.println("Congratulations! You now have an account with GoSin. GO FORTH AND FUCK SHIT UP.");
added = true;
}
}
String insertQuery = "INSERT INTO userlist (username,password, description)" +
"VALUES ( '" + new_username + "' , '" + new_password + "', ' ' );";
try{
stat.execute(insertQuery);
currentUser = newAccount;
}catch(Exception e){
System.out.println(e);
}
}
///////////////////////////////////////////////////////////////////////////////////////////////
public static void postMessage(){
boolean found = false;
Scanner in = new Scanner(System.in);
System.out.println("Enter a message, please make it under 140 characters.");
String new_message = in.nextLine();
int priv = 0;//boolean value for database, whether the message is private
if(loggedOn){
System.out.println("Would you like your message to be private? (y/n)");
String new_input = in.next();
if(new_input.equals("y")){
priv = 1;
}else{
priv = 0;
}
}
String originalPoster = currentUser.getName();
java.util.Date utilDate = new java.util.Date();
java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
Calendar cal = Calendar.getInstance();
cal.set( cal.MILLISECOND, 0 );
java.sql.Time sqlTime =
new java.sql.Time( cal.getTime().getTime() );
// int id = currentUser.getUserID(stat);
String insertmessageQuery = "INSERT INTO messagelist (username, datePosted, message_content, private)"
+ "VALUES ( '"+ originalPoster +"', '" + sqlDate + " " + sqlTime + "' , '"+ new_message + "','" + priv + "' );";
try{
stat.execute(insertmessageQuery);
}catch(Exception e){
System.out.println(e);
}
}
///////////////////////////////////////////////////////////////////////////////////////////////
public static void showPublicMessages(){
String showPublicMessagesQuery = "SELECT * FROM messagelist WHERE private = 0;";
String postedMessage = "";
String originalPoster = "";
Date postedDate = null;
Time postedTime = null;
try{//try 1
ResultSet rs = stat.executeQuery(showPublicMessagesQuery);
try{
while (rs.next()) {
originalPoster = rs.getString("username");
postedDate = rs.getDate("datePosted");
postedTime = rs.getTime("datePosted");
postedMessage = rs.getString("message_content");
System.out.print(originalPoster + " | " + postedDate + " | " + postedTime);
System.out.println();
System.out.println(postedMessage);
System.out.println();
}
}catch(Exception e2){
System.out.println("e2:" + e2);
}
}catch(Exception e){
System.out.println("e:" + e);
}
}
///////////////////////////////////////////////////////////////////////////////////////////////
public static void searchUser(String enteredUsername){
String searchForUserQuery = "SELECT * FROM userlist WHERE username = '" + enteredUsername + "';";
String searchedUserName = "";
try{//try 1
ResultSet rs = stat.executeQuery(searchForUserQuery);
try{
while (rs.next()) {
searchedUserName = rs.getString("username");
System.out.println(searchedUserName);
}
}catch(Exception e2){
System.out.println("e2:" + e2);
}
}catch(Exception e){
System.out.println("e:" + e);
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////
public static void createDescription(){
Scanner input = new Scanner(System.in);
System.out.println("Please enter a brief description that you would like people to see on your GoSin profile:");
String new_description = input.nextLine();
String updateDescriptionQuery = "UPDATE userlist SET description = '" + new_description + "' WHERE username = '" + currentUser.getName() + "';";
try{
stat.execute(updateDescriptionQuery);
}catch(Exception e){System.out.println("CD e:" + e);}
}//end createDescription
///////////////////////////////////////////////////////////////////////////////////////////////
public static void viewMessagesFromUser(String entered_username){
//Scanner input = new Scanner(System.in);
String selectMessagesQuery = "SELECT * FROM messagelist WHERE username = '" + entered_username + "';";
String postedMessage = "";
String originalPoster = "";
Date postedDate = null;
Time postedTime = null;
try{//try 1
ResultSet rs = stat.executeQuery(selectMessagesQuery);
try{
while (rs.next()) {
originalPoster = rs.getString("username");
postedDate = rs.getDate("datePosted");
postedTime = rs.getTime("datePosted");
postedMessage = rs.getString("message_content");
System.out.print(originalPoster + " | " + postedDate + " | " + postedTime);
System.out.println();
System.out.println(postedMessage);
System.out.println();
}
}catch(Exception e2){
System.out.println("VMe2:" + e2);
}
}catch(Exception e){
System.out.println("VMe:" + e);
}
}
///////////////////////////////////////////////////////////////////////////////////////////////
public static void viewDescription(String entered_username){
Scanner input = new Scanner(System.in);
ResultSet rs = null;
String searched_description = "";
String showDescriptionQuery = "SELECT description FROM userlist WHERE username = '" + entered_username + "';";
try{
rs = stat.executeQuery(showDescriptionQuery);
try{
while(rs.next()){
searched_description = rs.getString("description");
System.out.println(searched_description);
}
}catch(Exception e2){
System.out.println("VDe2:" + e2.getMessage());
}
}catch(Exception e){
System.out.println("VDe:" + e.getMessage());
}
}
///////////////////////////////////////////////////////////////////////////////////////////////
public static void subscribeToUser(String usernameToSubscribeTo){
System.out.println("Attempting to subscribe to " + usernameToSubscribeTo + ".");
String insertSubscriptionQuery = "INSERT INTO subscribers (subscriber_name, subscribed_user_name) VALUES ('" + currentUser.getName() + "', '" + usernameToSubscribeTo + "');" ;
try{
stat.execute(insertSubscriptionQuery);
System.out.println("You are now subscribed to " + usernameToSubscribeTo + ".");
}catch(Exception e){
System.out.println("SU e: " + e);
System.out.println("Could not subscribe to " + usernameToSubscribeTo + ".");
}
}
///////////////////////////////////////////////////////////////////////////////////////////////
public static void showSubscribers(){
String showSubscribersQuery = "SELECT subscriber_name FROM subscribers WHERE subscribed_user_name = '" + currentUser.getName() + "';";
//String subscriber = "";
try{
ResultSet rs = stat.executeQuery(showSubscribersQuery);
try{
while(rs.next()){
System.out.println(rs.getString("subscriber_name"));
}
}catch(Exception e2){
System.out.println("SS e2:" + e2);
}
}catch(Exception e){
System.out.println("SS e:" + e);
}
}
///////////////////////////////////////////////////////////////////////////////////////////////
public static void showUserSubscriptions(){//profiles that a user has subscribed to
String showSubscribersQuery = "SELECT subscribed_user_name FROM subscribers WHERE subscriber_name = '" + currentUser.getName() + "';";
try{
ResultSet rs = stat.executeQuery(showSubscribersQuery);
try{
while(rs.next()){
System.out.println(rs.getString("subscribed_user_name"));
}
}catch(Exception e2){
System.out.println("SS e2:" + e2);
}
}catch(Exception e){
System.out.println("SS e:" + e);
}
}
///////////////////////////////////////////////////////////////////////////////////////////////
public static void showMessagesFromSubscriptions(){
//PART ONE: GET SUBSCRIPTIONS
String showSubscribersQuery = "SELECT subscribed_user_name FROM subscribers WHERE subscriber_name = '" + currentUser.getName() + "';";
String subscribed_username = "";
String originalPoster = "";
Date postedDate = null;
Time postedTime = null;
String postedMessage = "";
ArrayList<String> listOfSubscriptions = new ArrayList<String>();
try{
ResultSet rs = stat.executeQuery(showSubscribersQuery);
try{
while(rs.next()){
subscribed_username = rs.getString("subscribed_user_name");
listOfSubscriptions.add(subscribed_username);
}
}catch(Exception e2){
System.out.println("SS e2:" + e2);
}
}catch(Exception e){
System.out.println("SS e:" + e);
}
//PART TWO: FOR EACH SUBSCRIPTION, PRINT OUT ALL MESSAGES
String showSubscribedMessagesQuery = "";
for(int i = 0; i<listOfSubscriptions.size(); i++){
String subscribedUserNameToBePrintedOut = listOfSubscriptions.get(i);
showSubscribedMessagesQuery = "SELECT * FROM messagelist WHERE username = '" + subscribedUserNameToBePrintedOut + "';";
try{
ResultSet rs2 = stat.executeQuery(showSubscribedMessagesQuery);
while(rs2.next()){
originalPoster = rs2.getString("username");
postedDate = rs2.getDate("datePosted");
postedTime = rs2.getTime("datePosted");
postedMessage = rs2.getString("message_content");
System.out.print(originalPoster + " | " + postedDate + " | " + postedTime);
System.out.println();
System.out.println(postedMessage);
System.out.println();
}
}catch(Exception e3){
System.out.println("SS e3:" + e3);
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////
private static UserAccount currentUser = new UserAccount(); //the current account being used
private static Connection con = null;
private static Statement stat = null;
private static boolean loggedOn = false; //this is a boolean to check if the user is online
}