[카카오 코딩테스트] 주차요금 계산 JAVA

wakeup Lee·2023년 3월 29일
0

알고리즘

목록 보기
2/2

package kakao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class Kakao_ParkingFee {

	public static void main(String[] args) {
		
		int[] fees = {180, 5000, 10, 600};
		String[] records = {"05:34 5961 IN", "06:00 0000 IN", "06:34 0000 OUT", "07:59 5961 OUT", "07:59 0148 IN", "18:59 0000 IN", "19:09 0148 OUT", "22:59 5961 IN", "23:00 5961 OUT"};
		
		solution(fees, records);
	}
	
    static int[] solution(int[] fees, String[] records) {
        
    	//입차한 차량 정보
        HashMap<String, String> carInfo = new HashMap<>();
        //총 사용시간
        HashMap<String, Integer> usedInfo = new HashMap<>();
        
        for(String record : records) {
        	
        	String time = record.split(" ")[0];
        	String carNum = record.split(" ")[1];
        	String action = record.split(" ")[2];
        	
        	if("IN".equals(action)) {
        		//입차한 차량 정보 등록
        		carInfo.put(carNum, time);        
        	}else if("OUT".equals(action)) {
        		String outTime = time;
        		String inTime = carInfo.get(carNum);
        		int usedTime = getUsedTime(inTime, outTime);

        		//하룻동안 두 번 이상 출차 한 경우
        		if(usedInfo.get(carNum) != null) {
        			int saveTime = usedInfo.get(carNum);
        			usedInfo.put(carNum, usedTime+saveTime);
        		}else {
        			usedInfo.put(carNum, usedTime);        			
        		}
 
        		//출차된 차량은 입차한 차량 정보에서 제거
        		carInfo.remove(carNum);
        	}
        }
        
        //오늘 출차하지 않은 차량
        for(String carNum : carInfo.keySet()) {
        	var notOutTime = todayIsNotOutCarUsedTime(carInfo.get(carNum));
    		if(usedInfo.get(carNum) != null) {
    			int saveTime = usedInfo.get(carNum);
    			usedInfo.put(carNum, notOutTime+saveTime);
    		}else {
    			usedInfo.put(carNum, notOutTime);        			
    		}
        }
        
        int[] answer = new int[usedInfo.size()];
        int i=0;
        
        //각 차량 총 요금 계산
        List<String> keyList = new ArrayList<>(usedInfo.keySet());
        keyList.sort((s1, s2) -> s1.compareTo(s2));
        for(String key : keyList) {
        	int usedTime = usedInfo.get(key);
        	answer[i++] = getUsedFee(fees, usedTime);
        }

        return answer;
    }
    
    //사용 시간 계산
    static int getUsedTime(String startTime, String endTime) {
    	
    	int startHour = Integer.parseInt(startTime.split(":")[0]);
    	int startMin = Integer.parseInt(startTime.split(":")[1]);
    	
    	int startAllMin = (startHour * 60) + startMin;
    	
    	int endHour = Integer.parseInt(endTime.split(":")[0]);
    	int endMin = Integer.parseInt(endTime.split(":")[1]);
    	
    	int endAllMin = (endHour * 60) + endMin;
    	
    	return endAllMin - startAllMin;
    }
    
    //사용 요금 계산
    static int getUsedFee(int[] feeInfo, int usedTime) {
    	int basicMin = feeInfo[0]; //기본 시간
    	int basicFee = feeInfo[1]; //기본 요금
    	int addMin = feeInfo[2]; //단위 시간
    	int addFee = feeInfo[3]; //단위 요금
    	
    	//기본 요금
    	if(usedTime <= basicMin) {
    		return basicFee;
    	}
    	
    	int overTime = usedTime - basicMin;
    	int overFee = basicFee + (overTime / addMin) * addFee;
    	
    	if((overTime % addMin) != 0) {
    		overFee = overFee + addFee;
    	}
    	
    	return overFee;
    }
    
    //오늘 출차되지 않는 차량의 사용시간
    static int todayIsNotOutCarUsedTime(String inTime) {
    	int inHour = Integer.parseInt(inTime.split(":")[0]);
    	int inMin = Integer.parseInt(inTime.split(":")[1]);
    	
    	int inAllMin = (inHour * 60) + inMin;
    	
    	int lastTime = (23 * 60) + 59;
    	
    	return lastTime - inAllMin;
    }
}
profile
백엔드 개발자

0개의 댓글