• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Sorting

 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Everyone.

I'm stuck with my assignment, pretty new to Java world.
I have 2 classes - Customer and Purchase. Purchase class has a field long CustomerId.
I have to print out a report from Purchase class by Customer's last name.

Any idea how to tie these two together?

Any hints will be greatly appreciated.
Codes are below.








Now I have to sort this purchase report by customer's last name.
Have no idea how to do it!

Thank you in advance!
 
Saloon Keeper
Posts: 15484
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, instead of having a customer ID in your purchase, you could have a reference to the customer object. Perform this translation when you retrieve the purchase from the database.

You can then easily sort your list like this:

The comparing() method is a static import from Comparator.
 
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't see anywhere where you are creating a new Purchase or a new Customer. Presumably you'd be keeping some sort of Collection of these.
 
Kristina Hawkins
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:I don't see anywhere where you are creating a new Purchase or a new Customer. Presumably you'd be keeping some sort of Collection of these.


Yes, there is a separate class that reads data from .txt file and put it into an ArrayList.
 
Stephan van Hulst
Saloon Keeper
Posts: 15484
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, first read all customers and put them in a Map keyed by customer ID. Then read all purchases, but instead of storing a customer ID in your Purchase class, store an actual Customer, which you look up from your customer map.
 
Kristina Hawkins
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:So, first read all customers and put them in a Map keyed by customer ID. Then read all purchases, but instead of storing a customer ID in your Purchase class, store an actual Customer, which you look up from your customer map.



I can't put an actual Customer in the Purchase class.
Purchase class has to have a long CustomerId.
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But you can create a class PurCust with fields Purchase and Customer. First make a map <Long, Customer> with key CustomerID, and for every Purchase create a new PurCust instance, by looking up the CustomerID of the Purchase in the map.
Then do as Stephan described.
 
Stephan van Hulst
Saloon Keeper
Posts: 15484
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kristina Hawkins wrote:Purchase class has to have a long CustomerId.


Why?
 
Kristina Hawkins
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:

Kristina Hawkins wrote:Purchase class has to have a long CustomerId.


Why?



that is what it says in the specs for the assignment
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kristina Hawkins wrote:

Stephan van Hulst wrote:

Kristina Hawkins wrote:Purchase class has to have a long CustomerId.


Why?



that is what it says in the specs for the assignment



Something which the assignment didn't mean to teach you: in real life too, you will sometimes be given specs which could easily be improved on.

Anyway, given this requirement you're going to need to build something which can be given a customer ID and return the matching Customer object.
 
Kristina Hawkins
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:

Kristina Hawkins wrote:

Stephan van Hulst wrote:

Kristina Hawkins wrote:Purchase class has to have a long CustomerId.


Why?



that is what it says in the specs for the assignment



Something which the assignment didn't mean to teach you: in real life too, you will sometimes be given specs which could easily be improved on.

Anyway, given this requirement you're going to need to build something which can be given a customer ID and return the matching Customer object.



ok, but then when I read data from .csv file using CSVIterator, everything is returned as String, but my variable is Customer object. Can I cast? Or is there other way ?
 
Piet Souris
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look  at the Long class to see how to convert a String to a Long  (or long), and likewise for other types.
 
Kristina Hawkins
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:Look  at the Long class to see how to convert a String to a Long  (or long), and likewise for other types.



I know how to do this, but my type is a user defined.
 
Piet Souris
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look at the way you create a Customer, then you know what conversions to make.
 
Kristina Hawkins
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:Look at the way you create a Customer, then you know what conversions to make.



I'm not sure what you mean
 
Piet Souris
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well,

you're using the Builder pattern to create a new Customer. Every parameter for this building is already a String, except for cusomer_id. So, if you read in the values of the CSV file, you should only need to convert the customer_id into a long, and then you create a Customer via the Builder.
 
Kristina Hawkins
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:Well,

you're using the Builder pattern to create a new Customer. Every parameter for this building is already a String, except for cusomer_id. So, if you read in the values of the CSV file, you should only need to convert the customer_id into a long, and then you create a Customer via the Builder.



I'm looking at the Purchase class (posted at the top of the post), there are two fields - customer_id & book_id. Somehow I have t pull out a customer object and a book obj from them.
If I declare these fields Customer & Book, then when I read data from . csv file I can't convert them from String to Book obj or Customer obj.
 
Piet Souris
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you also have a CSV containing Purchase data, including customer_id? Then you do exactly the same. Look at what the Purchase Builder expects, and convert the id's from the CSV file to the correct id types, just as with the Customer class. Then, having constructed all Purchase and Customer objects, see the various replies how to make a report.
 
reply
    Bookmark Topic Watch Topic
  • New Topic