-
[김영한 스프링] 05. 새로운 할인 정책 개발Spring/스프링 핵심 원리 - 기본편 2023. 4. 8. 00:24
RateDiscountPolicy 추가
RateDiscountPolicy.java
main/java/hello.core/discount/RateDiscountPolicy 생성
메서드 구현
package hello.core.discount;
import hello.core.member.Grade;
import hello.core.member.Member;
public class RateDiscountPolicy implements DiscountPolicy {
private int discountPercent = 10; // 할인율
@Override
public int discount(Member member, int price) {
if (member.getGrade() == Grade.VIP) {
return price * discountPercent / 100;
} else {
return 0;
}
}
}등급이 VIP인 사람들만 10% 할인 하도록
RateDiscountPolicyTest.java
test 클래스를 자동으로 만들어 줌
test/java/hello.core/discount/RateDiscountPolicyTest
package hello.core.discount;
import hello.core.member.Grade;
import hello.core.member.Member;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class RateDiscountPolicyTest {
RateDiscountPolicy discountPolicy = new RateDiscountPolicy();
@Test
@DisplayName("VIPsms 10% 할인이 적용되어야 한다.")
void vip_o() {
// given
Member member = new Member(1L, "memberVIP", Grade.VIP);
// when
int discount = discountPolicy.discount(member, 10000);
// then
Assertions.assertThat(discount).isEqualTo(1000);
}
@Test
@DisplayName("VIP가 아니면 할인이 적용되지 않아야 한다.")
void vip_x() {
// given
Member member = new Member(2L, "memberBASIC", Grade.BASIC);
// when
int discount = discountPolicy.discount(member, 10000);
// then
Assertions.assertThat(discount).isEqualTo(0);
}
}스프링 핵심 원리 - 기본편 - 인프런 | 강의
스프링 입문자가 예제를 만들어가면서 스프링의 핵심 원리를 이해하고, 스프링 기본기를 확실히 다질 수 있습니다., - 강의 소개 | 인프런
www.inflearn.com
'Spring > 스프링 핵심 원리 - 기본편' 카테고리의 다른 글
[김영한 스프링] 07. AppConfig 리팩터링, 새로운 구조와 할인 정책 적용 (0) 2023.04.12 [김영한 스프링] 06. 새로운 할인 정책 적용과 문제점, 관심사의 분리 (0) 2023.04.12 [김영한 스프링] 04. 주문과 할인 도메인 (1) 2023.04.07 [김영한 스프링] 03. 회원 도메인 (0) 2023.04.05 [김영한 스프링] 02. 프로젝트 생성 (0) 2023.04.05