Java Enum 사용기3

앞서 작성한 Enum 의 Grade 그리고 DiscountService 코드를 살펴보자

Enum.enum

public enum Grade {
    BRONZE(10), SILVER(20), GOLD(30);

    private final int discountPercent;

    Grade(int discountPercent) {
        this.discountPercent = discountPercent;
    }

    public int getDiscountPercent() {
        return discountPercent;
    }
}

DiscountService.class

public class DiscountService {
    public int discount(Grade grade, int price) {

        return grade.getDiscountPercent() * price / 100;
    }
}
  • 할인율 계산을 위한 Grade 데이터를 사용하고 discountPercent 값을 꺼내서 사용한다.
  • DiscountService 클래스는 Grade의 필드 변수로 통해서 할인을 계산한다.
  • 객체지향 관점으로 자신의 데이터를 외부로 호출 하는 것보다 Grade 클래스가 할인율 계산을 스스로 하는 것이 캡슐화 원칙에 맞다.

discount() 메소드 옮기기

DiscountService 클래스에 정의한 메소드를 Grade 클래스로 옮겨주도록 한다.

Grade.class

public enum Grade {
    BRONZE(10), SILVER(20), GOLD(30);

    private final int discountPercent;

    Grade(int discountPercent) {
        this.discountPercent = discountPercent;
    }

    public int getDiscountPercent() {
        return discountPercent;
    }

    //  new
    public int discount(int price) {
        return price * discountPercent / 100;
    }
}
  • discount() 메소드 추가

DiscountService.class

public class DiscountService {
    public int discount(Grade grade, int price) {

        return grade.discount(price);
    }
}
  • 서비스 로직에서 계산을 제거하였다.
  • grade discount(price) 호출하여 값을 전달하는 역할을 한다.

Main.class

public class Main {
    public static void main(String[] args) throws Exception {
        // 모든 ENUM 반환하기
        int price = 10000;

        DiscountService dc = new DiscountService();
        System.out.println(dc.discount(Grade.BRONZE, price));
        System.out.println(dc.discount(Grade.SILVER, price));
        System.out.println(dc.discount(Grade.GOLD, price));
    }
}

1000
2000
3000

메인함수에서 호출은 문제 없이 동작하여야 한다.


DiscountService 미사용하기

서비스 로직에서 계산을 없애고 불필요한 느낌이 발생하는 DiscountService 사용하지 않아도 된다.

호출부에서 바로 Enum 클래스의 discount() 호출해도 동작한다.

Main.class

public class Main {
    public static void main(String[] args) throws Exception {
        // 모든 ENUM 반환하기
        int price = 10000;

        //DiscountService dc = new DiscountService();
        System.out.println(Grade.BRONZE.discount(price));
        System.out.println(Grade.SILVER.discount(price));
        System.out.println(Grade.GOLD.discount(price));
    }
}

1000
2000
3000


호출부인 Main에서 print 메소드 만들기

호출부에서 ...println 함수 여러번 출력한 것을 볼 수 있다.

쿠폰 등급 이름을 출력하고 차감되는 금액을 출력해주도록 한다.

Main.class

public class Main {
    public static void main(String[] args) throws Exception {
        int price = 10000;

        printDiscount(Grade.BRONZE, price);
        printDiscount(Grade.SILVER, price);
        printDiscount(Grade.GOLD, price);
    }

    private static void printDiscount(Grade grade, int price) {
        System.out.println(grade.name() + " 등급으로 할인된 금액: " + grade.discount(price));
    }
}

BRONZE 등급으로 할인된 금액: 1000
SILVER 등급으로 할인된 금액: 2000
GOLD 등급으로 할인된 금액: 3000

  • printDiscount 여러 개를 사용하고 있다.
  • 신규 쿠폰을 발급하여도 문제 없이 확인하고 출력하는 코드로 만들어 주자.

Main.class

public class Main {
    public static void main(String[] args) throws Exception {
        int price = 10000;

        Grade[] grades = Grade.values();
        for (Grade grade : grades) {
            printDiscount(grade, price);
        }
    }

    private static void printDiscount(Grade grade, int price) {
        System.out.println(grade.name() + " 등급으로 할인된 금액: " + grade.discount(price));
    }
}

BRONZE 등급으로 할인된 금액: 1000
SILVER 등급으로 할인된 금액: 2000
GOLD 등급으로 할인된 금액: 3000

values 메소드로 사용하여 신규 쿠폰 발급하여도 문제없이 출력한다.