0

I have a registration program where users can reserve tables at a restaurant.

Since I need to store potentially endless amount of customers, I cannot explicitly create new instances of my Customer class, because I need to be able to add them at runtime.

I thought about using a HashMap to insert every user through a registration form they fill up, however, rather than keeping the unique values of each Customer, it overwrites them with the latest entry in the hashmap.

To be clear, the key IS changing, it's successfully adding a new key, but the values inside the previous key are overwritten

Example

Class Customer has 3 setters: set Id, set fname, set lname.

These simply take the input from the registration form and assign it to each instance variable.

I create my only instance of the class with Customer cust = new Customer(). And HashMap hm = new HashMap()

User fills up the Form, selects submit. This submit button calls all the setters to assign the info.

I then use hm.put(cust.getId(), cust).

And I am able to correctly retrieve the values with Customer retrieveCust = (Customer) map.get(ID) String fname = retrieveCust.getName()

However, it's only the latest set of values. So If I have John doe and Jane doe, only Jane is retrieved for every key.

Is there a way to make it store every different "instance" of cust, with the values submitted by the Form?

1
  • Please share more code, SO users will have better chance to help you. Commented Apr 2, 2020 at 6:38

1 Answer 1

1

I'm assuming you have something like Map<String, Customer>. The key can only have one value. So, the map will always have the latest value that you assigned to the key.

If you want to have different values for the key, you should consider something likeMap<String, List<Customer>> or Map<String, Set<Customer>>.

Sign up to request clarification or add additional context in comments.

5 Comments

Yes, the key is the ID of my customer, which is set on registration, and I'm storing the whole object, so an example would be map.put(1, cust). However, every key is unique and it's correctly storing multiple objects. It's just that every object stored is a copy of each other, rather than an unique set of values. To be clear: I am in fact able to retrieve every attribute that cust had, but every attribute is that of the latest addition.
@Elmambo2194 There must be something wrong with the logic. Without seeing the code, it's difficult to see what's wrong.
@Elmambo2194 Sounds like you keep reusing the same Customer object rather than creating a new one each time, but until you post the relevant code in your question it's anybody's guess.
Well yes, that is exactly the point. How can I be creating a new customer if customers need to be able to register while the program is running? As far as I know, there is no way to dynamically name new variables like customer_i where I is an int that changes... So how do I handle this? I switched to arraylist and the same issue is happening. It's overwriting the old customers
@Elmambo2194 With, err, new Customer(...)? I don't understand the rest of this, but the next thing you should do is post the code in your question, and then continue the conversation there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.