Advertisement
MirelaP

Untitled

Apr 6th, 2018
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.71 KB | None | 0 0
  1. package def;
  2.  
  3. import java.awt.Window.Type;
  4. import java.io.Closeable;
  5. import java.io.File;
  6. import java.io.FileNotFoundException;
  7. import java.io.FileReader;
  8. import java.io.IOException;
  9. import java.io.PrintStream;
  10. import java.util.ArrayList;
  11. import java.util.Collections;
  12. import java.util.HashMap;
  13. import java.util.List;
  14. import java.util.Map;
  15. import java.util.Scanner;
  16.  
  17. import javax.json.Json;
  18. import javax.json.JsonArrayBuilder;
  19. import javax.naming.ldap.ManageReferralControl;
  20.  
  21. import org.codehaus.jackson.JsonGenerationException;
  22. import org.codehaus.jackson.JsonParseException;
  23. import org.codehaus.jackson.map.JsonMappingException;
  24. import org.codehaus.jackson.map.ObjectMapper;
  25. import org.codehaus.jackson.type.TypeReference;
  26. import org.json.simple.JSONArray;
  27. import org.json.simple.JSONObject;
  28. import org.json.simple.parser.JSONParser;
  29. import org.json.simple.parser.ParseException;
  30.  
  31. import com.google.gson.Gson;
  32. import com.google.gson.JsonArray;
  33. import com.google.gson.JsonElement;
  34. import com.google.gson.JsonObject;
  35. import com.google.gson.reflect.TypeToken;
  36.  
  37. public class Demo1 {
  38. static HashMap<String, User> registeredUsers = new HashMap<>();
  39.  
  40. static Scanner sc = new Scanner(System.in);
  41. static UsersManager manager = new UsersManager();
  42.  
  43.  
  44. public static void main(String[] args) throws IOException, ParseException {
  45.  
  46. UsersManager.setRegisterredUsers(registeredUsers);
  47. deserializeUsers();
  48.  
  49.  
  50. PostIdSequence idSeq = new PostIdSequence();
  51. CommentIdSequence commentSeq = new CommentIdSequence();
  52. String mail;
  53. boolean hasFriend;
  54. Map<Integer, Post> posts = new HashMap<Integer, Post>();
  55. Map<Integer, Comment> comments = new HashMap<Integer, Comment>();
  56.  
  57. // User currentUser = manager.registerUser("Maria Ivanova", "mariaI@avb.bg", "bluesk900y", "female",
  58. // "mariaPic.jpg");
  59. //
  60. // User user2 = manager.registerUser("Pesho", "pesho@avb.bg", "bluesk900y", "male", "pic.jpg");
  61. // currentUser.acceptFriendRequest("pesho@avb.bg");
  62. //
  63. // currentUser.addPost("C'mon Chelsea", posts, (int) idSeq.getNextValue());
  64. // currentUser.addPost("Glory MUTD", posts, (int) idSeq.getNextValue());
  65. // user2.addPost("first post ever", posts, (int) idSeq.getNextValue());
  66. //
  67. // registeredUsers.put(currentUser.getMail(), currentUser);
  68. // registeredUsers.put(user2.getMail(), user2);
  69.  
  70. int choice1;
  71.  
  72. do {
  73. System.out.println("-----------Wellcome to facebook-----------");
  74. System.out.println("Type the number of your choice");
  75. System.out.println("1.Register");
  76. System.out.println("2.LogIn");
  77. System.out.println("3.Close the program");
  78. choice1 = sc.nextInt();
  79. sc.nextLine();
  80. String postIdString = null;
  81. int currentPostId = 0;
  82.  
  83. switch (choice1) {
  84. case 1:
  85.  
  86. register();
  87. break;
  88. case 2:
  89. // to do make login bool
  90. boolean res = logIn();
  91.  
  92. int choice2;
  93. if (res) {
  94. do {
  95. System.out.println("Wellcome to your profile ");
  96. System.out.println("Type your choice");
  97. System.out.println("1.PostToMyWall");
  98. System.out.println("2.ShowMyWall");
  99. System.out.println("3.PostToFriendWall");
  100. System.out.println("4.ChangeWallStatus");
  101. System.out.println("5.RemoveFriend");
  102. System.out.println("6.ShowFeed");
  103. System.out.println("7.ShowPost");
  104. System.out.println("8.CommentPost");
  105. System.out.println("9.LikePost");
  106. System.out.println("10.UnlikePost");
  107. System.out.println("11.Log out");
  108.  
  109. choice2 = sc.nextInt();
  110. sc.nextLine();
  111. switch (choice2) {
  112.  
  113. // case 1:
  114. // System.out.println("Type in your post");
  115. //
  116. // String post = sc.nextLine();
  117. // currentUser.addPost(post, posts, (int) idSeq.getNextValue());
  118. // break;
  119. // case 2:
  120. // for (Integer postId : currentUser.getPosts()) {
  121. // Post currentPost = posts.get(postId);
  122. // showPostInfo(currentPost);
  123. // System.out.println();
  124. // }
  125. // break;
  126. // case 3:
  127. // // edinstvenoto unikalno pole na User
  128. // System.out.println("Type your friends email");
  129. // mail = sc.nextLine();
  130. //
  131. // hasFriend = isFriend(currentUser, mail);
  132. //
  133. // if (hasFriend) {
  134. // User friend = registeredUsers.get(mail);
  135. // if (!friend.isWallPublic()) {
  136. // System.out.println(friend.getName() + "'s wall is not public");
  137. // } else {
  138. // System.out.println("Type in your post");
  139. // String postFriend = sc.nextLine();
  140. // currentUser.addPostFriend(friend, postFriend, posts, (int) idSeq.getNextValue());
  141. // }
  142. // } else {
  143. // System.out.println("There is no such user or he is not your friend");
  144. // }
  145. // break;
  146. // case 4:
  147. // System.out.println("Your current status is : " + currentUser.isWallPublic());
  148. // currentUser.changeWallStatus();
  149. // System.out.println("Changed to : " + currentUser.isWallPublic());
  150. // break;
  151. // case 5:
  152. //
  153. // System.out.println("Type your friends email");
  154. // mail = sc.nextLine();
  155. // hasFriend = isFriend(currentUser, mail);
  156. // if (hasFriend) {
  157. // User friend = registeredUsers.get(mail);
  158. // currentUser.removeFriend(friend);
  159. // System.out.println(friend.getName() + " removed from friends");
  160. // } else {
  161. // System.out.println("There is no such user or he is not your friend");
  162. // }
  163. // break;
  164. // case 6:
  165. // // pokazva vsichki postove v ot priqteli v hronologichen red
  166. // List<Post> listofPosts = new ArrayList<Post>();
  167. // for (String friendMail : currentUser.getFriends()) {
  168. // User u = registeredUsers.get(friendMail);
  169. // System.out.println(u.getName());
  170. // for (Integer postId : u.getPosts()) {
  171. // Post currentPost = posts.get(postId);
  172. // listofPosts.add(currentPost);
  173. // }
  174. //
  175. // }
  176. // Collections.sort(listofPosts, new PostComparator());
  177. // for (Post sortedPost : listofPosts) {
  178. // showPostInfo(sortedPost);
  179. // }
  180. //
  181. // break;
  182. // case 7:
  183. // // pokazva cqlta informaciq za konkreten post
  184. // do {
  185. // System.out.println("Type post number");
  186. // postIdString = sc.nextLine();
  187. // } while (!isInteger(postIdString));
  188. //
  189. // currentPostId = Integer.parseInt(postIdString);
  190. // Post currentPost = posts.get(currentPostId);
  191. // showPostInfo(currentPost);
  192. // System.out.print("Liked by: ");
  193. // if (!currentPost.getPeopleLikes().isEmpty()) {
  194. // for (String likedBy : currentPost.getPeopleLikes()) {
  195. // System.out.print(registeredUsers.get(likedBy).getName() + ",");
  196. // }
  197. // System.out.println();
  198. // }
  199. //
  200. // for (Integer commentId : currentPost.getComments()) {
  201. // Comment currentComment = comments.get(commentId);
  202. // showCommentInfo(registeredUsers, currentComment);
  203. //
  204. // }
  205. // break;
  206. // case 8:
  207. // do {
  208. // System.out.println("Type post number");
  209. // postIdString = sc.nextLine();
  210. // } while (!isInteger(postIdString));
  211. //
  212. // currentPostId = Integer.parseInt(postIdString);
  213. // Post currentPostToComment = posts.get(currentPostId);
  214. //
  215. // String commentToAdd = null;
  216. // do {
  217. // System.out.println("Type in your comment : ");
  218. // commentToAdd = sc.nextLine();
  219. // } while (commentToAdd != null && commentToAdd.length() <= 0);
  220. //
  221. // currentUser.commentOnPost(currentPostToComment, commentToAdd, comments,
  222. // (int) commentSeq.getNextValue());
  223. //
  224. // break;
  225. // case 9:
  226. // do {
  227. //
  228. // System.out.println("Type post number");
  229. // postIdString = sc.nextLine();
  230. // } while (!isInteger(postIdString));
  231. //
  232. // currentPostId = Integer.parseInt(postIdString);
  233. // Post currentPostLike = posts.get(currentPostId);
  234. // currentUser.likePost(currentPostLike);
  235. // showPostInfo(currentPostLike);
  236. // break;
  237. // case 10:
  238. // do {
  239. // System.out.println("Type post number");
  240. // postIdString = sc.nextLine();
  241. // } while (!isInteger(postIdString));
  242. // currentPostId = Integer.parseInt(postIdString);
  243. // Post currentPostUnlike = posts.get(currentPostId);
  244. // currentUser.unlikePost(currentPostUnlike);
  245. // showPostInfo(currentPostUnlike);
  246. // break;
  247. }
  248. } while (choice2 != 11);
  249.  
  250. }
  251. break;
  252. }
  253.  
  254. } while (choice1 != 3);
  255. serializeUser();
  256. }
  257.  
  258. private static void serializeUser() {
  259. try {
  260. ObjectMapper mapper = new ObjectMapper();
  261.  
  262. // write JSON to a file
  263. mapper.writerWithDefaultPrettyPrinter().writeValue(new File("src//RegisteredUsers.json"), registeredUsers);
  264.  
  265. } catch (JsonGenerationException e) {
  266. e.printStackTrace();
  267. } catch (JsonMappingException e) {
  268. e.printStackTrace();
  269. } catch (IOException e) {
  270. e.printStackTrace();
  271. }
  272.  
  273. }
  274.  
  275. private static void deserializeUsers() {
  276. ObjectMapper mapper = new ObjectMapper();
  277.  
  278. try {
  279. registeredUsers = mapper.readValue(new File("src//RegisteredUsers.json"),
  280. new TypeReference<HashMap<String, User>>() {
  281. });
  282. for (String string : registeredUsers.keySet()) {
  283. System.out.println(registeredUsers.get(string));
  284. }
  285. System.out.println("In deserial");
  286. } catch (JsonParseException e) {
  287. // TODO Auto-generated catch block
  288. e.printStackTrace();
  289. } catch (JsonMappingException e) {
  290. // TODO Auto-generated catch block
  291. e.printStackTrace();
  292. } catch (IOException e) {
  293. // TODO Auto-generated catch block
  294. e.printStackTrace();
  295. }
  296.  
  297. }
  298.  
  299. public static User getUser(String mail) {
  300. return registeredUsers.get(mail);
  301. }
  302.  
  303. private static boolean logIn() {
  304. System.out.println("Insert mail");
  305. String mail = sc.nextLine();
  306. System.out.println("Insert password");
  307. String password = sc.nextLine();
  308. if (isUserExist(mail)) {
  309. return validateLogIn(mail, password);
  310. } else
  311. System.out.println("Not such user");
  312. return false;
  313.  
  314. }
  315.  
  316. private static void register() {
  317. System.out.println("Insert name");
  318. String name = sc.nextLine();
  319. System.out.println("Insert mail");
  320. String mail = sc.nextLine();
  321. System.out.println("Choose password that must contain at least 5 letters and 2 numbers ");
  322. String password = sc.nextLine();
  323. System.out.println("Insert gender- male/female");
  324. String gender = sc.nextLine();
  325. System.out.println("Insert avatarPath");
  326. String avatarPath = sc.nextLine();
  327. User user = registerUser(name, mail, password, gender, avatarPath);
  328. if (user != null) {
  329.  
  330. registeredUsers.put(mail, user);
  331. System.out.println("Registation is done");
  332.  
  333. } else {
  334. System.out.println("Registration has failed");
  335. }
  336. }
  337.  
  338. private static boolean isInteger(String s) {
  339. try {
  340. Integer.parseInt(s);
  341. } catch (NumberFormatException e) {
  342. return false;
  343. }
  344. return true;
  345. }
  346.  
  347. private static void showCommentInfo(Map<String, User> users, Comment currentComment) {
  348. System.out.println("\t Comment :" + currentComment.getText());
  349. System.out.println(" by: " + currentComment.getCommenter());
  350. System.out.println(" made at: " + currentComment.getTime());
  351. System.out.println(" number:" + currentComment.getId());
  352. System.out.println(" liked by : ");
  353. for (String friendLikedComment : currentComment.getLikedComment()) {
  354. System.out.print(users.get(friendLikedComment).getName() + ",");
  355. }
  356. }
  357.  
  358. private static void showPostInfo(Post post) {
  359. System.out.println("Post number : " + post.getId());
  360. System.out.println("Post : " + post.getText());
  361. System.out.println("Posted by: " + post.getPoster());
  362. System.out.println("Post made at: " + post.getTime());
  363. System.out.println("Likes: " + post.getLikes());
  364. }
  365.  
  366. private static boolean isFriend(User user, String mail) {
  367. for (String email : user.getFriendsMail()) {
  368. if (email.equals(mail)) {
  369. return true;
  370. }
  371. }
  372. System.out.println("This person is not your friend");
  373. return false;
  374. }
  375.  
  376. // public static void deserializeUs() throws FileNotFoundException, IOException,
  377. // ParseException {
  378. // JSONParser parser = new JSONParser();
  379. // JSONArray jsonArray = (JSONArray) parser.parse(new
  380. // FileReader("RegisteredUsers.json"));
  381. // System.out.println(jsonArray.size());
  382. //
  383. // for (Object o : jsonArray) {
  384. // JSONObject user = (JSONObject) o;
  385. //
  386. // String strName = (String) user.get("name");
  387. // System.out.println("Name::::" + strName);
  388. //
  389. // String strPasw = (String) user.get("password");
  390. // System.out.println("Password::::" + strPasw);
  391. //
  392. // String strMail = (String) user.get("mail");
  393. // System.out.println("mail::::" + strMail);
  394. //
  395. // String strGender = (String) user.get("gender");
  396. // System.out.println("gender::::" + strGender);
  397. //
  398. // ArrayList<String> arraysfr = (ArrayList<String>) user.get("friensdMail");
  399. // if (arraysfr != null && arraysfr.size() > 0) {
  400. //
  401. // for (int i = 0; i < arraysfr.size(); i++) {
  402. // System.out.println("friendMail::::" + arraysfr.get(i));
  403. //
  404. // }
  405. // } else {
  406. // System.out.println("friendMail-no");
  407. //
  408. // }
  409. // ArrayList<Integer> arrayspost = (ArrayList<Integer>) user.get("postsId");
  410. // if (arrayspost != null && arrayspost.size() > 0) {
  411. //
  412. // for (Object object : arrayspost) {
  413. // System.out.println("postsId::::" + object);
  414. // }
  415. // } else {
  416. // System.out.println("postsId:-no");
  417. //
  418. // }
  419. // String avatarPath = (String) user.get("avatarPath");
  420. // System.out.println("avatarPath::::" + avatarPath);
  421. //
  422. // ArrayList<String> arraysPost = (ArrayList<String>) user.get("posts");
  423. // if (arraysPost != null && arraysPost.size() > 0) {
  424. // for (Object object : arraysPost) {
  425. // System.out.println("posts::::" + object);
  426. // }
  427. // } else {
  428. // System.out.println("posts::--no:");
  429. //
  430. // }
  431. //
  432. // boolean wallP = (boolean) user.get("wallPublic");
  433. // System.out.println("wallPublic::::" + wallP);
  434. //
  435. // JSONArray arrfr = (JSONArray) user.get("friends");
  436. // if (arraysfr != null && arrfr.size() > 0) {
  437. // for (Object object : arrfr) {
  438. // System.out.println("friend::::" + object);
  439. // }
  440. // } else {
  441. // System.out.println("friend::-no");
  442. //
  443. // }
  444. // User u = new User(strName, strMail, strPasw, strGender, avatarPath);
  445. // registeredUsers.put(strMail, u);
  446. // System.out.println();
  447. // }
  448. //
  449. // }
  450.  
  451. // @SuppressWarnings("unchecked")
  452. // public static HashMap<String, User> deserializeUsers() throws IOException,
  453. // org.json.simple.parser.ParseException {
  454. //
  455. // HashMap<String, User> map = new HashMap<>();
  456. //
  457. // try {
  458. //
  459. // ObjectMapper mapper = new ObjectMapper();
  460. //
  461. // map = mapper.readValue(new File("RegisteredUsers.json"), new
  462. // TypeReference<HashMap<String, Object>>() {
  463. // });
  464. // } catch (JsonGenerationException e) {
  465. // e.printStackTrace();
  466. // } catch (JsonMappingException e) {
  467. // e.printStackTrace();
  468. // } catch (IOException e) {
  469. // e.printStackTrace();
  470. // }
  471. // return map;
  472. // }
  473.  
  474. // private static void serializeUser() throws IOException {
  475. //
  476. // File f = new File("RegisteredUsers.json");
  477. // if (f.exists()) {
  478. // try {
  479. //
  480. // PrintStream ps = new PrintStream(f);
  481. // JsonArray arr = new JsonArray();
  482. // for (String _mail : registeredUsers.keySet()) {
  483. // JsonObject obj = new JsonObject();
  484. // obj.addProperty("name", registeredUsers.get(_mail).getName());
  485. // obj.addProperty("mail", registeredUsers.get(_mail).getMail());
  486. // obj.addProperty("password", registeredUsers.get(_mail).getPassword());
  487. // obj.addProperty("gender", registeredUsers.get(_mail).getGender());
  488. //// JsonArrayBuilder arrBild=Json.createArrayBuilder();
  489. ////
  490. //// List <String> _friendsMail=new ArrayList<>();
  491. ////
  492. //// for(int i=0;i<registeredUsers.get(_mail).getFriendsMail().size();i++) {
  493. //// arrBild.add(registeredUsers.get(_mail).getFriendsMail().get(i));
  494. //// }
  495. //
  496. //// obj.add("friendsMail", (JsonElement) arrBild.build());
  497. // obj.addProperty("avatarPath",registeredUsers.get(_mail).getAvatarPath());
  498. // obj.addProperty("wallPublic", registeredUsers.get(_mail).isWallPublic());
  499. // arr.add(obj);
  500. // }
  501. // ps.println(arr.toString());
  502. // System.out.println(arr.toString());
  503. //
  504. // } catch (IOException e) {
  505. // System.out.println("file not found");
  506. // }
  507. // }
  508. // // try {
  509. // // ObjectMapper mapper = new ObjectMapper();
  510. // //
  511. // // // write JSON to a file
  512. // // mapper.writeValue(new File("src//RegisteredUsers.json"), registeredUsers);
  513. // //
  514. // // } catch (JsonGenerationException e) {
  515. // // e.printStackTrace();
  516. // // } catch (JsonMappingException e) {
  517. // // e.printStackTrace();
  518. // // } catch (IOException e) {
  519. // // e.printStackTrace();
  520. // // }
  521. //
  522. // }
  523.  
  524. public static void changeUserSettings(String name, String mail, String password, String gender, String avatarPath) {
  525. User user = getUser(mail);
  526. user.setName(name);
  527. user.setPassword(password);
  528. user.setGender(gender);
  529. user.setAvatarPath(avatarPath);
  530. }
  531.  
  532. public static User registerUser(String name, String mail, String password, String gender, String avatarPath) {
  533. User user = new User(name, mail, password, gender, avatarPath);
  534.  
  535. if (user.isPaswordStrong(password)) {
  536. registeredUsers.put(mail, user);
  537. return user;
  538.  
  539. } else {
  540. System.out.println("That password is not strong enough or there is such registered mail");
  541. return null;
  542. }
  543. }
  544.  
  545. public static boolean validateLogIn(String mail, String password) {
  546. for (String _mail : registeredUsers.keySet()) {
  547. if (mail.equals(_mail)) {
  548. if (getUser(_mail).getPassword().equals(password)) {
  549. System.out.println("Succesfull log in");
  550. return true;
  551. }
  552. }
  553. }
  554. System.out.println("Uncorrect password or email");
  555. return false;
  556. }
  557.  
  558. public static boolean isUserExist(String mail) {
  559. return registeredUsers.containsKey(mail);
  560. }
  561.  
  562. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement