Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package def;
- import java.awt.Window.Type;
- import java.io.Closeable;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.IOException;
- import java.io.PrintStream;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.Scanner;
- import javax.json.Json;
- import javax.json.JsonArrayBuilder;
- import javax.naming.ldap.ManageReferralControl;
- import org.codehaus.jackson.JsonGenerationException;
- import org.codehaus.jackson.JsonParseException;
- import org.codehaus.jackson.map.JsonMappingException;
- import org.codehaus.jackson.map.ObjectMapper;
- import org.codehaus.jackson.type.TypeReference;
- import org.json.simple.JSONArray;
- import org.json.simple.JSONObject;
- import org.json.simple.parser.JSONParser;
- import org.json.simple.parser.ParseException;
- import com.google.gson.Gson;
- import com.google.gson.JsonArray;
- import com.google.gson.JsonElement;
- import com.google.gson.JsonObject;
- import com.google.gson.reflect.TypeToken;
- public class Demo1 {
- static HashMap<String, User> registeredUsers = new HashMap<>();
- static Scanner sc = new Scanner(System.in);
- static UsersManager manager = new UsersManager();
- public static void main(String[] args) throws IOException, ParseException {
- UsersManager.setRegisterredUsers(registeredUsers);
- deserializeUsers();
- PostIdSequence idSeq = new PostIdSequence();
- CommentIdSequence commentSeq = new CommentIdSequence();
- String mail;
- boolean hasFriend;
- Map<Integer, Post> posts = new HashMap<Integer, Post>();
- Map<Integer, Comment> comments = new HashMap<Integer, Comment>();
- // User currentUser = manager.registerUser("Maria Ivanova", "mariaI@avb.bg", "bluesk900y", "female",
- // "mariaPic.jpg");
- //
- // User user2 = manager.registerUser("Pesho", "pesho@avb.bg", "bluesk900y", "male", "pic.jpg");
- // currentUser.acceptFriendRequest("pesho@avb.bg");
- //
- // currentUser.addPost("C'mon Chelsea", posts, (int) idSeq.getNextValue());
- // currentUser.addPost("Glory MUTD", posts, (int) idSeq.getNextValue());
- // user2.addPost("first post ever", posts, (int) idSeq.getNextValue());
- //
- // registeredUsers.put(currentUser.getMail(), currentUser);
- // registeredUsers.put(user2.getMail(), user2);
- int choice1;
- do {
- System.out.println("-----------Wellcome to facebook-----------");
- System.out.println("Type the number of your choice");
- System.out.println("1.Register");
- System.out.println("2.LogIn");
- System.out.println("3.Close the program");
- choice1 = sc.nextInt();
- sc.nextLine();
- String postIdString = null;
- int currentPostId = 0;
- switch (choice1) {
- case 1:
- register();
- break;
- case 2:
- // to do make login bool
- boolean res = logIn();
- int choice2;
- if (res) {
- do {
- System.out.println("Wellcome to your profile ");
- System.out.println("Type your choice");
- System.out.println("1.PostToMyWall");
- System.out.println("2.ShowMyWall");
- System.out.println("3.PostToFriendWall");
- System.out.println("4.ChangeWallStatus");
- System.out.println("5.RemoveFriend");
- System.out.println("6.ShowFeed");
- System.out.println("7.ShowPost");
- System.out.println("8.CommentPost");
- System.out.println("9.LikePost");
- System.out.println("10.UnlikePost");
- System.out.println("11.Log out");
- choice2 = sc.nextInt();
- sc.nextLine();
- switch (choice2) {
- // case 1:
- // System.out.println("Type in your post");
- //
- // String post = sc.nextLine();
- // currentUser.addPost(post, posts, (int) idSeq.getNextValue());
- // break;
- // case 2:
- // for (Integer postId : currentUser.getPosts()) {
- // Post currentPost = posts.get(postId);
- // showPostInfo(currentPost);
- // System.out.println();
- // }
- // break;
- // case 3:
- // // edinstvenoto unikalno pole na User
- // System.out.println("Type your friends email");
- // mail = sc.nextLine();
- //
- // hasFriend = isFriend(currentUser, mail);
- //
- // if (hasFriend) {
- // User friend = registeredUsers.get(mail);
- // if (!friend.isWallPublic()) {
- // System.out.println(friend.getName() + "'s wall is not public");
- // } else {
- // System.out.println("Type in your post");
- // String postFriend = sc.nextLine();
- // currentUser.addPostFriend(friend, postFriend, posts, (int) idSeq.getNextValue());
- // }
- // } else {
- // System.out.println("There is no such user or he is not your friend");
- // }
- // break;
- // case 4:
- // System.out.println("Your current status is : " + currentUser.isWallPublic());
- // currentUser.changeWallStatus();
- // System.out.println("Changed to : " + currentUser.isWallPublic());
- // break;
- // case 5:
- //
- // System.out.println("Type your friends email");
- // mail = sc.nextLine();
- // hasFriend = isFriend(currentUser, mail);
- // if (hasFriend) {
- // User friend = registeredUsers.get(mail);
- // currentUser.removeFriend(friend);
- // System.out.println(friend.getName() + " removed from friends");
- // } else {
- // System.out.println("There is no such user or he is not your friend");
- // }
- // break;
- // case 6:
- // // pokazva vsichki postove v ot priqteli v hronologichen red
- // List<Post> listofPosts = new ArrayList<Post>();
- // for (String friendMail : currentUser.getFriends()) {
- // User u = registeredUsers.get(friendMail);
- // System.out.println(u.getName());
- // for (Integer postId : u.getPosts()) {
- // Post currentPost = posts.get(postId);
- // listofPosts.add(currentPost);
- // }
- //
- // }
- // Collections.sort(listofPosts, new PostComparator());
- // for (Post sortedPost : listofPosts) {
- // showPostInfo(sortedPost);
- // }
- //
- // break;
- // case 7:
- // // pokazva cqlta informaciq za konkreten post
- // do {
- // System.out.println("Type post number");
- // postIdString = sc.nextLine();
- // } while (!isInteger(postIdString));
- //
- // currentPostId = Integer.parseInt(postIdString);
- // Post currentPost = posts.get(currentPostId);
- // showPostInfo(currentPost);
- // System.out.print("Liked by: ");
- // if (!currentPost.getPeopleLikes().isEmpty()) {
- // for (String likedBy : currentPost.getPeopleLikes()) {
- // System.out.print(registeredUsers.get(likedBy).getName() + ",");
- // }
- // System.out.println();
- // }
- //
- // for (Integer commentId : currentPost.getComments()) {
- // Comment currentComment = comments.get(commentId);
- // showCommentInfo(registeredUsers, currentComment);
- //
- // }
- // break;
- // case 8:
- // do {
- // System.out.println("Type post number");
- // postIdString = sc.nextLine();
- // } while (!isInteger(postIdString));
- //
- // currentPostId = Integer.parseInt(postIdString);
- // Post currentPostToComment = posts.get(currentPostId);
- //
- // String commentToAdd = null;
- // do {
- // System.out.println("Type in your comment : ");
- // commentToAdd = sc.nextLine();
- // } while (commentToAdd != null && commentToAdd.length() <= 0);
- //
- // currentUser.commentOnPost(currentPostToComment, commentToAdd, comments,
- // (int) commentSeq.getNextValue());
- //
- // break;
- // case 9:
- // do {
- //
- // System.out.println("Type post number");
- // postIdString = sc.nextLine();
- // } while (!isInteger(postIdString));
- //
- // currentPostId = Integer.parseInt(postIdString);
- // Post currentPostLike = posts.get(currentPostId);
- // currentUser.likePost(currentPostLike);
- // showPostInfo(currentPostLike);
- // break;
- // case 10:
- // do {
- // System.out.println("Type post number");
- // postIdString = sc.nextLine();
- // } while (!isInteger(postIdString));
- // currentPostId = Integer.parseInt(postIdString);
- // Post currentPostUnlike = posts.get(currentPostId);
- // currentUser.unlikePost(currentPostUnlike);
- // showPostInfo(currentPostUnlike);
- // break;
- }
- } while (choice2 != 11);
- }
- break;
- }
- } while (choice1 != 3);
- serializeUser();
- }
- private static void serializeUser() {
- try {
- ObjectMapper mapper = new ObjectMapper();
- // write JSON to a file
- mapper.writerWithDefaultPrettyPrinter().writeValue(new File("src//RegisteredUsers.json"), registeredUsers);
- } catch (JsonGenerationException e) {
- e.printStackTrace();
- } catch (JsonMappingException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- private static void deserializeUsers() {
- ObjectMapper mapper = new ObjectMapper();
- try {
- registeredUsers = mapper.readValue(new File("src//RegisteredUsers.json"),
- new TypeReference<HashMap<String, User>>() {
- });
- for (String string : registeredUsers.keySet()) {
- System.out.println(registeredUsers.get(string));
- }
- System.out.println("In deserial");
- } catch (JsonParseException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (JsonMappingException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public static User getUser(String mail) {
- return registeredUsers.get(mail);
- }
- private static boolean logIn() {
- System.out.println("Insert mail");
- String mail = sc.nextLine();
- System.out.println("Insert password");
- String password = sc.nextLine();
- if (isUserExist(mail)) {
- return validateLogIn(mail, password);
- } else
- System.out.println("Not such user");
- return false;
- }
- private static void register() {
- System.out.println("Insert name");
- String name = sc.nextLine();
- System.out.println("Insert mail");
- String mail = sc.nextLine();
- System.out.println("Choose password that must contain at least 5 letters and 2 numbers ");
- String password = sc.nextLine();
- System.out.println("Insert gender- male/female");
- String gender = sc.nextLine();
- System.out.println("Insert avatarPath");
- String avatarPath = sc.nextLine();
- User user = registerUser(name, mail, password, gender, avatarPath);
- if (user != null) {
- registeredUsers.put(mail, user);
- System.out.println("Registation is done");
- } else {
- System.out.println("Registration has failed");
- }
- }
- private static boolean isInteger(String s) {
- try {
- Integer.parseInt(s);
- } catch (NumberFormatException e) {
- return false;
- }
- return true;
- }
- private static void showCommentInfo(Map<String, User> users, Comment currentComment) {
- System.out.println("\t Comment :" + currentComment.getText());
- System.out.println(" by: " + currentComment.getCommenter());
- System.out.println(" made at: " + currentComment.getTime());
- System.out.println(" number:" + currentComment.getId());
- System.out.println(" liked by : ");
- for (String friendLikedComment : currentComment.getLikedComment()) {
- System.out.print(users.get(friendLikedComment).getName() + ",");
- }
- }
- private static void showPostInfo(Post post) {
- System.out.println("Post number : " + post.getId());
- System.out.println("Post : " + post.getText());
- System.out.println("Posted by: " + post.getPoster());
- System.out.println("Post made at: " + post.getTime());
- System.out.println("Likes: " + post.getLikes());
- }
- private static boolean isFriend(User user, String mail) {
- for (String email : user.getFriendsMail()) {
- if (email.equals(mail)) {
- return true;
- }
- }
- System.out.println("This person is not your friend");
- return false;
- }
- // public static void deserializeUs() throws FileNotFoundException, IOException,
- // ParseException {
- // JSONParser parser = new JSONParser();
- // JSONArray jsonArray = (JSONArray) parser.parse(new
- // FileReader("RegisteredUsers.json"));
- // System.out.println(jsonArray.size());
- //
- // for (Object o : jsonArray) {
- // JSONObject user = (JSONObject) o;
- //
- // String strName = (String) user.get("name");
- // System.out.println("Name::::" + strName);
- //
- // String strPasw = (String) user.get("password");
- // System.out.println("Password::::" + strPasw);
- //
- // String strMail = (String) user.get("mail");
- // System.out.println("mail::::" + strMail);
- //
- // String strGender = (String) user.get("gender");
- // System.out.println("gender::::" + strGender);
- //
- // ArrayList<String> arraysfr = (ArrayList<String>) user.get("friensdMail");
- // if (arraysfr != null && arraysfr.size() > 0) {
- //
- // for (int i = 0; i < arraysfr.size(); i++) {
- // System.out.println("friendMail::::" + arraysfr.get(i));
- //
- // }
- // } else {
- // System.out.println("friendMail-no");
- //
- // }
- // ArrayList<Integer> arrayspost = (ArrayList<Integer>) user.get("postsId");
- // if (arrayspost != null && arrayspost.size() > 0) {
- //
- // for (Object object : arrayspost) {
- // System.out.println("postsId::::" + object);
- // }
- // } else {
- // System.out.println("postsId:-no");
- //
- // }
- // String avatarPath = (String) user.get("avatarPath");
- // System.out.println("avatarPath::::" + avatarPath);
- //
- // ArrayList<String> arraysPost = (ArrayList<String>) user.get("posts");
- // if (arraysPost != null && arraysPost.size() > 0) {
- // for (Object object : arraysPost) {
- // System.out.println("posts::::" + object);
- // }
- // } else {
- // System.out.println("posts::--no:");
- //
- // }
- //
- // boolean wallP = (boolean) user.get("wallPublic");
- // System.out.println("wallPublic::::" + wallP);
- //
- // JSONArray arrfr = (JSONArray) user.get("friends");
- // if (arraysfr != null && arrfr.size() > 0) {
- // for (Object object : arrfr) {
- // System.out.println("friend::::" + object);
- // }
- // } else {
- // System.out.println("friend::-no");
- //
- // }
- // User u = new User(strName, strMail, strPasw, strGender, avatarPath);
- // registeredUsers.put(strMail, u);
- // System.out.println();
- // }
- //
- // }
- // @SuppressWarnings("unchecked")
- // public static HashMap<String, User> deserializeUsers() throws IOException,
- // org.json.simple.parser.ParseException {
- //
- // HashMap<String, User> map = new HashMap<>();
- //
- // try {
- //
- // ObjectMapper mapper = new ObjectMapper();
- //
- // map = mapper.readValue(new File("RegisteredUsers.json"), new
- // TypeReference<HashMap<String, Object>>() {
- // });
- // } catch (JsonGenerationException e) {
- // e.printStackTrace();
- // } catch (JsonMappingException e) {
- // e.printStackTrace();
- // } catch (IOException e) {
- // e.printStackTrace();
- // }
- // return map;
- // }
- // private static void serializeUser() throws IOException {
- //
- // File f = new File("RegisteredUsers.json");
- // if (f.exists()) {
- // try {
- //
- // PrintStream ps = new PrintStream(f);
- // JsonArray arr = new JsonArray();
- // for (String _mail : registeredUsers.keySet()) {
- // JsonObject obj = new JsonObject();
- // obj.addProperty("name", registeredUsers.get(_mail).getName());
- // obj.addProperty("mail", registeredUsers.get(_mail).getMail());
- // obj.addProperty("password", registeredUsers.get(_mail).getPassword());
- // obj.addProperty("gender", registeredUsers.get(_mail).getGender());
- //// JsonArrayBuilder arrBild=Json.createArrayBuilder();
- ////
- //// List <String> _friendsMail=new ArrayList<>();
- ////
- //// for(int i=0;i<registeredUsers.get(_mail).getFriendsMail().size();i++) {
- //// arrBild.add(registeredUsers.get(_mail).getFriendsMail().get(i));
- //// }
- //
- //// obj.add("friendsMail", (JsonElement) arrBild.build());
- // obj.addProperty("avatarPath",registeredUsers.get(_mail).getAvatarPath());
- // obj.addProperty("wallPublic", registeredUsers.get(_mail).isWallPublic());
- // arr.add(obj);
- // }
- // ps.println(arr.toString());
- // System.out.println(arr.toString());
- //
- // } catch (IOException e) {
- // System.out.println("file not found");
- // }
- // }
- // // try {
- // // ObjectMapper mapper = new ObjectMapper();
- // //
- // // // write JSON to a file
- // // mapper.writeValue(new File("src//RegisteredUsers.json"), registeredUsers);
- // //
- // // } catch (JsonGenerationException e) {
- // // e.printStackTrace();
- // // } catch (JsonMappingException e) {
- // // e.printStackTrace();
- // // } catch (IOException e) {
- // // e.printStackTrace();
- // // }
- //
- // }
- public static void changeUserSettings(String name, String mail, String password, String gender, String avatarPath) {
- User user = getUser(mail);
- user.setName(name);
- user.setPassword(password);
- user.setGender(gender);
- user.setAvatarPath(avatarPath);
- }
- public static User registerUser(String name, String mail, String password, String gender, String avatarPath) {
- User user = new User(name, mail, password, gender, avatarPath);
- if (user.isPaswordStrong(password)) {
- registeredUsers.put(mail, user);
- return user;
- } else {
- System.out.println("That password is not strong enough or there is such registered mail");
- return null;
- }
- }
- public static boolean validateLogIn(String mail, String password) {
- for (String _mail : registeredUsers.keySet()) {
- if (mail.equals(_mail)) {
- if (getUser(_mail).getPassword().equals(password)) {
- System.out.println("Succesfull log in");
- return true;
- }
- }
- }
- System.out.println("Uncorrect password or email");
- return false;
- }
- public static boolean isUserExist(String mail) {
- return registeredUsers.containsKey(mail);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement