-
Notifications
You must be signed in to change notification settings - Fork 300
Expand file tree
/
Copy pathDinnerConstructor.java
More file actions
48 lines (41 loc) · 1.58 KB
/
DinnerConstructor.java
File metadata and controls
48 lines (41 loc) · 1.58 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
package ru.practicum.dinner;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;
public class DinnerConstructor {
private HashMap<String, ArrayList<String>> dishes;
private Random random;
public DinnerConstructor() {
dishes = new HashMap<>();
random = new Random();
}
// Method to add a new dish
public void addDish(String type, String name) {
dishes.putIfAbsent(type, new ArrayList<>());
dishes.get(type).add(name);
}
// Method to check if a dish type exists
public boolean checkType(String type) {
return dishes.containsKey(type);
}
// Method to generate dish combinations
public ArrayList<ArrayList<String>> generateCombinations(int numberOfCombos, ArrayList<String> types) {
ArrayList<ArrayList<String>> combinations = new ArrayList<>();
for (int i = 0; i < numberOfCombos; i++) {
ArrayList<String> combo = new ArrayList<>();
for (String type : types) {
if (checkType(type)) {
ArrayList<String> dishesOfType = dishes.get(type);
if (!dishesOfType.isEmpty()) {
String dish = dishesOfType.get(random.nextInt(dishesOfType.size()));
combo.add(dish);
}
} else {
System.out.println("Тип блюда " + type + " не существует. Введите другой тип.");
}
}
combinations.add(combo);
}
return combinations;
}
}