ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 15. demo1 - (5) CustomerDemo ( 상수 선언 )
    java/코드 리뷰 2020. 4. 27. 12:09
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    package demo1;
     
    public class Grade {
        
    //    public static final String VIP = "vip";
    //    public static final String GOLD = "gold";
    //    public static final String SILVER = "silver";
    //    public static final String BRONZE = "bronze";
     
        public static final int VIP = 0;
        public static final int GOLD = 1;
        public static final int SILVER = 2;
        public static final int BRONZE = 3;
        
    }
     
     
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    package demo1;
     
    public class Customer {
        
        private String name;
        private int grade;    // VIP, Gold, Silver, Bronze
        
        public Customer() {
            
        }
        
        public Customer(String name, int grade) {
            this.name = name;
            this.grade = grade;
        }
        
        public String getName() {
            return name;
        }
        
        public int getGrade() {
            return grade;
        }
        
    }
     
     
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    package demo1;
     
    public class CustomerDemo {
     
        public static void main(String[] args) {
     
    //        Customer customer1 = new Customer("홍길동", 0); // 가독성 좋지않음
            Customer customer1 = new Customer("홍길동", Grade.VIP); // 가독성 증가
            Customer customer2 = new Customer("김유신", Grade.SILVER);
            
            // 상수의 단점
            // 지정된 상수 외의 값이 대입되는 것을 컴파일 시에 체크 할 수 없음
    //        Customer customer5 = new Customer("강감찬", 100);
     
            System.out.println(getPoint1(customer1, 10000));
            System.out.println(getPoint1(customer2, 10000));
            System.out.println();
            System.out.println(getPoint2(customer1, 10000));
            System.out.println(getPoint2(customer2, 10000));
     
        }
     
        public static int getPoint1(Customer customer, int orderPrice) {
            
            int point = 0;
            if (customer.getGrade() == 0) {
                point = (int) (orderPrice * 0.05);
            } else if (customer.getGrade() == 1) {
                point = (int) (orderPrice * 0.03);
            } else if (customer.getGrade() == 2) {
                point = (int) (orderPrice * 0.02);
            } else if (customer.getGrade() == 3) {
                point = (int) (orderPrice * 0.01);
            }
            return point;
            
        }
     
        public static int getPoint2(Customer customer, int orderPrice) {
     
            int point = 0;
            if (customer.getGrade() == Grade.VIP) {
                point = (int) (orderPrice * 0.05);
            } else if (customer.getGrade() == Grade.GOLD) {
                point = (int) (orderPrice * 0.03);
            } else if (customer.getGrade() == Grade.SILVER) {
                point = (int) (orderPrice * 0.02);
            } else if (customer.getGrade() == Grade.BRONZE) {
                point = (int) (orderPrice * 0.01);
            }
            return point;
            
        }
     
    }
     
     
Designed by Tistory.