시나리오
- 회사에서 고객 정보를 활용한 맞춤 서비스를 하기 위해 일반고객(Customer)과 이보다 충성도가 높은 우수고객(VipCustomer)에 따른 서비스를 제공하고자 함
- 물품을 구매 할때 적용되는 할인율과 적립되는 보너스 포인트의 비율이 다름
- 여러 멤버십에 대한 각각 다양한 서비스를 제공할 수 있음
- 멤버십에 대한 구현을 클래스 상속을 활용하여 구현
일반 고객(Customer) 클래스 구현
- 고객의 속성 : 고객 아이디, 고객 이름, 고객 등급, 보너스 포인트, 보너스 포인트 적립비율
- 일반 고객의 경우 물품 구매시 1%의 보너스 포인트 적립
public class Customer {
protected int customerID;
protected String customerName;
protected String customerGrade;
int bonusPoint;
double bonusRatio;
public Customer()
{
customerGrade = "Bronze";
bonusRatio = 0.01;
}
public int calPrice(int price) {
bonusPoint += price * bonusRatio;
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;
}
}
고객 클래스를 상속받은 VipCustomer class
public class VipCustomer extends Customer{
private int agentID;
double salesRatio;
public VipCustomer() {
bonusRatio = 0.05;
salesRatio = 0.1;
customerGrade ="VIP";
}
public int getAgentID() {
return agentID;
}
public void setAgentID(int agentID) {
this.agentID = agentID;
}
public double getSalesRatio() {
return salesRatio;
}
public void setSalesRatio(double salesRatio) {
this.salesRatio = salesRatio;
}
}
Test.java
public class Test {
public static void main(String[] args) {
Customer customer = new Customer();
customer.setCustomerName("손님");
customer.setCustomerID(10000);
customer.bonusPoint = 1000;
System.out.println(customer.showCustomerInfo());
VipCustomer vip = new VipCustomer();
vip.setCustomerName("lRoot");
vip.setCustomerID(10001);
vip.bonusPoint = 1000;
System.out.println(vip.showCustomerInfo());
}
}
'프로그래밍 언어 > JAVA(자바) 응용' 카테고리의 다른 글
25.메서드 재정의(오버라이딩,Overriding) (0) | 2022.05.24 |
---|---|
24.상속에서 클래스 생성과정과 형 변환 (0) | 2022.05.24 |
22.상속(inheritance) (0) | 2022.05.23 |
21.ArrayList를 활용한 간단한 성적 산출 프로그램 (0) | 2022.05.23 |
20.객체 배열을 구현한 클래스 ArrayList (0) | 2022.05.22 |
댓글