JAVA Programming/JAVA 문제

[38] 코딩해보세요 (상속,오버라이딩,업캐스팅,ArrayList 활용)

꾸준히개발하자 2020. 7. 16. 07:52

 

Customer 클래스 

package inheritance2;

public class Customer {
	private int customerID;
	protected String customerName;
	protected String customerGrade;
    int bonusPoint;
	double bonusRatio;

	public Customer() {
		initCustomer();
	}
	
	public Customer (int customerID , String customerName) { // 기본생성자에서 초기화 
		this.customerID = customerID;
		this.customerName = customerName;
		
		customerGrade = "SILVER";
		bonusRatio = 0.01; // 10 
	}
	
	private void initCustomer() {
		customerGrade = "SILVER";
		bonusRatio = 0.01;
	}
	
	public int calcPrice(int price) { // 구매에 따른 보너스포인트 적립 
		bonusPoint += price * bonusPoint;
		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;
	}

	public int getBonusPoint() {
		return bonusPoint;
	}

	public void setBonusPoint(int bonusPoint) {
		this.bonusPoint = bonusPoint;
	}

	public double getBonusRatio() {
		return bonusRatio;
	}

	public void setBonusRatio(double bonusRatio) {
		this.bonusRatio = bonusRatio;
	}
}

 

상속받은  VIPCustomer 클래스 

package inheritance2;

public class VIPCustomer extends Customer {

	// VIP 기능 추가
	private int agentID;
	double salesRatio;
	
	public VIPCustomer(int customerID , String customerName , int agentID) {
		super(customerID , customerName);
		
		customerGrade = "VIP";
		bonusRatio = 0.05; 
		salesRatio = 0.1; 
		this.agentID = agentID;
	}
	
	public int calcPrice(int price) {
		
		bonusPoint += price * bonusPoint;
		return price - (int)(price * salesRatio);
	}	
	public int getAgentID() {
		return agentID;
	}
	
	// 오버라이딩  VIP 에서  상담원 번호를 추가했다. 
	@Override
	public String showCustomerInfo() {
		
		return super.showCustomerInfo() + " 담당 상담원 번호는" + agentID +"입니다.";
	}

	public void setAgentID(int agentID) {
		this.agentID = agentID;
	}
}

GoldCustomer  

package inheritance2;

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

 

 

package inheritance2;

public class SilverCustomer extends Customer {
	
	// SILVER 추가해야할거
		double salesRatio;
		private int agentID;
		
		public SilverCustomer(int customerID , String customerName) {
			super(customerID , customerName);
			customerGrade = "SILVER"; // private 키워드는 외부클래스에서 접근할수없다. 그렇기 때문에  protected 를 주면 된다. 
			bonusRatio = 0.03;
			salesRatio = 0.05;		
		}
		public int calcPrice(int price) {
			price += price * bonusPoint;
			return price;
		}
	}

CustomerTest 클래스

 

package inheritance2;

import java.util.ArrayList;

public class CustomerTest {
	
	public static void main(String[] args) {
		ArrayList<Customer> customerList = new ArrayList<Customer>();
		
		Customer customerlee = new VIPCustomer(10001,"이순신",12345);
		Customer customeryee = new GoldCustomer(10002,"율곡이이");
		Customer customersee = new GoldCustomer(10003,"세종대왕");
		Customer customerkim = new SilverCustomer(10004,"김유신");
		
		customerList.add(customerlee);
		customerList.add(customeryee);
		customerList.add(customersee);
		customerList.add(customerkim);
		
		System.out.println("=======고객 정보 출력=========");
		
		for(Customer cus : customerList) { 
			System.out.println(cus.showCustomerInfo());
		}
		
		
		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 + "점 입니다.");
		}
	}
}