일반 고객 클래스 구현
- 고객의 속성 : 고객 아이디, 고객 이름, 고객 등급, 보너스 포인트, 보너스 포인트 적립비율
- 일반 고객의 경우 물품 구매시 1%의 보너스 포인트 적립
public class Customer {
protected int customerID;
protected String customerName;
protected String customerGrade;
int bonusPoint;
double bonusdRatio;
// 일반 고객이 생성시 등급과 보너스 비율
public Customer() {
customerGrade = "SILVER";
bonusdRatio = 0.01;
}
// 물건 구매 시
public int calcPrice(int price) {
bonusPoint += price * bonusdRatio;
return price;
}
// 고객 정보
public String showCustomerInfo() {
return customerName + "님의 등급은 " + customerGrade + "이며, 보너스 포인트는 " + bonusPoint + "입니다.";
}
public int getCustomerID() {
return customerID;
}
public void setCustomerID(int customerID) {
this.customerID = customerID;
}
public String getCustomerName() {
return customerName;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
public String getCustomerGrade() {
return customerGrade;
}
public void setCustomerGrade(String customerGrade) {
this.customerGrade = customerGrade;
}
}
우수 고객 구현
- Customer 클래스에 추가해서 구현하는 것은 좋지 않음
- VIPCustomer 클래스 따로 구현
- 이미 Customer에 구현된 내용이 중복되므로 Customer를 확장하여 구현
public class VIPCustomer extends Customer {
double salseRatio;
private String agentID;
public VIPCustomer() {
bonusdRatio = 0.05;
salseRatio = 0.1;
customerGrade = "VIP";
}
public String getAgentID() {
return agentID;
}
public void setAgentID(String agentID) {
this.agentID = agentID;
}
}

protected 접근 제어자
- 상위 클래스에 선언된 private 멤버 변수는 하위 클래스에서 접근 할 수 없음
- 외부 클래스는 접근 할 수 없지만, 하위 클래스는 접근 할 수 있도록 protected 접근 제어자 사용
테스트 코드
public class CustomerTest {
public static void main(String[] args) {
Customer customerLee = new Customer();
customerLee.setCustomerName("이고객");
customerLee.setCustomerID(10010);
customerLee.bonusPoint = 1000;
System.out.println(customerLee.showCustomerInfo());
VIPCustomer customerKim = new VIPCustomer();
customerKim.setCustomerName("김고객");
customerKim.setCustomerID(10020);
customerKim.bonusPoint = 10000;
System.out.println(customerKim.showCustomerInfo());
}
}
참조 :
- 패스트캠퍼스
'JAVA' 카테고리의 다른 글
| [JAVA] 메서드 재정의 하기 (2) | 2023.07.16 |
|---|---|
| [JAVA] 상속에서 클래스 생성 과정과 형 변환 (1) | 2023.07.16 |
| [JAVA] 객체 간의 상속 (3) | 2023.07.12 |
| [JAVA] 2차원 배열 (1) | 2023.07.10 |
| [JAVA] 배열 (2) | 2023.07.09 |