I have 4 classes.
One holds information about a customer. The other one about orders.
2 more classes play the registry role, one is customer registry, the other is orders registry.
Orders registry has a hashmap which looks like this:
private HashMap<Integer, Order> orderRegistryMap = new HashMap<>();
The same goes for customers registry.
private HashMap<Integer, Customer> customersRegistryMap = new HashMap<>();
Class orders has an int orderid. Class customers has an int customerid.
I added demo data through both registries (lets say 1 customer with customerid 100, and one order with orderid 500.
I wrote simple methods to search for orders by orderid or to get a hashmap of all orders.
I also wrote simple methods to search for customers by customerid or to get a hashmap of all customers.
I need help with writing a method which can find a customer by orderid and get all orders which are associated with that customerid.
Any ideas?
The typical way to handle such relations(and this seems like a one to many realtion) is to store a customer id in the order itself. This is done because each order has one and only one customer. if you don’t do that you have no choice but iterate through the array.
EDIT: for the reverse relation Customer->Orders I would use a reverse registry something like:
And keep that in sync with the orders and customers. This will perform as fast as you can get and I don’t think you can use less memory to achieve what you want.