-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOpenChat.java
54 lines (40 loc) · 1.44 KB
/
OpenChat.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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
class Solution {
static HashMap<String, String> nickmap = new HashMap<>();
static ArrayList<String> chatLog = new ArrayList<>();
public static String[] solution(String[] record) {
for(int i=0; i<record.length; i++){
StringTokenizer st = new StringTokenizer(record[i], " ");
String command = st.nextToken();
String userID = st.nextToken();
String name = "";
if(!command.equals("Leave"))
name = st.nextToken();
commandString(command,userID,name);
}
int cnt = 0;
String[] answer = new String[chatLog.size()];
for(String message: chatLog){
String uid = message.substring(0, message.indexOf("님"));
answer[cnt++] = message.replace(uid, nickmap.get(uid));
}
return answer;
}
public static void commandString (String command, String userID, String name) {
switch (command) {
case "Enter":
nickmap.put(userID, name);
chatLog.add(userID + "님이 들어왔습니다.");
break;
case "Leave":
chatLog.add(userID + "님이 나갔습니다.");
break;
case "Change":
nickmap.put(userID, name);
break;
}
}
}