'How to put Arraylist<object> as parameter in HashMap in Java? [closed]

I am doing a Java project - Budget manager, which shows how I spend my money. For that purpose, I used Hashmap to store the type of purchase with parameters of String(enum), and Arraylist<Product>. Product is a new class where name and price are to be passed.

How can I put multiple values from ArrayList to the same key in HashMap?

switch (choice) {
    case 1:
        mapProducts.put(Product.Type.FOOD.name(),//add array);
        break;
    case 2:
        mapProducts.put(Product.Type.CLOTHES.name(), );
        break;
    case 3:
        mapProducts.put(Product.Type.ENTERTAINMENT.name(), );
        break;
    case 4:
        mapProducts.put(Product.Type.OTHER.name(), );
        break;
}

import java.util.*;

public class Account {
    private ArrayList<String> listOfPurchases;
    private ArrayList<Double> itemPrice;
    private ArrayList<Product> products;
    private double balance;
    private Scanner input = new Scanner(System.in);
    private Map<String, ArrayList<Product>> mapProducts = new HashMap<>();

    public Account() {
        this.listOfPurchases = new ArrayList<>();
        this.itemPrice = new ArrayList<>();
        this.balance = 0.00d;
    }

    public void mainMenuPrint() {
        System.out.println(
                "Choose your action:\n" +
                        "1) Add income\n" +
                        "2) Add purchase\n" +
                        "3) Show list of purchases\n" +
                        "4) Balance\n" +
                        "0) Exit");
    }

    public void startMenu() {
        Scanner input = new Scanner(System.in);
        boolean isOn = true;
        while (isOn) {
            mainMenuPrint();
            int choice = input.nextInt();
            switch (choice) {
                case 0:
                    System.out.println("Bye!");
                    isOn = false;
                    break;
                case 1:
                    addIncome();
                    break;
                case 2:
                    addPurchase();
                    break;
                case 3:
                    showPurchases();
                    break;
                case 4:
                    showBalance();
                    break;
            }
        }
    }

    public void addIncome() {
        double addMoney = 0.00d;
        System.out.println("Enter income:");
        addMoney = input.nextDouble();
        setBalance(addMoney);
        System.out.println("Income was added!");
        System.out.println("");
    }

    public void addPurchase() {
        System.out.println(
                "Choose the type of purchase\n" +
                        "1) Food\n" +
                        "2) Clothes\n" +
                        "3) Entertainment\n" +
                        "4) Other\n" +
                        "5) Back\n");

        int choice = input.nextInt();
        double price = 0.0d;
        if (choice == 5) {
            startMenu();
        } else {
            System.out.println("Enter purchase name:");
            String item = input.next();
            setListOfPurchases(item);
            System.out.println("Enter its price:");
            try {
                price = input.nextDouble();
                setItemPrice(price);
                balanceSubtractFromPurchasePrice(price);
            } catch (
                InputMismatchException e) {
                System.out.println("Wrong input");
            }
            switch (choice) {
                case 1:
                    mapProducts.put(Product.Type.FOOD.name(),//add array);
                    break;
                case 2:
                    mapProducts.put(Product.Type.CLOTHES.name(), );
                    break;
                case 3:
                    mapProducts.put(Product.Type.ENTERTAINMENT.name(), );
                    break;
                case 4:
                    mapProducts.put(Product.Type.OTHER.name(), );
                    break;
            }
            System.out.println("");
        }
    }

    public void showPurchases() {
        double sum = 0.0d;
        if (mapProducts.size() == 0) {
            System.out.println("The purchase list is empty");
        } else {
            System.out.println(
                    "Choose the type of purchase\n" +
                            "1) Food\n" +
                            "2) Clothes\n" +
                            "3) Entertainment\n" +
                            "4) Other\n" +
                            "5) All\n" +
                            "6) Back\n");

            int choice = input.nextInt();
            for (Map.Entry<String, ArrayList<Product>> entry : mapProducts.entrySet()) {
                System.out.println(entry.getKey());
                System.out.println(mapProducts.get("FOOD").get(0));
            }
            for (int i = 0; i < listOfPurchases.size(); i++) {
                sum += itemPrice.get(i);
            }
            System.out.println("Total sum: $" + sum);
        }
        System.out.println("");
    }

    public void showBalance() {
        System.out.printf("Balance: $%.2f", getBalance());
        System.out.println("");
        System.out.println("");
    }

    public void setBalance(double balanceAmount) {
        this.balance += balanceAmount;
    }

    public void balanceSubtractFromPurchasePrice(double purchasePrice) {
        this.balance -= purchasePrice;
    }

    public double getBalance() {
        if (balance < 0) {
            System.out.println("Balance is negative ");
        }
        return this.balance;
    }

    public void setListOfPurchases(String item) {
        this.listOfPurchases.add(item);
    }

    public ArrayList<String> getListOfPurchases() {
        return listOfPurchases;
    }

    public ArrayList<Double> getItemPrice() {
        return itemPrice;
    }

    public void setItemPrice(double itemPrice) {
        this.itemPrice.add(itemPrice);
    }
}

public class Product {
    enum Type {
        FOOD, CLOTHES, ENTERTAINMENT, OTHER;
    }

    private String name;
    private double price;
    private Scanner input = new Scanner(System.in);

    public Product(String name, double price) {
        this.name = name;
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public double getPrice() {
        return price;
    }
}

Thanks for the help!



Solution 1:[1]

You could do it like this:

ArrayList<Product> products = new ArrayList<>();
// add products to list
mapProducts.put(/*String*/, products);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 WeinSim