Untitled

mail@pastecode.io avatar
unknown
java
a year ago
2.5 kB
1
Indexable
Never
package a02a.e1;

import java.util.HashMap;
import java.util.Map;

public class DietFactoryImpl implements DietFactory {

    @Override
    public Diet standard() {
        return new Diet() {

            Map<String, Map<Nutrient, Integer>> foods = new HashMap<>();
            @Override
            public void addFood(String name, Map<Nutrient, Integer> nutritionMap) {
                foods.put(name, nutritionMap);
            }

            @Override
            public boolean isValid(Map<String, Double> dietMap) {
                var sum = 0.0;
                var mul = 0.0;
                for (var food : foods.entrySet()) {
                    for (var diet : dietMap.entrySet()) {
                        if (diet.getKey().equals(food.getKey())) {
                            mul = diet.getValue() / 100;
                            sum += ( food.getValue().values().stream().reduce(0, Integer::sum).intValue() * mul );
                        }
                    }
                }
                return sum > 1500 && sum < 2000 ? true : false;
            }
            
        };
    }

    @Override
    public Diet lowCarb() {
        return new Diet() {

            Map<String, Map<Nutrient, Integer>> foods = new HashMap<>();
            @Override
            public void addFood(String name, Map<Nutrient, Integer> nutritionMap) {
                foods.put(name, nutritionMap);
            }

            @Override
            public boolean isValid(Map<String, Double> dietMap) {
                var sum = 0.0;
                var mul = 0.0;
                var carbs = 0.0;
                for (var food : foods.entrySet()) {
                    for (var diet : dietMap.entrySet()) {
                        if (diet.getKey().equals(food.getKey())) {
                            mul = diet.getValue() / 100;
                            sum += ( food.getValue().values().stream().reduce(0, Integer::sum).intValue() * mul );
                            carbs += ( food.getValue().get(Nutrient.CARBS).doubleValue() * mul ) ;
                        }
                    }
                }
                return (sum > 1500 && sum < 2000) && carbs <= 300 ? true : false;
            }
            
        };
    }

    @Override
    public Diet highProtein() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Diet balanced() {
        // TODO Auto-generated method stub
        return null;
    }

}