7
7
8
8
public class Electronics {
9
9
10
- private static final int DEFAULT_CAPACITY = 10 ; // Default initial capacity
11
10
private static final Electronic [] EMPTY_ELECTRONIC_LIST = {};
12
11
13
12
private static Electronic [] electronicList ;
@@ -16,7 +15,7 @@ public class Electronics {
16
15
private int size ;
17
16
private int capacity ;
18
17
19
- Electronics (){
18
+ private Electronics (){
20
19
electronicList = EMPTY_ELECTRONIC_LIST ;
21
20
}
22
21
@@ -41,12 +40,11 @@ public Optional<Electronic[]> groupByCompanyName(CompanyName company){
41
40
List <Electronic > temp = new ArrayList <>();
42
41
43
42
for (Electronic electronic : electronicList )
44
- // 테스트용 임시 조건 ( electronic != null )
45
- if (electronic != null && electronic .getCompanyName ().equals (company ))
43
+ if (electronic .getCompanyName ().equals (company ))
46
44
temp .add (electronic );
47
45
48
46
Electronic [] companyNameGroup =
49
- temp .isEmpty () ? null : ElectronicArrayUtil . listToArray (temp );
47
+ temp .isEmpty () ? null : listToArray (temp );
50
48
51
49
return Optional .ofNullable (companyNameGroup );
52
50
}
@@ -65,16 +63,24 @@ public Optional<Electronic[]> groupByAuthMethod(AuthMethod authMethod){
65
63
List <Electronic > temp = new ArrayList <>();
66
64
67
65
for (Electronic electronic : electronicList )
68
- // 테스트용 임시 조건 ( electronic != null )
69
- if (electronic != null && electronic .isContainAuthMethod (authMethod ))
66
+ if (electronic .isContainAuthMethod (authMethod ))
70
67
temp .add (electronic );
71
68
72
69
Electronic [] authMethodNameGroup =
73
- temp .isEmpty () ? null : ElectronicArrayUtil . listToArray (temp );
70
+ temp .isEmpty () ? null : listToArray (temp );
74
71
75
72
return Optional .ofNullable (authMethodNameGroup );
76
73
}
77
74
75
+ private Electronic [] listToArray (List <Electronic > list ){
76
+ Electronic [] array = new Electronic [list .size ()];
77
+
78
+ for (int i = 0 ; i < array .length ; i ++)
79
+ array [i ] = list .get (i );
80
+
81
+ return array ;
82
+ }
83
+
78
84
public int getSize () {
79
85
return size ;
80
86
}
@@ -110,31 +116,4 @@ public String toString() {
110
116
", capacity=" + capacity +
111
117
", electronicList= " + Arrays .toString (electronicList ) + " }" ;
112
118
}
113
-
114
- //==================================== TEST CODE ====================================//
115
- public void add (Electronic electronic ) {
116
- if (electronicList == EMPTY_ELECTRONIC_LIST )
117
- electronicList = new Electronic [DEFAULT_CAPACITY ];
118
- /* 배열 크기 체크하고 늘리는 로직 구현 할 것 */
119
- electronicList [size ++] = electronic ;
120
- }
121
-
122
- public static void main (String [] args ) {
123
- Electronic iPhone13 = new Electronic ("아이폰13" , CompanyName .APPLE , new AuthMethod []{AuthMethod .FACE , AuthMethod .PIN , AuthMethod .PATTERN });
124
- Electronic iPhone12 = new Electronic ("아이폰12" , CompanyName .APPLE , new AuthMethod []{AuthMethod .FACE , AuthMethod .PIN , AuthMethod .PATTERN });
125
- Electronic galaxyS22 = new Electronic ("갤럭시S22" , CompanyName .SAMSUNG , new AuthMethod []{AuthMethod .FINGERPRINT , AuthMethod .PIN , AuthMethod .PATTERN });
126
-
127
- Electronics electronics = getInstance ();
128
- electronics .add (iPhone13 );
129
- electronics .add (iPhone12 );
130
- electronics .add (galaxyS22 );
131
-
132
- // System.out.println(electronics);
133
-
134
- Optional <Electronic []> authMethodGroupPIN = electronics .groupByAuthMethod (AuthMethod .FACE );
135
- authMethodGroupPIN .ifPresent (value -> System .out .println (Arrays .toString (value )));
136
-
137
- Optional <Electronic []> companyNameGroupAPPLE = electronics .groupByCompanyName (CompanyName .SAMSUNG );
138
- companyNameGroupAPPLE .ifPresent (value -> System .out .println (Arrays .toString (value )));
139
- }
140
119
}
0 commit comments