주문과 할인 실행과 테스트
주문 도메인 전체
Order 클래스 생성
OrderService 인터페이스 생성
DiscountPolicy 인터페이스 생성
OrderService 구현체 생성 : OrderServiceImpl
DiscountPolicy 구현체 생성 : FixDiscountPolicy
테스트코드 작성
Order 클래스 생성
package hello.core.order;
public class Order {
private Long memberId;
private String itemName;
private int itemPrice;
private int discountPrice;
public Order(Long memberId, String itemName, int itemPrice, int discountPrice) {
this.memberId = memberId;
this.itemName = itemName;
this.itemPrice = itemPrice;
this.discountPrice = discountPrice;
}
//비즈니스 로직 - 계산
public int calculatePrice(){
return itemPrice-discountPrice;
}
public Long getMemberId() {
return memberId;
}
public void setMemberId(Long memberId) {
this.memberId = memberId;
}
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
public int getItemPrice() {
return itemPrice;
}
public void setItemPrice(int itemPrice) {
this.itemPrice = itemPrice;
}
public int getDiscountPrice() {
return discountPrice;
}
public void setDiscountPrice(int discountPrice) {
this.discountPrice = discountPrice;
}
@Override
public String toString() {
return "Order{" +
"memberId=" + memberId +
", itemName='" + itemName + '\'' +
", itemPrice=" + itemPrice +
", discountPrice=" + discountPrice +
'}';
}
}
2. OrderService 인터페이스 생성
package hello.core.order;
public interface OrderService {
Order createOrder(Long memberId, String itemName, int itemPrice);
}
3. DiscountPolicy 인터페이스 생성
package hello.core.discount;
import hello.core.member.Member;
public interface DiscountPolicy {
/*
@return 할인 대상 금액
*/
int discount(Member member, int price);
}
4. OrderService 구현체 생성 : OrderServiceImpl
package hello.core.order;
import hello.core.discount.DiscountPolicy;
import hello.core.discount.FixDiscountPolicy;
import hello.core.member.Member;
import hello.core.member.MemberRepository;
import hello.core.member.MemoryMemberRepository;
public class OrderServiceImpl implements OrderService{
private final MemberRepository memberRepository = new MemoryMemberRepository();
private final DiscountPolicy discountPolicy = new FixDiscountPolicy();//인터페이스에 의존하면섣도 구현체를 할당받고 사용하고 있다!!
@Override
public Order createOrder(Long memberId, String itemName, int itemPrice) {
//회원을 먼저 찾고
Member member = memberRepository.findById(memberId);
//단일책임원칙을 잘 지키며 할인에 대한 역할은 discountPolicy에 넘긴다!
int discountPrice = discountPolicy.discount(member,itemPrice);
return new Order(memberId,itemName,itemPrice,discountPrice);//주문결과 반환.
}
}
5. DiscountPolicy 구현체 생성 : FixDiscountPolicy
package hello.core.discount;
import hello.core.member.Grade;
import hello.core.member.Member;
public class FixDiscountPolicy implements DiscountPolicy{
private int discountFixAmount = 1000;//1000원 할인
@Override
public int discount(Member member, int price) {
//enum타입은 == 쓰는 게 맞다.
if(member.getGrade() == Grade.VIP){
return discountFixAmount;
} else{
return 0;
}
}
}
6. 테스트코드 작성
package hello.core.order;
import hello.core.member.Grade;
import hello.core.member.Member;
import hello.core.member.MemberService;
import hello.core.member.MemberServiceImpl;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
public class OrderServiceTest {
MemberService memberService = new MemberServiceImpl();
OrderService orderService = new OrderServiceImpl();
@Test
void createOrder(){
Long memberId = 1L;
Member member = new Member(memberId,"memberA", Grade.VIP);
memberService.join(member);
Order order = orderService.createOrder(memberId,"itemA",10000);
Assertions.assertThat(order.getDiscountPrice()).isEqualTo(1000);
}
}
Last updated