-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashMap.java
More file actions
66 lines (52 loc) · 1.88 KB
/
Copy pathHashMap.java
File metadata and controls
66 lines (52 loc) · 1.88 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
//Варіант 4
//Управління інвентарем магазину
import java.util.HashMap;
public class Main {
static class Product {
String productCode;
String name;
int quantity;
public Product(String productCode, String name, int quantity) {
this.productCode = productCode;
this.name = name;
this.quantity = quantity;
}
@Override
public String toString() {
return productCode + " | " + name + " | Кількість: " + quantity;
}
}
static class Inventory {
HashMap<String, Product> products = new HashMap<>();
void addProduct(Product p) {
products.put(p.productCode, p);
}
void removeProduct(String code) {
products.remove(code);
}
Product findProduct(String code) {
return products.get(code);
}
void printAllProducts() {
for (Product p : products.values()) {
System.out.println(p);
}
}
}
public static void main(String[] args) {
Inventory inventory = new Inventory();
inventory.addProduct(new Product("A01", "Телефон", 10));
inventory.addProduct(new Product("B15", "Навушники", 25));
inventory.addProduct(new Product("C33", "Ноутбук", 5));
System.out.println("Усі товари:");
inventory.printAllProducts();
System.out.println("\nПошук C33:");
Product found = inventory.findProduct("C33");
if (found != null) System.out.println(found);
else System.out.println("Не знайдено");
System.out.println("\nВидалення B15...");
inventory.removeProduct("B15");
System.out.println("\nОновлений список:");
inventory.printAllProducts();
}
}