각진 세상에 둥근 춤을 추자

[Java] 다형성 활용하기 본문

Java

[Java] 다형성 활용하기

circle.j 2022. 9. 20. 12:36

상위 클래스인 Customer 클래스와 이를 상속받는 하위 클래스인 VIPCustomer 클래스가 있다.

만약 고객이 늘어 VIP 고객만큼 물건을 많이 구매하지는 않지만, 그래도 단골인 고객에게 혜택을 주고자 한다.

그래서 일반 고객과 VIP 고객 사이의 중간 등급인 GOLD 등급의 클래스를 만들고자 한다.

 

GOLD 고객의 혜택은 다음과 같다.

- 제품 구매시 10% 할인

- 보너스 포인트 2% 적립

- 담당 전문 상담원 없음 

 

1. Customer 클래스 생성하기 (상위 클래스)

package ch08_5;

public class Customer {

	// 멤버 변수
	protected int customerID;	// 고객 아이디
	protected String customerName;	// 고객 이름
	protected String customerGrade;	// 고객 등급
	int bonusPoint;	// 보너스 포인트
	double bonusRatio;	// 적립 비율
	
	
	
	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 Customer() {
		initCustomer();
	}
	
	public Customer(int customerID, String customerName) {
		this.customerID = customerID;
		this.customerName = customerName;
		initCustomer();
	}
	private void initCustomer() {
		customerGrade = "SILVER";	// 기본 등급
		bonusRatio = 0.01;	// 보너스 포인트 기본 적립 비율
	}
	
	// 보너스 포인트 적립, 지불 가격 계산 메서드
	public int calcPrice(int price) {
		bonusPoint += price * bonusRatio;	// 보너스 포인트 계산
		return price;
	}
	
	// 고객 정보를 반환하는 메서드
	public String show() {
		return customerName + " 님의 등급은 " + customerGrade + "이며, 보너스 포인트는 " + bonusPoint + "입니다.";
	}
	
}

 

2. VIP 고객 클래스 생성하기 (하위 클래스)

package ch08_5;

public class VIPCustomer extends Customer {

	// 추가 속성
	private int agentID;	// VIP 고객 상담원 아이디
	double saleRatio;	// 할인율
	
	public VIPCustomer(int customerID, String customerName, int agentID) {
		super(customerID, customerName);
		customerGrade = "VIP";
		bonusRatio = 0.05;
		saleRatio = 0.1;
	
	}
	
	public int calcPrice(int price) {
		bonusPoint += price * bonusRatio;
		return price - (int)(price * saleRatio);
	}
	
	public int getAgentID() {
		return agentID;
	}

}

 

3. GOLD 고객 클래스 생성하기 (하위 클래스) 

package ch08_5;

public class GoldCustomer extends Customer{

	double saleRatio;
	
	public GoldCustomer(int customerID, String customerName) {
		super(customerID, customerName);
		customerGrade = "GOLD";
		bonusRatio = 0.02;
		saleRatio = 0.1;
	}
	
	public int calcPrice (int price) {
		bonusPoint += price * bonusRatio;
		return price - (int)(price * saleRatio);
	}
	
}

 

4. 여러 등급의 고객을 한 번에 관리할 수 있는 프로그램 구현하기 (배열로 고객 5명 구현하기)

- 회사의 고객은 현재 5명이다. 5명 중 VIP 1명, GOLD 2명, SILVER 2명이다. 

  고객들이 각각 10,000원짜리 상품을 구매했을 때의 결과를 출력한다.

package ch08_5;

import java.util.ArrayList;

public class CustomerTest {
	public static void main(String[] args) {
		
		ArrayList<Customer> customerList = new ArrayList<Customer>();
		
		// 고객 생성
		Customer cus1 = new Customer(10010, "이순신");
		Customer cus2 = new Customer(10020, "신사임당");
		Customer cus3 = new GoldCustomer(10030, "홍길동");
		Customer cus4 = new GoldCustomer(10040, "이율곡");
		Customer cus5 = new VIPCustomer(10050, "김유신",12345);
		
		// ArrayList의 add 속성을 사용해 객체 배열에 고객 추가
		customerList.add(cus1);
		customerList.add(cus2);
		customerList.add(cus3);
		customerList.add(cus4);
		customerList.add(cus5);
		
		// 고객 정보 출력
		System.out.println("------ 고객 정보 출력 -------");
		for (Customer customer : customerList) {
			System.out.println(customer.show());
		}
		
		System.out.println("------ 할인율과 보너스 포인트 계산 -------");
		int price = 10000;
		// 다형성 구현
		for (Customer customer : customerList) {
			int cost = customer.calcPrice(price);
			System.out.println(customer.getCustomerName()+" 님이 "+cost+"원 지불하셨습니다.");
			System.out.println(customer.getCustomerName()+" 님의 현재 보너스 포인트는 "+customer.bonusPoint+"점 입니다.");
		}
		
	}
}

'Java' 카테고리의 다른 글

[Java] 추상 클래스  (0) 2022.09.20
[Java] 다운캐스팅과 instanceof  (0) 2022.09.20
[Java] 다형성 - VIP 고객 클래스  (0) 2022.09.20
[Java] 다형성  (0) 2022.09.19
[Java] 메소드 오버라이딩  (0) 2022.09.19