Skip to content

Commit 503bee9

Browse files
authored
Create Customer.java
1 parent f526bba commit 503bee9

File tree

1 file changed

+222
-0
lines changed

1 file changed

+222
-0
lines changed

BecomingFunctional/Customer.java

Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
package chapter3;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class Customer {
7+
// 모든 고객 정보리스트
8+
static public ArrayList<Customer> allCustomers = new ArrayList<Customer>();
9+
public Integer id = 0;
10+
public String name = "";
11+
public String address = "";
12+
public String state = "";
13+
public String primaryContact = "";
14+
public String domain = "";
15+
public Boolean enabled = true;
16+
public Contract contract;
17+
18+
public Customer() {
19+
}
20+
21+
/**
22+
* 초기화
23+
*/
24+
static {
25+
for (int i = 0; i < 10; i++) {
26+
Customer customer = new Customer();
27+
customer.name = "name : " + i;
28+
customer.address = "address : " + i;
29+
customer.state = "state : " + i;
30+
customer.primaryContact = "primaryContact : " + i;
31+
customer.domain = "domain : " + i;
32+
customer.enabled = i % 2 == 0;
33+
allCustomers.add(customer);
34+
}
35+
}
36+
37+
public Customer setCustomerId(Integer customer_id) {
38+
this.id = customer_id;
39+
return this;
40+
}
41+
42+
public Customer setName(String name) {
43+
this.name = name;
44+
return this;
45+
}
46+
47+
public Customer setState(String state) {
48+
this.state = state;
49+
return this;
50+
}
51+
52+
public Customer setDomain(String domain) {
53+
this.domain = domain;
54+
return this;
55+
}
56+
57+
public Customer setEnabled(Boolean enabled) {
58+
this.enabled = enabled;
59+
return this;
60+
}
61+
62+
public Customer setContract(Contract contract) {
63+
this.contract = contract;
64+
return this;
65+
}
66+
67+
/**
68+
* 고객정보를 가지고오는 정보 클래스
69+
*/
70+
public static List<String> getEnabledCustomerAddresses() {
71+
return getEnabledCustomerField(new Function1<Customer, String>() {
72+
public String call(Customer customer) {
73+
return customer.address;
74+
}
75+
});
76+
}
77+
78+
public static List<String> getEnabledCustomerNames() {
79+
return getEnabledCustomerField(new Function1<Customer, String>() {
80+
public String call(Customer customer) {
81+
return customer.name;
82+
}
83+
});
84+
}
85+
86+
public static List<String> getEnabledCustomerStates() {
87+
return getEnabledCustomerField(new Function1<Customer, String>() {
88+
public String call(Customer customer) {
89+
return customer.state;
90+
}
91+
});
92+
}
93+
94+
public static List<String> getEnabledCustomerPrimaryContacts() {
95+
return getEnabledCustomerField(new Function1<Customer, String>() {
96+
public String call(Customer customer) {
97+
return customer.primaryContact;
98+
}
99+
});
100+
}
101+
102+
public static List<String> getEnabledCustomerDomains() {
103+
return getEnabledCustomerField(new Function1<Customer, String>() {
104+
public String call(Customer customer) {
105+
return customer.domain;
106+
}
107+
});
108+
}
109+
110+
public static <B> List<B> getEnabledCustomerField(Function1<Customer, B> func) {
111+
ArrayList<B> outList = new ArrayList<B>();
112+
for (Customer customer : Customer.allCustomers) {
113+
if (customer.enabled) {
114+
outList.add(func.call(customer));
115+
}
116+
}
117+
return outList;
118+
}
119+
120+
/**
121+
* id에 해당하는 고객이 존재하면 반환하고, 그렇지 않으면 null을 반환하는 간단한 메소드
122+
*
123+
* @param customer_id
124+
* @return
125+
*/
126+
public static Customer getCustomerById(Integer customer_id) {
127+
for (Customer customer : Customer.allCustomers) {
128+
if (customer.id == customer_id) {
129+
return customer;
130+
}
131+
}
132+
// 이걸 대체 무슨 의미로 받아들여야 할까요?
133+
// - 에러인가? 못찾았다는 것인가?
134+
return null;
135+
}
136+
137+
/**
138+
* 프로그램 오류는 아닌데도 호출자에게 ‘고객을 찾지 못했으니 예외를 던지는 거 야’라고 말하는 건 좀 이상하므로 위의 getCustomerById 보완
139+
*
140+
* @param customer_id
141+
* @return
142+
*/
143+
public static ArrayList<Customer> getCustomerListById(Integer customer_id) {
144+
ArrayList<Customer> outList = new ArrayList<Customer>();
145+
for (Customer customer : Customer.allCustomers) {
146+
if (customer.id == customer_id) {
147+
outList.add(customer);
148+
}
149+
}
150+
return outList;
151+
}
152+
153+
/**
154+
* 일급함수이지만 순수함수는 아니다. 참조된 allCustomers값에 의해서 변경될 소지가 있기 때문이다
155+
*
156+
* @param func
157+
* @return
158+
*/
159+
public static ArrayList<Customer> filter(Function1<Customer, Boolean> func) {
160+
ArrayList<Customer> outList = new ArrayList<Customer>();
161+
for (Customer customer : Customer.allCustomers) {
162+
if (func.call(customer)) {
163+
outList.add(customer);
164+
}
165+
}
166+
return outList;
167+
}
168+
169+
/**
170+
* filter 메소드를 이용해서 getCustomerListById 개선한 메소드
171+
* 함수를 더 작은 순수 함수들로 나누어 작성하면 코드의 전반적인 기능을 잘 이해하고 파악하는 데 도움이 될 수 있음
172+
*
173+
* @param customer_id
174+
* @return
175+
*/
176+
public static ArrayList<Customer> getCustomerListFilterById(final Integer customer_id) {
177+
return Customer.filter(new Function1<Customer, Boolean>() {
178+
public Boolean call(Customer customer) {
179+
return customer.id == customer_id;
180+
}
181+
});
182+
}
183+
184+
/**
185+
*
186+
* @param function1
187+
* @param function2
188+
* @param <B>
189+
* @return
190+
*/
191+
public static <B> List<B> getField(Function1<Customer, Boolean> function1, Function1<Customer, B> function2) {
192+
ArrayList<B> outList = new ArrayList<B>();
193+
for (Customer customer : Customer.filter(function1)) {
194+
outList.add(function2.call(customer));
195+
}
196+
return outList;
197+
}
198+
199+
// 테스트
200+
public static void main(String[] args) {
201+
List<String> addresses = getEnabledCustomerAddresses();
202+
for (String address : addresses) {
203+
System.out.print(address + " ");
204+
}
205+
System.out.println();
206+
List<String> names = getField(
207+
new Function1<Customer, Boolean>() {
208+
@Override
209+
public Boolean call(Customer in1) {
210+
return in1.enabled;
211+
}
212+
}, new Function1<Customer, String>() {
213+
@Override
214+
public String call(Customer in1) {
215+
return in1.name;
216+
}
217+
});
218+
for (String name : names) {
219+
System.out.print(name + " ");
220+
}
221+
}
222+
}

0 commit comments

Comments
 (0)