프로그래머스 커피 심부름

KIMYEONGJUN·2026년 8월 10일
post-thumbnail

문제

내가 생각했을때 문제에서 원하는부분

팀의 막내인 철수는 아메리카노와 카페 라테만 판매하는 카페에서 팀원들의 커피를 사려고 합니다.
아메리카노와 카페 라테의 가격은 차가운 것과 뜨거운 것 상관없이 각각 4500, 5000원입니다.
각 팀원에게 마실 메뉴를 적어달라고 하였고, 그 중에서 메뉴만 적은 팀원의 것은 차가운 것으로 통일하고 "아무거나"를 적은 팀원의 것은 차가운 아메리카노로 통일하기로 하였습니다.
각 직원이 적은 메뉴가 문자열 배열 order로 주어질 때, 카페에서 결제하게 될 금액을 return 하는 solution 함수를 작성해주세요.
order의 원소는 아래의 것들만 들어오고, 각각의 의미는 다음과 같습니다.

내가 이 문제를 보고 생각해본 부분

solution 메서드는 주문 메뉴 배열 order를 입력받아 정수형 total 변수에 누적 계산 결과를 저장한다.
반복문으로 주문 배열의 각 문자열 아이템을 순회한다.
switch 문을 통해 문자열 값에 따라 아메리카노와 카페 라떼 중 어떤 것을 시켰는지 분류한다.
차가운, 뜨거운, 또는 그냥 메뉴명만 적힌 아메리카노와 "anything" 주문은 모두 4500원을 더한다.
카페 라떼 종류는 모두 5000원을 더한다.
입력값이 문제에서 제시된 범위 밖이거나 기타 예외일 경우 default 구문에서 무시한다.
모든 주문 아이템을 처리한 뒤 누적된 total 값을 반환한다.

코드로 구현

class Solution {
    public int solution(String[] order) {
        int total = 0;
        for (String item : order) {
            switch (item) {
                case "iceamericano":
                case "americanoice":
                case "hotamericano":
                case "americanohot":
                case "americano":
                case "anything":
                    total += 4500;
                    break;
                case "icecafelatte":
                case "cafelatteice":
                case "hotcafelatte":
                case "cafelattehot":
                case "cafelatte":
                    total += 5000;
                    break;
                default:
                    break;
            }
        }
        return total;
    }
}

프로그래머스 코드

package programmers.programmers2;

// 프로그래머스 커피 심부름
public class Main108 {
    public static void main(String[] args) {
        Solution solution = new Solution();

        // 테스트용 주문 배열 예시
        String[] order1 = {"cafelatte", "americanoice", "hotcafelatte", "anything"};
        String[] order2 = {"americanoice", "americano", "iceamericano"};

        // 결과 출력
        System.out.println(solution.solution(order1)); // 기대값: 19000
        System.out.println(solution.solution(order2)); // 기대값: 13500
    }
}

// 주문에 따른 총 결제 금액 계산 클래스 및 메서드
class Solution {
    public int solution(String[] order) {
        int total = 0;
        for (String item : order) {
            switch (item) {
                case "iceamericano":
                case "americanoice":
                case "hotamericano":
                case "americanohot":
                case "americano":
                case "anything":
                    total += 4500;
                    break;
                case "icecafelatte":
                case "cafelatteice":
                case "hotcafelatte":
                case "cafelattehot":
                case "cafelatte":
                    total += 5000;
                    break;
                default:
                    // 문제 조건에서는 없는 경우지만 혹시 대비
                    break;
            }
        }
        return total;
    }
}

위에 있는 코드를 변경한 코드

마무리

코드와 설명이 부족할수 있습니다. 코드를 보시고 문제가 있거나 코드 개선이 필요한 부분이 있다면 댓글로 말해주시면 감사한 마음으로 참고해 코드를 수정 하겠습니다.

profile
Junior backend developer

0개의 댓글