dev

작업자수와 예약시간 알고리즘

영리0 2024. 9. 19. 11:18

전체코드

function fetchAndDisableTimes(selectedDate) {
                fetch('/user/res/resTime', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({ date: selectedDate })
                })
                    .then(response => response.json())
                    .then(data => {
                        const times = data.disabledTimes;
                        const allButtons = document.querySelectorAll('input[name="time"]');
                        allButtons.forEach(button => {
                            button.disabled = false;
                            const label = document.querySelector(`label[for="${button.id}"]`);
                            if (label) {
                                label.classList.remove('disabled');
                            }
                        });

                        times.forEach(time => {
                            const timeButton = document.querySelector(`input[value="${time}"]`);
                            if (timeButton) {
                                timeButton.disabled = true;
                                const label = document.querySelector(`label[for="${timeButton.id}"]`);
                                if (label) {
                                    label.classList.add('disabled');
                                }
                            }

                        });
                    })
                    .catch(error => {
                        console.error('Error:', error);
                    });
            }
        });
  • input 타입의 value 를 설정하고 그 value 에 대한 값을 비활성화 하는 코드를 짬.
  • 즉, 컨트롤러에서 배열로 온시간 즉 {"10:00:00","11:00:00"} 이면 10시 11시의 시간블럭이 disabled 됨.
  • 따라서 컨트롤러에서 해당 날짜 총 인원 - 해당 날짜 일하고 있는 직원 을 하여 0 인 시간을 반환하는 알고리즘을 작성하면 됨.

service 코드

public List<String>getTime(Date date1) {

        int AllemployeeCount = userMapper.getCustomerCount();
        List<Map<String,Object>> count = userMapper.getTimeCount(date1);

        int checkNull = 0;
        String thisTime = "";
        List<String> disabledTimesList = new ArrayList<>();
        for (Map<String, Object> a : count) {


            Object rawTime = a.get("time");
            Object rawTimeCount = a.get("employeeCount");
            Object extraTime = a.get("extraTime");

            int time = Integer.parseInt(rawTime.toString());
            int employeeCount = ((Number) rawTimeCount).intValue();
            int extraTimeCount = ((Number) extraTime).intValue();

            log.info("extraTimeCount {} ", extraTimeCount);
            for(int i = 0; i < extraTimeCount; i++) {

                switch (time) {
                    case 9:
                        checkNull = AllemployeeCount - employeeCount;
                        thisTime = "09:00:00"; time++;
                        break;
                    case 10:
                        checkNull = AllemployeeCount - employeeCount;
                        thisTime = "10:00:00"; time++;
                        break;
                    case 11:
                        checkNull = AllemployeeCount - employeeCount;
                        thisTime = "11:00:00"; time++;
                        break;
                    case 12:
                        checkNull = AllemployeeCount - employeeCount;
                        thisTime = "12:00:00"; time++;
                        break;
                    case 13:
                        checkNull = AllemployeeCount - employeeCount;
                        thisTime = "13:00:00"; time++;
                        break;
                    case 14:
                        checkNull = AllemployeeCount - employeeCount;
                        thisTime = "14:00:00"; time++;
                        break;
                    case 15:
                        checkNull = AllemployeeCount - employeeCount;
                        thisTime = "15:00:00"; time++;
                        break;
                    case 16:
                        checkNull = AllemployeeCount - employeeCount;
                        thisTime = "16:00:00"; time++;
                        break;
                    case 17:
                        checkNull = AllemployeeCount - employeeCount;
                        thisTime = "17:00:00"; time++;
                        break;
                    default:
                        log.info("Default case");
                        break;
                }

                if (checkNull <= 0) {
                    disabledTimesList.add(thisTime);
                }
            }
        }
        return disabledTimesList;
    }
  1. 직원 수를 가져옴. (int AllemployeeCount = userMapper.getCustomerCount();)
  2. 해당 날짜의 직원 수를 가져옴 ( List<Map<String,Object>> count = userMapper.getTimeCount(date1);)
  3. rawTime, rawTimeCount, extraTime: 각 시간대, 해당 시간대의 예약된 직원 수, 추가 시간 정보를 가져옴.
  4. 각 시간대별로 checkNull 변수를 이용해 예약 가능한 직원 수를 계산함, 예약 가능한 직원 수가 0 이하일 경우 해당 시간대를 disabledTimesList에 추가

느낀점

  • 중복 코드가 너무 많다는 점을 느낌.
  • 좀 더 최적화 된 방법 생각해야함.

Object 파싱 오류

Map<String,Object> 로 JSON 파일을 받으면 int 파싱 시 오류남.
따라서
Object rawTime = a.get("time");
받은 후
int time = Integer.parseInt(rawTime.toString());
즉, ToString 한 다음 Integer 로 파싱 해야함.