๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Java

[Java] ๋‹คํ˜•์„ฑ ํ™œ์šฉํ•˜๊ธฐ

by ์ฝ”๋”ฉํ•˜๋Š” ๋ถ•์–ด 2021. 2. 20.
๋ฐ˜์‘ํ˜•

โ–ถ ์ผ๋ฐ˜ ๊ณ ๊ฐ๊ณผ 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 + "์ ์ž…๋‹ˆ๋‹ค.");
		}
	}
}

 

<์‹คํ–‰ ๊ฒฐ๊ณผ>

 

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€