프로그래머스 - 오픈채팅방

K PizzaCola·2021년 6월 13일
0
post-thumbnail

https://programmers.co.kr/learn/courses/30/lessons/42888

id - nickname형태로 저장하고, change가 들어오면 교체하는 형태로 구성한다.
그리고, 동작 - id 형태의 list에서 id를 key로 nickname을 가져와 결과를 구성한다.

import java.util.*;
class Solution {
    
    private Map<String, String> userDb = new HashMap<>();
    private List<String[]> history = new ArrayList<>();
    
    public String[] solution(String[] record) {
        for(String r : record) {
            String[] parsedRecode = r.split(" ");
            if (parsedRecode[0].charAt(0) == 'E') {
                userDb.put(parsedRecode[1], parsedRecode[2]);
                history.add(new String[]{parsedRecode[0], parsedRecode[1]});
            } else if (parsedRecode[0].charAt(0) == 'L') {
                history.add(new String[]{parsedRecode[0], parsedRecode[1]});
            } else if (parsedRecode[0].charAt(0) == 'C') {
                userDb.put(parsedRecode[1], parsedRecode[2]);
            }
        }
        String[] answer = new String[history.size()];
        
        for (int i = 0; i < answer.length; i++) {
            String[] h = history.get(i);
            answer[i] = userDb.get(h[1]) + Behavior.valueOf(h[0]).getDescription();
        }
        
        return answer;
    }
    
    static enum Behavior {
        Enter("님이 들어왔습니다."),
        Leave("님이 나갔습니다.");
        
        private final String description;
        
        Behavior(String description) {
            this.description = description;
        }
        
        public String getDescription() {
            return description;
        }
    }
}
profile
공부하는 개발자입니다.

0개의 댓글