회사에서 고객 정보를 활용한 맞춤 서비스를 하기 위해 일반 고객(customer)과 이보다 충성도가 높은 우수고객(VIPCustomer)에 따른 서비스를 제공하고자 함
물품을 구매 할때 적용되는 할인율과 적용되는 보너스 포인트와 비율이 다름
여러 맴버십에 대한 각각 다양한 서비스를 제공할 수 있음
멤버십에 대한 구현을 클래스 상속을 활용하여 구현해보기
public class Customer {
protected int customerId;
protected String customerName;
protected String customerGrade;
int bonusPoint;
double bonusRatio;
public Customer() {
customerGrade = "SILVER";
bonusRatio = 0.01;
}
public int calcPrice(int price) {
bonusPoint += price * bonusRatio;
return price;
}
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;
}
public String showCustomerInfo() {
return customerName + "님의 등급은" + customerGrade + "이며, 보너스 포인트는 " + bonusPoint + "입니다.";
}
}
protected 접근 제어자
- 외부 클래스는 접근할 수 없지만, 하위 클래스는 접근할 수 있도록 protected 접근 제어자를 사용
- protected 멤버는 부모 클래스에 대해서는 public 멤버처럼 취급되며, 외부에서는 private 멤버처럼 취급
- 접근할 수 있는 영역
- 이 멤버를 선언한 클래스의 멤버
- 이 멤버를 선언한 클래스가 속한 패키지의 멤버
- 이 멤버를 선언한 클래스를 상속받은 자식 클래스(child class)의 멤버
public class VIPCustomer extends Customer{
private String agentId;
double saleRatio;
public VIPCustomer() {
bonusRatio = 0.05;
saleRatio = 0.1;
customerGrade = "VIP";
}
public String getAgentId() {
return agentId;
}
}
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());
}
}
이순신 님의 등급은 SILVER이며, 보너스 포인트는 1000입니다.
김유신 님의 등급은 VIP이며, 보너스 포인트는 10000입니다.
https://danmilife.tistory.com/21
http://www.tcpschool.com/java/java_modifier_accessModifier