ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 15. demo2 - (7) DepositRateDemo ( 열거형 멤버변수 )
    java/코드 리뷰 2020. 4. 27. 13:50
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    package demo3;
     
    public enum ScoreGrade {
        
        A(10090), B(8980), C(7970), D(6960), F(590);
        
        private final int high;
        private final int low;
        
        ScoreGrade(int high, int low) {
            this.high = high;
            this.low = low;
        }
        
        public int getHigh() {
            return high;
        }
        
        public int getLow() {
            return low;
        }
        
    }
     
     
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    package demo3;
     
    public class ScoreGradeDemo {
        
        public static void main(String[] args) {
            
            ScoreGrade grade1 = ScoreGrade.A;
            ScoreGrade grade2 = ScoreGrade.B;
            
            System.out.println("최저점수 : " + grade1.getLow() + " 최고점수 : " + grade1.getHigh());
            System.out.println("최저점수 : " + grade2.getLow() + " 최고점수 : " + grade2.getHigh());
            
        }
    }
     
     
    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
    package demo3;
     
    public class User {
        
        private String name;
        private DepositRate depositRate;
        private int orderPrice;
        
        public User(String name, DepositRate depositRate, int orderPrice) {
            this.name = name;
            this.depositRate = depositRate;
            this.orderPrice = orderPrice;
        }
        
        public String getName() {
            return name;
        }
        
        public DepositRate getDepositRate() {
            return depositRate;
        }
        
        public int getOrderPrice() {
            return orderPrice;
        }
        
    }
     
     
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    package demo3;
     
    public enum DepositRate {
     
        BRONZE(0.01), SILVER(0.03),    GOLD(0.05),    VIP(0.1);
        
        // 열거형의 멤버변수는 반드시 final로 정의
        private final double value;
        
        // 열거형의 생성자는 멤버변수를 초기화해야 함
        // 열거형의 생성자는 오직 private만 가능
        // 열거형의 생성자는 열거형 내부적으로 사용
        private DepositRate(double value) {
            this.value = value;
        }
        
        public double getValue() {
            return value;
        }
        
    }
     
     
     
    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
    package demo3;
     
    public class DepositRateDemo {
        
        public static void main(String[] args) {
            
            User user1 = new User("홍길동", DepositRate.BRONZE, 120000);
            User user2 = new User("강감찬", DepositRate.GOLD, 500000);
            User user3 = new User("김유신", DepositRate.VIP, 450000);
            
            int point1 = (int) (user1.getOrderPrice()*user1.getDepositRate().getValue());
            int point2 = (int) (user2.getOrderPrice()*user2.getDepositRate().getValue());
            int point3 = (int) (user3.getOrderPrice()*user3.getDepositRate().getValue());
            
            System.out.println("홍길동의 적률 : " + user1.getDepositRate().getValue());
            System.out.println("강감찬의 적률 : " + user2.getDepositRate().getValue());
            System.out.println("김유신의 적률 : " + user3.getDepositRate().getValue());
            
            System.out.println(point1);
            System.out.println(point2);
            System.out.println(point3);
            
        }
    }
     
     
Designed by Tistory.