-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_28_Example.java
More file actions
76 lines (68 loc) · 1.72 KB
/
_28_Example.java
File metadata and controls
76 lines (68 loc) · 1.72 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
package chap_07;
// 1) 물품 클래스
class JavaProduct1 {
int price;
int point;
JavaProduct1 () {}
JavaProduct1(int price) {
this.price = price;
this.point = price / 10;
}
}
// 2-1) 세부 물품 클래스
class JavaPhone1 extends JavaProduct1 {
JavaPhone1() {
super(200);
}
public String toString() {
return "자바폰";
}
}
// 2-2) 세부 물품 클래스
class JavaPad1 extends JavaProduct1 {
JavaPad1() {
super(100);
}
public String toString() {
return "자바패드";
}
}
// 2-3) 세부 물품 클래스
class JavaPods1 extends JavaProduct1 {
JavaPods1() {
super(50);
}
public String toString() {
return "자바팟";
}
}
// 3-1) 구매 고객 클래스
class Customer1 {
int budget = 1000;
int point = 0;
// 3-2) 물품 구매 메서드 정의
void buyProduct(JavaProduct1 jProduct) { // new JavaPhone(), new JavaPad(), new JavaPods()
if (budget < jProduct.price) {
System.out.println("예산 초과");
return;
}
budget -= jProduct.price;
point += jProduct.point;
System.out.println(jProduct.toString() + " 구매 완료"); // System.out.println(jProduct + " 구매 완료");
}
}
public class _28_Example {
public static void main(String[] args) {
// 4-1) 구매 고객 객체 생성
Customer1 c = new Customer1();
// 4-2) 물품 구매 메서드 호출
c.buyProduct(new JavaPhone1());
c.buyProduct(new JavaPad1());
c.buyProduct(new JavaPods1());
/*
자바폰 구매 완료
자바패드 구매 완료
자바팟 구매 완료
*/
}
}