-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPostDB.java
69 lines (57 loc) · 1.61 KB
/
PostDB.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
import java.util.*;
public class PostDB {
static ArrayList<Post> posts = Demo.getPosts();
static HashMap<String, ArrayList> allCatPosts = new HashMap();
static void init() {
for (Post p : posts) {
if (!allCatPosts.containsKey(p.getCategory())) {
ArrayList<Post> catList = new ArrayList<Post>();
allCatPosts.put(p.getCategory(), catList);
}
allCatPosts.get(p.getCategory()).add(p);
}
}
// Access methods
public static ArrayList<Post> getPosts(String category) {
if (category.equals("all")) {
return posts;
} else {
return allCatPosts.get(category);
}
}
public static Post getSinglePost(String category, int input) {
if (category.equals("all")) {
return posts.get(input);
} else {
ArrayList<Post> list = allCatPosts.get(category);
return list.get(input);
}
}
public static int getCategorySize(String category){
if (category.equals("all")){
return posts.size();
}else{
int size = allCatPosts.get(category).size();
return size;
}
}
public static Post answerPost(Post post, String answerBody) {
Post answer = post.clone();
answer.setDescription(answerBody);
answer.setType("answer");
return answer;
}
public static void addPost(Post post){
posts.add(post);
allCatPosts.get(post.getCategory()).add(post);
}
public static HashMap getPostsHashMap(){
return allCatPosts;
}
public static void printCategories(){
System.out.println(Arrays.toString(PostDB.getPostsHashMap().keySet().toArray()));
}
// abstract Post updatePost(Post post);
// abstract boolean deletePost(Post post);
// abstract User flagPost(Post post);
}