๋ฐ์ํ
โถ ์ผ๋ฐ ๊ณ ๊ฐ๊ณผ VIP ๊ณ ๊ฐ์ ์ค๊ฐ ๋ฑ๊ธ ๋ง๋ค๊ธฐ
package witharraylist;
public class GoldCustomer extends Customer {
double saleRatio;
public GoldCustomer(int customerID, String customerName) {
super(customerID, customerName);
customerGrade = "GOLD";
bonusRatio = 0.02;
saleRatio = 0.1;
}
@Override
public int calcPrice(int price) { // ์ฌ์ ์๋ ๋ฉ์๋
bonusPoint += price * bonusRatio;
return price - (int) (price * saleRatio);
}
}
-Customer ํด๋์ค๋ฅผ ํจํค์ง๋ก ๋ณต์ฌํด์ฌ๊ฒ.
โ
โถ ๋ฐฐ์ด๋ก ๊ณ ๊ฐ 5๋ช ๊ตฌํํ๊ธฐ
package witharraylist;
import java.util.ArrayList;
public class CustomerTest {
public static void main(String[] args) {
ArrayList<Customer> customerList = new ArrayList<Customer>();
Customer customerLee = new Customer(10010, "์ด์์ ");
Customer customerShin = new Customer(10020, "์ ์ฌ์๋น");
Customer customerHong = new GoldCustomer(10030, "ํ๊ธธ๋");
Customer customerYul = new GoldCustomer(10040, "์ด์จ๊ณก");
Customer customerKim = new VIPCustomer(10050, "๊น์ ์ ", 12345);
customerList.add(customerLee);
customerList.add(customerShin);
customerList.add(customerHong);
customerList.add(customerYul);
customerList.add(customerKim);
System.out.println("====== ๊ณ ๊ฐ ์ ๋ณด ์ถ๋ ฅ =======");
for (Customer customer : customerList) {
System.out.println(customer.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 + "์ ์
๋๋ค.");
}
}
}
<์คํ ๊ฒฐ๊ณผ>
๋ฐ์ํ
'Java' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Java] ์ค์ต (0) | 2021.02.20 |
---|---|
[Java] ๋ค์ด ์บ์คํ ๊ณผ instanceof (0) | 2021.02.20 |
[Java] ๋คํ์ฑ (0) | 2021.02.19 |
[Java] ๊ฐ์ ๋ฉ์๋ (0) | 2021.02.19 |
[Java] ๋ฌต์์ ํด๋์ค ํ ๋ณํ๊ณผ ๋ฉ์๋ ์ฌ์ ์ (0) | 2021.02.19 |
๋๊ธ