Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
3.1 kB
2
Indexable
Never
import java.util.*;

public class ShoppingList {
    private boolean isCheckingRecipe;
    private TreeMap<String, Integer> available;
    private TreeMap<String, Integer> recipe;

    public ShoppingList() {
        available = new TreeMap<String, Integer>();
        recipe = new TreeMap<String, Integer>();
        isCheckingRecipe = false;
    }

    public void read(String[] lines) {
        Scanner scanner = new Scanner(System.in);
        String line = scanner.nextLine();

        int index = 0;
        while (scanner.hasNextLine()) {

            line = scanner.nextLine();

            String[] lineArray = line.split(" ");

            if (lineArray.length != 3) {
                continue;
            }

            if (lineArray[0].equals("RECIPE")) {
                isCheckingRecipe = true;
                continue;
            }

            if (!isCheckingRecipe) {
                String amount = lineArray[0];
                String unit = lineArray[1];
                String ingredient = lineArray[2];
                if (unit.equals("kg")) {
                    int amountInGrams = kgToG(Integer.parseInt(amount));
                    available.put(ingredient, amountInGrams);
                } else {
                    available.put(ingredient, Integer.parseInt(amount));
                }
            } else {
                String amount = lineArray[0];
                String unit = lineArray[1];
                String ingredient = lineArray[2];
                if (unit.equals("kg")) {
                    int amountInGrams = kgToG(Integer.parseInt(amount));
                    recipe.put(ingredient, amountInGrams);
                } else {
                    recipe.put(ingredient, Integer.parseInt(amount));
                }
            }
            index += 1;
        }

        TreeMap<String, String> shoppingList = createShoppingList();

        System.out.println("Available: " + available);
        System.out.println("Recipe: " + recipe);
        System.out.println("Shopping List: " + shoppingList);
    }

    public int kgToG(int kg) {
        return kg * 1000;
    }

    public TreeMap<String, String> createShoppingList() {
        TreeMap<String, String> shoppingList = new TreeMap<String, String>();
        for (String ingredient : recipe.keySet()) {
            if (available.containsKey(ingredient)) {

                int availableAmount = available.get(ingredient);
                int recipeAmount = recipe.get(ingredient);
                if (availableAmount >= recipeAmount) {
                    continue;
                } else {
                    int amountNeeded = recipeAmount - availableAmount;
                    shoppingList.put(ingredient, "g " + String.valueOf(amountNeeded));
                }
            } else {
                shoppingList.put(ingredient, "g " + String.valueOf(recipe.get(ingredient)));
            }
        }
        System.out.println("Shopping List: " + shoppingList);
        return shoppingList;
    }

}