aspose file tools*
The moose likes Beginning Java and the fly likes Logic Help Please: Sets and Iteration Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Logic Help Please: Sets and Iteration" Watch "Logic Help Please: Sets and Iteration" New topic
Author

Logic Help Please: Sets and Iteration

Jason Koonce
Ranch Hand

Joined: Oct 30, 2010
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.




Jason K. (AC)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40061
    
  28
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

Joined: Apr 22, 2008
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

Joined: Oct 13, 2005
Posts: 40061
    
  28
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

Joined: Oct 30, 2010
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

Joined: Oct 02, 2003
Posts: 11499
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Jason Koonce
Ranch Hand

Joined: Oct 30, 2010
Posts: 36
Thanks everyone! Map's are wonderful things!
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3860

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.


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40061
    
  28
I was hoping you would find a Map. What are you using as the key and what as the value?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Logic Help Please: Sets and Iteration