[프로그래머스] Java 코딩테스트 - 양꼬치

yihyun·2024년 8월 1일

코딩테스트

목록 보기
4/105
post-thumbnail

양꼬치

✅ 문제 설명

머쓱이네 양꼬치 가게는 10인분을 먹으면 음료수 하나를 서비스로 줍니다. 양꼬치는 1인분에 12,000원, 음료수는 2,000원입니다. 정수 n과 k가 매개변수로 주어졌을 때, 양꼬치 n인분과 음료수 k개를 먹었다면 총얼마를 지불해야 하는지 return 하도록 solution 함수를 완성해보세요.

✅ 제한사항

0 < n < 1,000
n / 10 ≤ k < 1,000
서비스로 받은 음료수는 모두 마십니다.

🔽 소스코드 1

양꼬치와 음료수 가격을 먼저 정의해준 후
int는 자동으로 소숫점을 버리기 때문에 양꼬치를 먹은 수에 10을 나눠 음료값을 곱해준다.
이후 양꼬치 + 음료 - 서비스음료를 계산해준다.

public class Solution {
    public int solution(int n, int k) {
		int yang = 12000 * n; // 양꼬치
        int drink = 2000 * k; // 음료
        int free = (n/10) * 2000; // 서비스 음료
        
        int answer = yang + drink - free;
    }
}

🔽 소스코드 2

다른 사람이 작성한 코드인데 코드를 분석해보면서 많은 공부가 될 수 있어서 가져왔다!

class Solution {
    public int solution(int n, int k) {
        int lambTotalPrice = totalPrice(Menu.LAMB, n);
        int drinkTotalPrice = totalPrice(Menu.DRINK, k);
        int discountPrice = discount(Menu.DRINK, n);

        int totalPay = lambTotalPrice + drinkTotalPrice - discountPrice;
        return totalPay;
    }

    private int totalPrice(Menu menu, int quantity) {
     return menu.getPrice() * quantity;   
    }

    private int discount(Menu menu, int lambQuantity) {
        // 양꼬치 10인분에 음료수 하나
        int point = lambQuantity / 10;

        return menu.getPrice() * point;
    }
}

enum Menu {

    LAMB("양꼬치", 12000),
    DRINK("음료수", 2000);

    private final String name;
    private final int price;

    Menu(String name, int price) {
        this.name = name;
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public int getPrice() {
        return price;
    }
}
profile
개발자가 되어보자

0개의 댓글