Search code examples
javafor-loopconstructorhashmap

Is there a way to keep a reference to a hashmap which is an instance variable of an object?


So I have a constructor called Member.

It stores a Member ID and a Hashmap of all the receipts a member has made. The hashmap includes a String to store the date of purchase, and an ArrayList of strings to store the items purchased.

public class Member {

    // Member ID of customer
    private int memberID;
    // All of the receipts from this customer. Key is Date and Value is List of all items on that date.
    public HashMap<String, ArrayList<String>> receipts;

    public Member(int memberID) {
        this.memberID = memberID;
        this.receipts = new HashMap<>();
    }

In the following class, the receipts for a member are generated. A new Member object is instantiated by inputting a new memberID every time an iteration is completed.

        for (int memberID : memberIDSet) {
            // Creates a new member based on the ID.
            Member currentMember = new Member(memberID);
            System.out.println("===== Current Member: " + memberID + " =====");

            TreeSet<String> memberPurchaseDates = getMemberPurchaseDates(memberID);

            // Goes through the list of dates and searches specifically for the dates in which the memberID purchased an item.
            for (String date : memberPurchaseDates) {
                // Creates a receipt in the "Member.java" class for the currentMember.
                // HERE IS WHERE I AM CONFUSED. Is this possible?
                currentMember.receipts.put(date, fillReceipt(memberID, date));
            }

            //Prints out what I want perfectly, but does not store the hashmap for each new member object created.
            System.out.println(currentMember.receipts);
        }

Everytime the loop iterates, the reference to the previous hashmap is lost.

How do I go about storing this information? Do I need to create another constructor class to keep track of the receipts?


Solution

  • I think you may be confused about classes. Your code

    Member currentMember = new Member(memberID);
    

    Creates a NEW EMPTY Member for the specific member ID, which is why you say the hash map gets reset. It doesn't really get reset, the hash map of the previous member object contains the data.

    Your code actually looks correct, you just need to store your members. For example,

            List<Member> members = new ArrayList<>();
            for (int memberID : memberIDSet) {
                // Creates a new member based on the ID.
                Member currentMember = new Member(memberID);
                System.out.println("===== Current Member: " + memberID + " =====");
    
                TreeSet<String> memberPurchaseDates = getMemberPurchaseDates(memberID);
    
                // Goes through the list of dates and searches specifically for the dates in which the memberID purchased an item.
                for (String date : memberPurchaseDates) {
                    // Creates a receipt in the "Member.java" class for the currentMember.
                    // HERE IS WHERE I AM CONFUSED. Is this possible?
                    currentMember.receipts.put(date, fillReceipt(memberID, date));
                }
    
                //Prints out what I want perfectly, but does not store the hashmap for each new member object created.
                System.out.println(currentMember.receipts);
                members.add(currentMember);
            }
            for (Member member : members) {
                System.out.println(member.receipts);
            }
    

    Unless you want to put everything into 1 HashMap, in which case you need to extract it out of Member.

    Or does memberIDSet have repeating members? In which case it wouldn't be a set, but I guess you would have to keep a map of id to member so that if the member already exists you don't create a new one.