-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLaptopStore.java
More file actions
125 lines (110 loc) · 8.16 KB
/
LaptopStore.java
File metadata and controls
125 lines (110 loc) · 8.16 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
import java.util.*;
class Laptop {
private int ram; // ОЗУ в ГБ
private int hdd; // Объем ЖД в ГБ
private String os; // Операционная система
private String color; // Цвет
public Laptop(int ram, int hdd, String os, String color) {
this.ram = ram;
this.hdd = hdd;
this.os = os;
this.color = color;
}
public int getRam() {
return ram;
}
public int getHdd() {
return hdd;
}
public String getOs() {
return os;
}
public String getColor() {
return color;
}
@Override
public String toString() {
return "Laptop{" +
"ram=" + ram + "gb" +
", hdd=" + hdd +
"gb, os='" + os + '\'' +
", color='" + color + '\'' +
'}';
}
}
public class LaptopStore {
public static void main(String[] args) {
Set<Laptop> laptops = new HashSet<>(Arrays.asList(
new Laptop(8, 256, "Windows", "Black"),
new Laptop(16, 512, "Windows", "Silver"),
new Laptop(8, 256, "MacOS", "Gray"),
new Laptop(16, 1024, "Linux", "Black"),
new Laptop(32, 2048, "Windows", "White")
));
filterLaptops(laptops);
}
public static void filterLaptops(Set<Laptop> laptops) {
Scanner scanner = new Scanner(System.in);
Map<String, Object> filters = new HashMap<>();
System.out.println("Введите цифру, соответствующую необходимому критерию:");
System.out.println("1 - ОЗУ");
System.out.println("2 - Объем ЖД");
System.out.println("3 - Операционная система");
System.out.println("4 - Цвет");
int criterion = scanner.nextInt();
scanner.nextLine(); // Очистить буфер
switch (criterion) {
case 1:
System.out.println("Введите минимальный объем ОЗУ (в ГБ):");
filters.put("ram", scanner.nextInt());
break;
case 2:
System.out.println("Введите минимальный объем ЖД (в ГБ):");
filters.put("hdd", scanner.nextInt());
break;
case 3:
System.out.println("Введите операционную систему:");
filters.put("os", scanner.nextLine());
break;
case 4:
System.out.println("Введите цвет:");
filters.put("color", scanner.nextLine());
break;
default:
System.out.println("Некорректный критерий.");
return;
}
Set<Laptop> filteredLaptops = filterByCriteria(laptops, filters);
System.out.println("Ноутбуки, подходящие под критерии:");
for (Laptop laptop : filteredLaptops) {
System.out.println(laptop);
}
}
public static Set<Laptop> filterByCriteria(Set<Laptop> laptops, Map<String, Object> filters) {
Set<Laptop> result = new HashSet<>();
for (Laptop laptop : laptops) {
boolean matches = true;
for (Map.Entry<String, Object> entry : filters.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
switch (key) {
case "ram":
if (laptop.getRam() < (int) value) matches = false;
break;
case "hdd":
if (laptop.getHdd() < (int) value) matches = false;
break;
case "os":
if (!laptop.getOs().equalsIgnoreCase((String) value)) matches = false;
break;
case "color":
if (!laptop.getColor().equalsIgnoreCase((String) value)) matches = false;
break;
}
if (!matches) break;
}
if (matches) result.add(laptop);
}
return result;
}
}