-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPostManager.java
More file actions
83 lines (61 loc) · 1.8 KB
/
PostManager.java
File metadata and controls
83 lines (61 loc) · 1.8 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
import java.util.*;
/**
* Created by alex on 07/12/2016.
*/
public class PostManager {
/**
* Singleton
*/
private static PostManager postManager = new PostManager();
private PostManager() {
}
public static PostManager getInstance() {
return postManager;
}
/**
* HashMap with postId containing an HashMap with the userId and the text of the post.
*/
private static Map<Integer, Post> posts = new HashMap<>();
private static Integer currentId = 0;
public void addPost() {
Scanner scanner = new Scanner(System.in);
System.out.println("From which user is the post? ");
UserManager userManager = UserManager.getInstance();
if (userManager.countUsers() == 0) {
System.out.println("No users available!");
return;
}
userManager.printAllUsers();
Integer userId = scanner.nextInt();
User user = userManager.getUserById(userId);
System.out.println("What is the post from " + user + "?");
// Skip the newline
scanner.nextLine();
String text = scanner.nextLine();
Post post = new Post(userId, text);
posts.put(currentId, post);
currentId++;
}
public void viewAllPosts() {
UserManager userManager = UserManager.getInstance();
posts.forEach((k,v) -> {
System.out.println("[" + k + "]");
System.out.println("\t" + userManager.getUserById(v.getUserId()));
System.out.println("\t" + v.getText());
});
}
public void viewPostsByUser() {
UserManager userManager = UserManager.getInstance();
System.out.println("Posts by which user?");
userManager.printAllUsers();
Scanner scanner = new Scanner(System.in);
int userId = scanner.nextInt();
System.out.println(userManager.getUserById(userId));
posts.forEach((k,v) -> {
if(v.getUserId() == userId) {
System.out.println(v.getText());
System.out.println("#################");
}
});
}
}