-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_30_Example.java
More file actions
101 lines (91 loc) · 2.51 KB
/
_30_Example.java
File metadata and controls
101 lines (91 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package chap_07;
// 1) 물품 클래스
class JavaProduct2 {
int price;
int point;
JavaProduct2 () {}
JavaProduct2(int price) {
this.price = price;
this.point = price / 10;
}
}
// 2-1) 세부 물품 클래스
class JavaPhone2 extends JavaProduct2 {
JavaPhone2() {
super(200);
}
public String toString() {
return "자바폰";
}
}
// 2-2) 세부 물품 클래스
class JavaPad2 extends JavaProduct2 {
JavaPad2() {
super(100);
}
public String toString() {
return "자바패드";
}
}
// 2-3) 세부 물품 클래스
class JavaPods2 extends JavaProduct2 {
JavaPods2() {
super(50);
}
public String toString() {
return "자바팟";
}
}
// 3-1) 구매 고객 클래스
class Customer2 {
int budget = 1000;
int point = 0;
// 3-2) 장바구니 배열 생성
JavaProduct2[] cart = new JavaProduct2[3];
int i = 0; // 배열 인덱스
// 3-3) 물품 구매 메서드 정의
void buyProduct(JavaProduct2 jProduct) { // new JavaPhone(), new JavaPad(), new JavaPods()
if (budget < jProduct.price) {
System.out.println("예산 초과");
return;
}
budget -= jProduct.price;
point += jProduct.point;
// 3-4) 물품 구매 시 장바구니에 저장
cart[i++] = jProduct;
System.out.println(jProduct + " 구매 완료"); // System.out.println(jProduct.toString() + " 구매 완료");
}
// 3-5) 구매 정보 요약 메서드 정의
void printSummary() {
int sum = 0;
for (int i = 0; i < cart.length; i++) {
if (cart[i] == null) {
break;
}
sum += cart[i].price;
}
System.out.println("총 구매 비용: " + sum);
System.out.println("남은 잔액: " + budget);
System.out.println("적립 점수: " + point);
}
}
public class _30_Example {
public static void main(String[] args) {
// 4-1) 구매 고객 객체 생성
Customer2 c = new Customer2();
// 4-2) 물품 구매 메서드 호출
c.buyProduct(new JavaPhone2());
c.buyProduct(new JavaPad2());
c.buyProduct(new JavaPods2());
// 4-3) 구매 정보 요약 메서드 호출
c.printSummary();
/*
자바폰 구매 완료
자바패드 구매 완료
자바팟 구매 완료
총 구매 비용: 350
남은 잔액: 650
적립 점수: 35
*/
}
}