Пример #1
0
 public Recipe() {
   random = new Random();
   ingredient1 = new Ingredient((char) (65 + random.nextInt(5)));
   ingredient2 = new Ingredient((char) (65 + random.nextInt(5)));
   while (ingredient2.equals(ingredient1))
     ingredient2 = new Ingredient((char) (65 + random.nextInt(5)));
   cookingTime = 2000 + random.nextInt(10000);
   ingredient1.setQuantity(2 + random.nextInt(9));
   ingredient2.setQuantity(2 + random.nextInt(9));
 }
 @Override
 public Set<Ingredient> getAllIngredients() {
   boolean ingredientIncluded = false;
   Set<Ingredient> allIngredients = new HashSet<Ingredient>();
   allIngredients.clear();
   if (fullMenu != null && !fullMenu.isEmpty()) {
     for (Dish dish : fullMenu) {
       Set<Ingredient> newIngredients = dish.getIngredients();
       if (newIngredients != null && !newIngredients.isEmpty()) {
         if (allIngredients != null && !allIngredients.isEmpty()) {
           for (Ingredient newIngredient : newIngredients) {
             Iterator<Ingredient> iterator = allIngredients.iterator();
             ingredientIncluded = false;
             while (iterator.hasNext() && ingredientIncluded == false) {
               Ingredient existingIngredient = iterator.next();
               if (existingIngredient.getName().equals(newIngredient.getName())) {
                 existingIngredient.setQuantity(
                     existingIngredient.getQuantity()
                         + newIngredient.getQuantity() * numberOfGuests);
                 existingIngredient.setPrice(
                     existingIngredient.getPrice() + newIngredient.getPrice() * numberOfGuests);
                 allIngredients.remove(existingIngredient);
                 allIngredients.add(
                     new Ingredient(
                         existingIngredient.getName(),
                         existingIngredient.getQuantity(),
                         existingIngredient.getUnit(),
                         existingIngredient.getPrice()));
                 ingredientIncluded = true;
               }
             }
             if (ingredientIncluded == false) {
               allIngredients.add(newIngredient);
             }
           }
         } else {
           Iterator<Ingredient> iterator = newIngredients.iterator();
           while (iterator.hasNext()) {
             Ingredient newIngredient = iterator.next();
             allIngredients.add(
                 new Ingredient(
                     newIngredient.getName(),
                     newIngredient.getQuantity() * numberOfGuests,
                     newIngredient.getUnit(),
                     newIngredient.getPrice() * numberOfGuests));
           }
         }
       }
     }
     return allIngredients;
   }
   return null;
 }