diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..4444b22 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..40e0791 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/dinner-constructor.iml b/dinner-constructor.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/dinner-constructor.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/out/production/dinner-constructor/ru/practicum/dinner/DinnerConstructor.class b/out/production/dinner-constructor/ru/practicum/dinner/DinnerConstructor.class new file mode 100644 index 0000000..447cc5a Binary files /dev/null and b/out/production/dinner-constructor/ru/practicum/dinner/DinnerConstructor.class differ diff --git a/out/production/dinner-constructor/ru/practicum/dinner/Main.class b/out/production/dinner-constructor/ru/practicum/dinner/Main.class new file mode 100644 index 0000000..dddb98b Binary files /dev/null and b/out/production/dinner-constructor/ru/practicum/dinner/Main.class differ diff --git a/src/ru/practicum/dinner/DinnerConstructor.java b/src/ru/practicum/dinner/DinnerConstructor.java index 29da0b5..2558b9c 100644 --- a/src/ru/practicum/dinner/DinnerConstructor.java +++ b/src/ru/practicum/dinner/DinnerConstructor.java @@ -1,5 +1,48 @@ package ru.practicum.dinner; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Random; + public class DinnerConstructor { + private HashMap> 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> generateCombinations(int numberOfCombos, ArrayList types) { + ArrayList> combinations = new ArrayList<>(); + for (int i = 0; i < numberOfCombos; i++) { + ArrayList combo = new ArrayList<>(); + for (String type : types) { + if (checkType(type)) { + ArrayList 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; + } } diff --git a/src/ru/practicum/dinner/Main.java b/src/ru/practicum/dinner/Main.java index f68de29..1480d5b 100644 --- a/src/ru/practicum/dinner/Main.java +++ b/src/ru/practicum/dinner/Main.java @@ -1,5 +1,6 @@ package ru.practicum.dinner; +import java.util.ArrayList; import java.util.Scanner; public class Main { @@ -24,6 +25,9 @@ public static void main(String[] args) { break; case "3": return; + default: + System.out.println("Invalid command"); + break; } } } @@ -41,7 +45,7 @@ private static void addNewDish() { System.out.println("Введите название блюда:"); String dishName = scanner.nextLine(); - // добавьте новое блюдо + dc.addDish(dishType, dishName); } private static void generateDishCombo() { @@ -49,17 +53,26 @@ private static void generateDishCombo() { System.out.println("Введите количество наборов, которые нужно сгенерировать:"); int numberOfCombos = scanner.nextInt(); - scanner.nextLine(); + while (numberOfCombos <= 0) { + System.out.println("Пожалуйста, введите положительное целое число."); + numberOfCombos = scanner.nextInt(); + } + scanner.nextLine(); // Consume newline + System.out.println("Вводите типы блюда, разделяя символом переноса строки (enter). Для завершения ввода введите пустую строку"); + ArrayList dishTypes = new ArrayList<>(); String nextItem = scanner.nextLine(); - //реализуйте ввод типов блюд while (!nextItem.isEmpty()) { - + dishTypes.add(nextItem); + nextItem = scanner.nextLine(); } - // сгенерируйте комбинации блюд и выведите на экран + ArrayList> combinations = dc.generateCombinations(numberOfCombos, dishTypes); + for (ArrayList combo : combinations) { + System.out.println("Комбинация: " + combo); + } } }