Win a copy of Functional Reactive Programming this week in the Other Languages forum!

# Logic Help Please: Sets and Iteration

Jason Koonce
Ranch Hand
Posts: 36
Hello everyone. I am stuck on a piece of logic and can't seem to get past it. To be clear, I am not asking for you to do this for me, but I would greatly appreciate it if you could point me in the right direction. First, here is the assignment:

Develop an application that will read and process customer history order
information from a file. Each record of the text file contains a customer ID,
order number, and total of order. An example book order file:

10051 1234567 150.00
10052 1234568 200.00
10051 1234569 120.00

The file can contain several entries for the same customer ID. Your
application should accumulate the total spent for each customer as you read
the order information from the file. The application should print total spent
for each customer.

Refer to Collections in the Sun Java API Documentation. Determine which
Collection implementation would be the best choice for your application.

My problem is trying to figure out the logic. I just can't seem to wrap my head around how to compare customer ID's as I Iterate through the Set or Array and add their orders together for the same customer, but retain their order ID's. Again, if you could point me in the right direction, I would be eternally grateful. I have been chewing on this for a while now.

Campbell Ritchie
Sheriff
Posts: 50258
79
Don't try any code at all until you have worked out what you want to do.

Get a pencil and paper, and a large eraser (far and away the most important piece of hardware ever )

Write down what you are starting from and what you want to finish with. The starting point is easy:
10051 1234567 150.00
10052 1234568 200.00
10051 1234569 120.00
Now what do you intend to finish with? Do you want 10051 and a record of all his orders put together? Write down what the data will look like when you put them all together. Erase it and start again, and repeat until you are happy with what you have got.

Look at the diagram now. Can you see any pairings or combinations which you can create objects to encapsulate? Can you see any way of creating a Customer object? Add more orders for 10051, and see what your diagram looks like now.

Go to the Java™ Tutorials, and read through all the well-known interfaces in the Collections Framework. Draw a diagram for what each interface in the framework looks like. See whether you can see any analogies to your first diagram.

Karanjeet Kareer
Greenhorn
Posts: 3
Hi Jason,

One hint , create a new POJO class CustomerOrder and include following attributes in it
private double customerID;
private double orderNumber;
private double orderCost;

Thanks

Campbell Ritchie
Sheriff
Posts: 50258
79
Not convinced that is the best way to do it, I am afraid.
It is better than three arrays in parallel, however. That is quite error-prone.

Jason Koonce
Ranch Hand
Posts: 36
Karanjeet Kareer wrote:Hi Jason,

One hint , create a new POJO class CustomerOrder and include following attributes in it
private double customerID;
private double orderNumber;
private double orderCost;

Thanks

Thank you for your suggestions! I appreciate it.

Sorry, but what is a POJO class?

Campbell Ritchie wrote:
Not convinced that is the best way to do it, I am afraid.
It is better than three arrays in parallel, however. That is quite error-prone.

This is exactly what I am trying to avoid. I know there is a better way...I have seen it through my studies, but I have a severe case of writers block. Either way, I plan on looking into each of your suggestions more in depth! Thanks a ton!

fred rosenberger
lowercase baba
Bartender
Posts: 12202
35
POJO is a "plain old Java object"...or something like that. I believe the idea is you don't need any fancy, uber-powerful third party class/library....you just write a simple class that has/does what you need.

Jason Koonce
Ranch Hand
Posts: 36
Thanks everyone! Map's are wonderful things!

Vijitha Kumara
Bartender
Posts: 3918
10
Map's are wonderful things!

Sure they are...

You may add some tips or code to the solution which might help someone else in the future.

Campbell Ritchie
Sheriff
Posts: 50258
79
I was hoping you would find a Map. What are you using as the key and what as the value?