Dev.J
[JAVA] 문제 6-1 객체를 이용한 다형성 / 고객클래스 & VIP고객 클래스 본문
Customer 클래스
class Customer {
protected String customerID; //고객아이디
protected String customerName; //고객이름
protected String customerGrade; //고객등급
public String getCustomerID() {
return customerID;
}
public void setCustomerID(String 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;
}
int bonusPoint = 0; //보너스포인트 (가격*적립비율)
double bonusRatio; //보너스포인트 적립비율
Customer (){ //기본 생성자. 고객 한명이 생성되면 customerGrade는 silver이고, bonusRatio 는 1%로 적립
customerGrade = "SILVER";
bonusRatio = 0.01;
}
Customer(String customerID, String customerName){
this.customerID = customerID;
this.customerName = customerName;
}
int calcPrice(int price){
bonusPoint += price * bonusRatio; //왜 이 줄이랑 이 아래줄이랑 바꾸면 실행이 안되는거지?!
return price; //리턴타입이 int이므로 반환을 return 으로 해야함
}
String showCustomerInfo(){
return customerName + "님의 등급은 " + customerGrade + "이며, 보너스포인트는 " + bonusPoint + "입니다.";
}
}
VIPCustomer 클래스 extends Customer
public class VIPCustomer extends Customer {
private int agentID;
double saleRatio;
VIPCustomer(){ //기본 생성자. 고객 한명이 생성되면 customerGrade는 VIP이고, bonusRatio 는 5%로 적립
customerGrade = "VIP";
bonusRatio = 0.05;
saleRatio = 0.1;
}
VIPCustomer(String customerID, String customerName, int agentID){
this.customerID = customerID;
this.customerName = customerName;
this.agentID = agentID;
}
int calcPrice(int price) {
bonusPoint += price * bonusRatio;
return price * (int)(1 - saleRatio);
}
int getAgentID() {
return agentID;
}
}
CustomerTest 클래스
public class CustomerTest {
public static void main(String[] args) {
Customer 이순신 = new Customer();
이순신.customerName = ("이순신");
이순신.customerID = ("10010");
이순신.bonusPoint = 1000;
System.out.println(이순신.showCustomerInfo());
VIPCustomer 김유신 = new VIPCustomer();
김유신.customerName = ("김유신");
김유신.customerID = ("10020");
김유신.bonusPoint = 10000;
System.out.println(김유신.showCustomerInfo());
}
}
실행결과 :
이순신님의 등급은 SILVER이며, 보너스포인트는 1000입니다.
김유신님의 등급은 VIP이며, 보너스포인트는 10000입니다.
'Solved' 카테고리의 다른 글
[JAVA] 문제 12-1 다음 3개의 데이터를 입력받아 프로그램을 완성하시오. / 컬렉션(map) (0) | 2021.09.22 |
---|---|
[JAVA] 문제 6-2 객체를 이용한 다형성 / 배열을 이용하여 극장 예약시스템을 작성 (0) | 2021.09.06 |
[JAVA] 문제 4-3 다형성 / 인터페이스 구현(Printer, UsbMemory -> Device) (0) | 2021.09.06 |
[JAVA] 문제 4-5 Interface / 사용된 회사의 이름의 타이어를 출력 (0) | 2021.09.06 |
[JAVA] 문제 4-3 Interface / 인터페이스를 이용하여 가장 키 큰 사람 의 이름을 반환 (0) | 2021.09.06 |