Dev.J

[JAVA] 문제 6-1 객체를 이용한 다형성 / 고객클래스 & VIP고객 클래스 본문

Solved

[JAVA] 문제 6-1 객체를 이용한 다형성 / 고객클래스 & VIP고객 클래스

JJ____ 2021. 9. 6. 20:14

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입니다.

728x90