-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiscussionBoard.java
More file actions
200 lines (178 loc) · 8.07 KB
/
DiscussionBoard.java
File metadata and controls
200 lines (178 loc) · 8.07 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
/* Name:Sukhapreet Sandhu
* Student ID: 1274838
* Compile commands: javac discussionboard/DiscussionBoard.java
* Run Command: java discussionboard.DiscussionBoard
* NOTE: run these commands in the folder where you put the discussionboard folder
* as mine was in lab4 I would run this command while in the lab4 folder
*/
package discussionboard;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.StringTokenizer;
public class DiscussionBoard {
/**
* @param args main method
* Handles all the logic, which option to proceed
*/
public static void main(String[] args) {
// declaring the scanner
Scanner inputScanner = new Scanner(System.in);
// user and post arraylists
ArrayList<Post> posts = new ArrayList<>();
ArrayList<User> users = new ArrayList<>();
HashMap<String, ArrayList<Integer>> userPostIndices = new HashMap<String, ArrayList<Integer>>();
// instance varibales
int choice;
String userName;
String userFullName;
boolean found = false;
String keyword;
// do while loop for the main menue, keeps running until user inputs choice as 6
do {
// display options and scan user input
System.out.println(
"\n(1) Create a new user\n(2) Create new post\n(3) View all posts\n(4) View all posts with a given username\n(5) End Program");
// helper fucntion call to get input
choice = getIntInput("Enter your choice: ", inputScanner);
// switch cases to execute different options selected
switch (choice) {
case 1:
User temp;
found = false;
// asks user for full name and a username
System.out.print("Enter your full name: ");
userFullName = inputScanner.nextLine();
System.out.print("Enter your username: ");
// scans user name and converts to lowercase
userName = inputScanner.nextLine().toLowerCase();
// try making the user object and cathes any exceptions thorown by the
// constructor of that
try {
temp = new User(userFullName, userName);
} catch (Exception e) {
// prints the message from the exception thrown and back to main menu
String message = e.getMessage();
System.out.println(message);
continue;
}
// check if user already exists in the arrayList
for (User user : users) {
if (user.getUserName().equals(temp.getUserName())) {
found = true;
}
}
// if username exists quit option 1
if (found) {
System.out.println("Username already exists");
break;
} else {
// add the user to the array list users
users.add(temp);
}
break;
case 2:
// scans user input
System.out.print("Enter username: ");
userName = inputScanner.nextLine().toLowerCase();
// no empty username for making a post
while (userName.isEmpty()) {
System.out.print("Enter a valid username: ");
userName = inputScanner.nextLine().toLowerCase();
}
User foundUser = null;
// checks if user exists
for (User user : users) {
if (user.getUserName().equals(userName)) {
foundUser = user;
}
}
// if user not registered
if (foundUser == null) {
System.out.println("Request denied, user doesn’t have a permission (Register as a user)");
break;
}
// create title and content for the post
System.out.print("Enter post title: ");
String title = inputScanner.nextLine();
System.out.print("Enter content: ");
String content = inputScanner.nextLine();
Post newPost;
// add post to the post arrayList
// posts.add(new Post(title, content, foundUser));
try {
newPost = new Post(title, content, foundUser);
} catch (Exception e) {
// prints the message from the exception thrown and back to main menu
String message = e.getMessage();
System.out.println(message);
continue;
}
// adds new post to eh arrylist
posts.add(newPost);
int postIndex = posts.size() - 1;
// Add the post index to userPostIndices hashmap
if (userPostIndices.containsKey(userName)) {
userPostIndices.get(userName).add(postIndex);
} else {
ArrayList<Integer> newPostList = new ArrayList<>();
newPostList.add(postIndex);
userPostIndices.put(userName, newPostList);
}
break;
case 3:
// is the discussion board empty
if (posts.size() == 0) {
System.out.println("There are no posts.");
} else {
// not empty, print all the posts
for (Post post : posts) {
System.out.println(post.toString());
}
}
break;
case 4:
// scans user input
System.out.print("Enter username to view its all posts: ");
userName = inputScanner.nextLine().toLowerCase();
// username in the hashmap postIndices
if (userPostIndices.containsKey(userName)) {
// get the arraylist of the indices for this user
ArrayList<Integer> postIndices = userPostIndices.get(userName);
// loop through the indices and print them
for (int index : postIndices) {
System.out.println(posts.get(index).toString());
}
} else {
// no user name in the hashmap
System.out.println("No posts by that username.");
}
break;
case 5:
break;
default:
System.out.println("Invalid option");
}
} while (choice != 5);
// close scanner
inputScanner.close();
}
// helper function to get int input from user
public static int getIntInput(String prompt, Scanner inputScanner) {
int value;
while (true) {
// print whatever prompt for user
System.out.print(prompt);
// try catch to validate the input
try {
// getting integer from the input
value = Integer.parseInt(inputScanner.nextLine().trim());
return value;
// catches the error and reprompts the user
} catch (NumberFormatException e) {
// reask user for invalid input
System.out.println("Invalid input. Please enter a valid integer.");
}
}
}
}