Created
March 31, 2022 06:17
-
-
Save crisine/7a0af060a0b83291af080d20b7c888f0 to your computer and use it in GitHub Desktop.
Customer 프로젝트 다형성 활용
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package customerpkg; | |
public class Customer { | |
protected 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; | |
initCustomer(); | |
} | |
private void initCustomer() { | |
customerGrade = "SILVER"; | |
bonusRatio = 0.01; | |
} | |
public int calcPrice(int price) { | |
bonusPoint += price * bonusRatio; | |
return price; | |
} | |
public void setCustomerID(int customerID) { | |
this.customerID = customerID; | |
} | |
public int getCustomerID() { | |
return this.customerID; | |
} | |
public String showCustomerInfo() { | |
return customerName + "님의 등급은 " + customerGrade + "이며, 보너스 포인트는 " + bonusPoint + "입니다."; | |
} | |
public void setCustomerName(String customerName) { | |
this.customerName = customerName; | |
} | |
public String getCustomerName() { | |
return this.customerName; | |
} | |
public void setCustomerGrade(String customerGrade) { | |
this.customerGrade = customerGrade; | |
} | |
public String getCustomerGrade() { | |
return this.customerGrade; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package customerpkg; | |
import java.util.ArrayList; | |
public class CustomerTest { | |
public static void main(String[] args) { | |
ArrayList<Customer> customerList = new ArrayList<Customer>(); | |
Customer customerKim = new Customer(1, "김선생"); | |
Customer customerLee = new Customer(2, "이선생"); | |
Customer customerChoi = new GoldCustomer(3, "최선생"); | |
Customer customerPark = new GoldCustomer(4, "박선생"); | |
Customer customerJung = new VIPCustomer(5, "정선생", 777); | |
customerList.add(customerKim); | |
customerList.add(customerLee); | |
customerList.add(customerChoi); | |
customerList.add(customerPark); | |
customerList.add(customerJung); | |
System.out.println("=====고객 정보 출력====="); | |
for (Customer c : customerList) { | |
System.out.println(c.showCustomerInfo()); | |
} | |
System.out.println("=====할인률과 보너스 포인트 계산====="); | |
for (Customer c : customerList) { | |
int cost = c.calcPrice(10000); | |
System.out.println(c.getCustomerName() + " 님이 " + cost + "원 지불하셨습니다."); | |
System.out.println(c.getCustomerName() + " 님의 현재 보너스 포인트는 " + c.bonusPoint + "점입니다."); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package customerpkg; | |
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package customerpkg; | |
public class VIPCustomer extends Customer { | |
private int agentID; | |
double saleRatio; | |
public VIPCustomer(int customerID, String customerName, int agentID) { | |
super (customerID, customerName); | |
customerGrade = "VIP"; | |
bonusRatio = 0.05; | |
saleRatio = 0.1; | |
this.agentID = agentID; | |
} | |
public int calcPrice(int price) { | |
bonusPoint += price * bonusRatio; | |
return price - (int)(price * saleRatio); | |
} | |
public String showCustomerInfo() { | |
return super.showCustomerInfo() + " 담당 상담원 번호는 " + agentID + "입니다."; | |
} | |
public int getAgentID() { | |
return agentID; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment