Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Logic Help Please: Sets and Iteration

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.


 
Marshal
Posts: 76489
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 3
Hibernate Eclipse IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 76489
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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!
 
lowercase baba
Posts: 13075
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks everyone! Map's are wonderful things!
 
Bartender
Posts: 4109
72
Hibernate Fedora Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 76489
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was hoping you would find a Map. What are you using as the key and what as the value?
 
I'm not sure if I approve of this interruption. But this tiny ad checks out:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic