-
15. demo1 - (5) CustomerDemo ( 상수 선언 )java/코드 리뷰 2020. 4. 27. 12:0912345678910111213141516package 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;}1234567891011121314151617181920212223242526package demo1;public class Customer {private String name;private int grade; // VIP, Gold, Silver, Bronzepublic Customer() {}public Customer(String name, int grade) {this.name = name;this.grade = grade;}public String getName() {return name;}public int getGrade() {return grade;}}1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556package 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;}}
'java > 코드 리뷰' 카테고리의 다른 글
15. demo2 - (7) DepositRateDemo ( 열거형 멤버변수 ) (0) 2020.04.27 15. demo2 - (6) CustomerDemo - 2 ( enum 선언 ) (0) 2020.04.27 15. demo2 - (4) FruitDemo ( ☆ 제네릭 메서드) (0) 2020.04.24 15. demo1.product - (3) BoxDemo ( ☆ 타입파라미터로 지정된 객체) (0) 2020.04.24 15. demo1.person - (2) PersonDemo (와일드카드 제네릭 타입제한) (0) 2020.04.24