|
1 | 1 | package ru.practicum.dinner; |
2 | 2 |
|
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.HashMap; |
| 5 | +import java.util.List; |
| 6 | +import java.util.Map; |
| 7 | +import java.util.Random; |
| 8 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 9 | +import java.util.stream.IntStream; |
| 10 | + |
3 | 11 | public class DinnerConstructor { |
4 | 12 |
|
| 13 | + private final DinnerDao dao; |
| 14 | + private final Random random; |
| 15 | + |
| 16 | + public DinnerConstructor(DinnerDao dao) { |
| 17 | + this.dao = dao; |
| 18 | + this.random = new Random(); |
| 19 | + } |
| 20 | + |
| 21 | + public void save(String dishType, String dishName) { |
| 22 | + dao.save(dishType, dishName); |
| 23 | + System.out.printf("новое блюдо %s добавлено в тип %s\n", dishName, dishType); |
| 24 | + } |
| 25 | + |
| 26 | + public Map<String, List<String>> generateDishCombo(List<String> dishTypes, int numberOfCombos) { |
| 27 | + Map<String, List<String>> dishesByTypes = dao.getAllDishesByTypes(dishTypes); |
| 28 | + Map<String, List<String>> combos = new HashMap<>(); |
| 29 | + |
| 30 | + IntStream.range(0, numberOfCombos) |
| 31 | + .forEach(i -> { |
| 32 | + List<String> comboDishes = new ArrayList<>(); |
| 33 | + dishesByTypes.values().forEach(dishList -> { |
| 34 | + String randomDish = dishList.get(random.nextInt(dishList.size())); |
| 35 | + comboDishes.add(randomDish); |
| 36 | + }); |
| 37 | + combos.put("Комбо " + (i + 1), comboDishes); |
| 38 | + }); |
| 39 | + return combos; |
| 40 | + } |
| 41 | + |
| 42 | + public boolean isNotContainsTypes(List<String> dishTypes) { |
| 43 | + AtomicBoolean result = new AtomicBoolean(false); |
| 44 | + dao.isNotContainsTypes(dishTypes) |
| 45 | + .ifPresent((d) -> { |
| 46 | + System.out.printf("Вы ввели несуществующий тип: %s", d); |
| 47 | + System.out.println(); |
| 48 | + System.out.println("Введите список существующих типов:"); |
| 49 | + result.set(true); |
| 50 | + }); |
| 51 | + return result.get(); |
| 52 | + } |
| 53 | + public void printDishCombos(Map<String, List<String>> combos) { |
| 54 | + combos.entrySet() |
| 55 | + .stream() |
| 56 | + .sorted((e1, e2) -> { |
| 57 | + String key1 = e1.getKey().replaceAll("\\D", ""); |
| 58 | + String key2 = e2.getKey().replaceAll("\\D", ""); |
| 59 | + return Integer.compare(Integer.parseInt(key1), Integer.parseInt(key2)); |
| 60 | + }) |
| 61 | + .forEach((e) -> System.out.println(e.getKey() + "\n" + e.getValue())); |
| 62 | + } |
5 | 63 | } |
0 commit comments