Java

상속과 메서드 오버라이드를 통한 쇼핑몰 회원정보 수정

데브노트 2025. 4. 21. 16:58

요청사항.

 

고객 클래스.

고객 ID

고객 이름

고객 등급

포인트

할인율

 

VIP고객 클래스.

전용 할인율

전용ID

 

GOLD고객 클래스.

전용 할인율

전용ID

 

코드 실행부.

 

 

1. 고객 클래스 설계

//멤버변수

//static

//constructor

//getter

//setter

//method

package com.inheritance;
/**
 * 4.21 상속개념 7
 * 클래스 다이어그램 준수해야 한다.
 */
public class Customer {

    //member
    private int customerID;
    private String customerName;
    private String customerGrade;
    private int bonuspoint;
    private double bonusRatio;

    static int idCount = 1;

    //constructor
    public Customer(String customerName) {
        this.customerID = idCount;
        idCount++;
        this.customerName = customerName;
        customerGrade = "일반";
        bonuspoint = 0;
        bonusRatio = 0.01;
    }

    //getter
    int getCustomerID() {
        return customerID;
    }

    String getCustomerName() {
        return customerName;
    }

    String getCustomerGrade() {
        return customerGrade;
    }

    int getBonuspoint() {
        return bonuspoint;
    }

    //setter
    void setCustomerGrade(String customerGrade) {
        this.customerGrade = customerGrade;
    }

    //method

    public int calcPrice(int int1) {
        return (int) (bonusRatio * int1);
    } //calcPrice

    public void showCustomerInfo() {
        System.out.print("고객 정보...");
        System.out.print(" 고객ID: " + customerID);
        System.out.print(" 고객이름: " + customerName);
        System.out.print(" 고객등급: " + customerGrade + "(" + bonusRatio + ")");
        System.out.println(" 포인트: " + bonuspoint);
    } //showCustomerInfo

}//end of class

 

2.VIP클래스 설계

package com.inheritance;
/**
 * 4.21 상속개념 7_1
 * 클래스 다이어그램 준수해야 한다.
 */
public class VIPCustomer extends Customer {

    //클래스속성
    private int agentID;
    private double salesRatio;

    //생성자
    public VIPCustomer(String customerName, int agentID) {
        super(customerName);
        this.agentID = agentID;
        salesRatio = 0.03;
        setCustomerGrade("VIP");
    }

    //getter
    public int getAgentID() {
        return agentID;
    }

    //메서드
    public int calcPrice(int int1) {
        return (int) (salesRatio * int1);
    }

    public void showCustomerInfo() {
        System.out.print("고객 정보...");
        System.out.print(" 고객ID: " + super.getCustomerID());
        System.out.print(" 고객이름: " + super.getCustomerName());
        System.out.print(" 고객등급: " + super.getCustomerGrade() + "(" + salesRatio + ")");
        System.out.println(" 포인트: " + super.getBonuspoint());
    }

}//end of class

 

3.GOLD 클래스 설계(VIP와 비슷)

package com.inheritance;
/**
 * 4.21 상속개념 7_2
 * 클래스 다이어그램 준수해야 한다.
 */
public class GoldCustomer extends Customer {

    //클래스속성
    private int agentID;
    private double salesRatio;

    //생성자
    public GoldCustomer(String customerName, int agentID) {
        super(customerName);
        this.agentID = agentID;
        salesRatio = 0.05;
        setCustomerGrade("GOLD");
    }

    //getter
    public int getAgentID() {
        return agentID;
    }

    //메서드
    public int calcPrice(int int1) {
        return (int) (salesRatio * int1);
    }

    public void showCustomerInfo() {
        System.out.print("고객 정보...");
        System.out.print(" 고객ID: " + super.getCustomerID());
        System.out.print(" 고객이름: " + super.getCustomerName());
        System.out.print(" 고객등급: " + super.getCustomerGrade() + "(" + salesRatio + ")");
        System.out.println(" 포인트: " + super.getBonuspoint());
    }

}//end of class

 

4.실행부

package com.inheritance;
/**
 * 4.21 상속개념 7_3
 * 클래스 다이어그램 준수해야 한다.
 */
public class CustomerMainTest {

    //main
    public static void main(String[] args) {

        Customer customer1 = new Customer("홍길동");
        VIPCustomer vipCustomer1 = new VIPCustomer("장길산", 1);
        GoldCustomer goldCustomer1 = new GoldCustomer("임꺽정", 1);

        customer1.showCustomerInfo();
        vipCustomer1.showCustomerInfo();
        goldCustomer1.showCustomerInfo();

        System.out.println(customer1.getCustomerName() + "이 받을 포인트: " + customer1.calcPrice(1000));
        System.out.println(vipCustomer1.getCustomerName() + "이 받을 포인트: " + vipCustomer1.calcPrice(1000));
        System.out.println(goldCustomer1.getCustomerName() + "이 받을 포인트: " + goldCustomer1.calcPrice(1000));

    }//end of main
}//end of class