티스토리 뷰

- 회원 도메인에 이어서 주문, 할인 / 이것도 순수 자바로만 구현했을때

 

 

 

DiscountPolicy.interface / 할인대상금액

package com.hello.core.discount;

import com.hello.core.member.Member;

public interface DiscountPolicy {
    int discount(Member member, int price);
}

 

 

 

FixDiscountPolicy.java / 할인대상금액 인터페이스 구현

package com.hello.core.discount;

import com.hello.core.discount.DiscountPolicy;
import com.hello.core.member.Grade;
import com.hello.core.member.Member;

public class FixDiscountPolicy implements DiscountPolicy {

    private int discountFixAmount = 1000;// 고정금액

    @Override
    public int discount(Member member, int price) {
        if(member.getGrade() == Grade.VIP){  // enum은 == 으로 비교
            return discountFixAmount;
        } else {
            return 0;
        }
    }
}

- 할인금액 1000원으로 고정

- 등급이 VIP일때만 할인

- enum은 == 으로 비교

 

 

 

Order.java / 주문 엔티티

package com.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 +
                '}';
    }
}

 

 

 

OrderService.interface / 주문 인터페이스

package com.hello.core.Order;

public interface OrderService {

    Order createOrder(Long memberId, String itemName, int itemPrice);
}

-리턴값이 Order

 

 

 

OrderServiceImpl.java / 회원서비스 - 가입, 조회

package com.hello.core.Order;

import com.hello.core.discount.DiscountPolicy;
import com.hello.core.discount.FixDiscountPolicy;
import com.hello.core.member.*;

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);
        int discountPrice = discountPolicy.discount(member, itemPrice);

        return new Order(memberId, itemName, itemPrice, discountPrice);
    }
}

- 회원조회 -> 등급이 VIP면 할인정책 적용 -> 새로운 주문

 

 

 

OrderApp.java / 테스트1

package com.hello.core;

import com.hello.core.Order.Order;
import com.hello.core.Order.OrderService;
import com.hello.core.Order.OrderServiceImpl;
import com.hello.core.member.Grade;
import com.hello.core.member.Member;
import com.hello.core.member.MemberService;
import com.hello.core.member.MemberServiceImpl;

public class OrderApp {

    public static void main(String[] args) {
        MemberService memberService = new MemberServiceImpl();
        OrderService orderService = new OrderServiceImpl();

        long memberId = 1L;
        Member member = new Member(memberId, "memberA", Grade.VIP);
        memberService.join(member);

        Order order = orderService.createOrder(memberId,"itemA",10000);

        System.out.println("order = " + order);
        System.out.println("calculatePrice = " + order.calculatePrice());
    }
}

- 결과 정상적으로 잘 나오는데 이렇게 테스트하는건 좋은 방법이 아니라고함

맨날 이렇게 테스트하는 1인 또 충격

 

 

 

OrderServiceTest / 테스트2 - JUnit 사용

package com.hello.core.order;

import com.hello.core.Order.Order;
import com.hello.core.Order.OrderService;
import com.hello.core.Order.OrderServiceImpl;
import com.hello.core.member.Grade;
import com.hello.core.member.Member;
import com.hello.core.member.MemberService;
import com.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);

    }
}

 

- Assertions 사용해서 확인 -> 정상적으로 실행

- Assertions쓰는것보다 soutv이 더 편한뎅 ㅠ

 

 

 

RateDiscountPolicy.java

package com.hello.core.discount;

import com.hello.core.member.Grade;
import com.hello.core.member.Member;

public class RateDiscountPolicy implements DiscountPolicy{

    private int discountPercent = 10; /// 금액에 따라 10% 할인적용

    @Override
    public int discount(Member member, int price) {
        if(member.getGrade() == Grade.VIP){
            return price * discountPercent / 100;
        } else {
            return 0;
        }
    }
}

- 고정된 할인 금액이 아니라 주문 금액에 따라 10% 할인될때

- ctrl + shift + t : 테스트 파일 생성

 

 

 

RateDiscountPolicyTest.java / 할인율 적용 되는지 테스트

package com.hello.core.discount;

import com.hello.core.member.Grade;
import com.hello.core.member.Member;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

class RateDiscountPolicyTest {

    RateDiscountPolicy discountPolicy = new RateDiscountPolicy();

    @Test
    @DisplayName("--- VIP Discount")  /// @DisplayName 테스트 클래스나, 메소드의 사용자 정의 표시 이름 선언
    void vip_o(){
        Member member = new Member(1L, "memberVIP", Grade.VIP);
        int discount = discountPolicy.discount(member, 10000);
        Assertions.assertThat(discount).isEqualTo(1000);
        /// alt + enter 간결해짐  static import
    }

    @Test
    @DisplayName("--- BASIC is No Discount")
    void vip_x(){
        Member member = new Member(2L, "memberBASIC", Grade.BASIC);
        int discount = discountPolicy.discount(member, 10000);
        Assertions.assertThat(discount).isEqualTo(0);
    }

}

- @DisplayName : 이런식으로 테스트 클래스나, 메소드의 사용자 정의 표시 이름 선언

- alt + enter : static import해줘서 코드 간단해짐

 

댓글
최근에 올라온 글
최근에 달린 댓글
링크
Total
Today
Yesterday