-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOperationWithNotebook.java
More file actions
247 lines (192 loc) · 8.42 KB
/
OperationWithNotebook.java
File metadata and controls
247 lines (192 loc) · 8.42 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package ru.MylearnCh1J1L1;
import java.util.*;
public class OperationWithNotebook {
private Set<Laptop> notebooks = new HashSet<>();
private List<Criterion> criterionList = new ArrayList<>();
private static Scanner scanner = new Scanner(System.in);
public void printList(){
for (Laptop notebook : notebooks){
if (notebookIsCorrect(notebook)){
System.out.println(notebook);
}
}
}
public boolean notebookIsCorrect(Laptop notebook){
for (Criterion criterion : criterionList){
Object valueNotebook = null;
if (criterion.property.equals("name")){
valueNotebook = notebook.getName();
}else if (criterion.property.equals("amountRAM")){
valueNotebook = notebook.getAmountRAM();
}else if (criterion.property.equals("operatingSystem")){
valueNotebook = notebook.getOperatingSystem();
}else if (criterion.property.equals("price")){
valueNotebook = notebook.getPrice();
}else if (criterion.property.equals("model")){
valueNotebook = notebook.getModel();
}else {
continue;
}
if (criterion.value != null && !criterion.value.equals(valueNotebook)){
return false;
}
if (criterion.maxValue != null && criterion.maxValue < Double.parseDouble(Objects.toString(valueNotebook))){
return false;
}
if (criterion.minValue != null && criterion.minValue > Double.parseDouble(Objects.toString(valueNotebook))){
return false;
}
}
return true;
}
public OperationWithNotebook(Set<Laptop> notebooks) {
this.scanner = new Scanner(System.in);
this.notebooks = notebooks;
}
public OperationWithNotebook(Set<Laptop> notebooks, List<Criterion> criterionList) {
this.scanner = new Scanner(System.in);
this.notebooks = notebooks;
this.criterionList = criterionList;
}
public int getCriteria(){
String text = "Введите цифру, соответствующую необходимому критерию: ";
List<String> properties = propertiesForFilter();
for (int i = 0; i < properties.size(); i++)
{
text += "\n" + (i + 1) + ". " + getPropertyDescription(properties.get(i));
}
System.out.println(text);
int value = scanner.nextInt();
return value;
}
public String getPropertyDescription(String property){
Map<String, String> descriptionsProperties = descriptionsProperties();
return descriptionsProperties.get(property);
}
public Map<String, String> descriptionsProperties(){
Map<String, String> map = new HashMap<>();
map.put("name", "Наименование");
map.put("amountRAM", "Объем оперативной памяти");
map.put("operatingSystem", "Операционная система");
map.put("price", "цена");
map.put("model", "модель");
return map;
}
public List<String> propertiesForFilter(){
List<String> list = new ArrayList<>();
list.add("name");
list.add("amountRAM");
list.add("operatingSystem");
list.add("price");
list.add("model");
return list;
}
public String getOperations(){
String text = "Выберите опрерацию: \n " +
"1. Добавить критерий \n " +
"2. Вывести список \n " +
"3. Завершить";
System.out.println(text);
String answer = scanner.next();
return answer;
}
public Set<String> quantitativeSelection(){
Set<String> set = new HashSet<>();
set.add("amountRAM");
set.add("price");
return set;
}
public Set<String> stringSelection(){
Set<String> set = new HashSet<>();
set.add("name");
set.add("operatingSystem");
set.add("model");
return set;
}
public void start(){
boolean flag = true;
while (flag){
String operation = getOperations();
if (operation.equals("3")){
flag = false;
scanner.close();
continue;
}else if(operation.equals("1")){
int criterion = getCriteria();
List<String> properties = propertiesForFilter();
if (criterion - 1 < 0 || criterion - 1 > properties.size() - 1){
System.out.println("Введено некорректное значение");
continue;
}
String property = properties.get(criterion - 1);
Criterion criterionObject = null;
try {
if (quantitativeSelection().contains(property)){
criterionObject = Criterion.startGetting(scanner, property, true);
}else {
criterionObject = Criterion.startGetting(scanner, property, false);
}
}catch (Exception e){
System.out.println("Ошибка при выборе критерия");
continue;
}
if (criterionObject != null){
System.out.println("Критерий добавлен");
criterionList.add(criterionObject);
}
}
else if (operation.equals("2")){
printList();
}
}
}
}
class Criterion {
Object value;
Double minValue;
Double maxValue;
boolean isQuantitative;
String property;
public Criterion(String property, boolean isQuantitative, Object value, Double minValue, Double maxValue) {
this.property = property;
this.isQuantitative = isQuantitative;
this.value = value;
this.minValue = minValue;
this.maxValue = maxValue;
}
public static Criterion startGetting(Scanner scanner, String property, boolean isQuantitative) {
if (isQuantitative) {
String quest = "Выберите тип криетрия: " +
"\n 1. Значение" +
"\n 2. Меньше" +
"\n 3. Больше" +
"\n 4. Интервал";
System.out.println(quest);
String text = scanner.next();
Criterion criterion = null;
if (text.equals("1")) {
System.out.println("Введите значение поиска: ");
int getValue = scanner.nextInt();
criterion = new Criterion(property, isQuantitative, getValue, null, null);
} else if (text.equals("2")) {
System.out.println("Введите максимальное предельное значение: ");
double getValue = scanner.nextDouble();
criterion = new Criterion(property, isQuantitative, null, null, getValue);
} else if (text.equals("3")) {
System.out.println("Введите минимальное предельное значение: ");
double getValue = scanner.nextDouble();
criterion = new Criterion(property, isQuantitative, null, getValue, null);
} else if (text.equals("4")) {
System.out.println("Введите минимальное предельное значение: ");
double getMin = scanner.nextDouble();
System.out.println("Введите максимальное предельное значение: ");
double getMax = scanner.nextDouble();
criterion = new Criterion(property, isQuantitative, null, getMin, getMax);
}
return criterion;
}
System.out.println("Введите значение поиска: ");
String getValue = scanner.next();
return new Criterion(property, isQuantitative, getValue, null, null);
}
}