-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathElectronics.java
More file actions
119 lines (87 loc) · 3.67 KB
/
Electronics.java
File metadata and controls
119 lines (87 loc) · 3.67 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package me.day05.practice;
import me.day05.practice.Electronic.AuthMethod;
import me.day05.practice.Electronic.CompanyName;
import java.util.*;
public class Electronics {
private static final Electronic[] EMPTY_ELECTRONIC_LIST = {};
private static Electronic[] electronicList;
private static Electronics electronicsInstance;
private int size;
private int capacity;
private Electronics(){
electronicList = EMPTY_ELECTRONIC_LIST;
}
// TODO: 1. Electronics 클래스의 객체를 싱글톤으로 생성하는 함수를 작성하시오.
public static Electronics getInstance() {
if (electronicsInstance == null) electronicsInstance = new Electronics();
return electronicsInstance;
}
// TODO: 2. 전자제품 일련번호 productNo를 통해 인자로 주어진 일련번호에 해당하는 전자제품을 반환하는 함수를 작성하시오.
public Optional<Electronic> findByProductNo(String productNo){
for (Electronic electronic : electronicList)
if (productNo.equals(electronic.getProductNo()))
return Optional.of(electronic);
return Optional.empty();
}
// TODO: 3. 전자제품들 중 인자로 주어진 제조 회사를 찾아서 하나의 배열에 반환하는 함수를 작성하시오.
public Optional<Electronic[]> groupByCompanyName(CompanyName company){
List<Electronic> temp = new ArrayList<>();
for (Electronic electronic : electronicList)
if (electronic.getCompanyName().equals(company))
temp.add(electronic);
Electronic[] companyNameGroup =
temp.isEmpty() ? null : listToArray(temp);
return Optional.ofNullable(companyNameGroup);
}
public Optional<Electronic> findByCompanyName(CompanyName company){
for (Electronic electronic : electronicList)
if (company.equals(electronic.getCompanyName()))
return Optional.of(electronic);
return Optional.empty();
}
// TODO: 4. 전자제품들 중 인자로 주어진 인증 방법을 찾아서 하나의 배열에 반환하는 함수를 작성하시오.
public Optional<Electronic[]> groupByAuthMethod(AuthMethod authMethod){
List<Electronic> temp = new ArrayList<>();
for (Electronic electronic : electronicList)
if (electronic.isContainsAuthMethod(authMethod))
temp.add(electronic);
Electronic[] authMethodNameGroup =
temp.isEmpty() ? null : listToArray(temp);
return Optional.ofNullable(authMethodNameGroup);
}
private Electronic[] listToArray(List<Electronic> list){
Electronic[] array = new Electronic[list.size()];
for (int i = 0; i < array.length; i++)
array[i] = list.get(i);
return array;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public int getCapacity() {
return capacity;
}
public void setCapacity(int capacity) {
this.capacity = capacity;
}
@Override
public int hashCode() {
return Objects.hash(Arrays.hashCode(electronicList), size, capacity);
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
return Objects.equals(hashCode(), ((Electronics)obj).hashCode());
}
@Override
public String toString() {
return "Users { " +
"size=" + size +
", capacity=" + capacity +
", electronicList= " + Arrays.toString(electronicList) + " }";
}
}