• Post Reply Bookmark Topic Watch Topic
  • New Topic

Homework: Multiple comparators  RSS feed

 
Cody Peterson
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greetings all.

I have a homework assignment that requires us to generate a bunch of bank accounts, and sort them using Comparators with different requirements. For example, for the first part, we simply sort the account by name (which I have successfully done). Then, we need to sort by name as well as balance and I am not sure how to sort on multiple parameters using comparators. Any and all help is greatly appreciated, even if it's a link to a tutorial on using multiple comparators Main.java code is given below with sample output (first)

Thank you!


CA:Acct#878 Name: Nash Zip:60651 Bal:3454.156761338234
CA:Acct#124 Name: Bob Zip:60623 Bal:9330.436335427394
SA:Acct#377 Name: Bob Zip:60623 Bal:5176.021326329733
CA:Acct#330 Name: Steve Zip:60609 Bal:4475.829967882866
SA:Acct#292 Name: Richard Zip:60654 Bal:1787.1918223355153
CA:Acct#493 Name: Nash Zip:60608 Bal:578.3817545856074
CA:Acct#801 Name: Jacob Zip:60610 Bal:8675.36939089393
CA:Acct#130 Name: Crosby Zip:60611 Bal:3519.3535386995
CA:Acct#260 Name: Michael Zip:60618 Bal:554.1668215122276
SA:Acct#171 Name: Jacob Zip:60623 Bal:5003.089610170856
CA:Acct#851 Name: Jacob Zip:60651 Bal:3436.8046938701023
SA:Acct#142 Name: Richard Zip:60610 Bal:2631.9119945488587
SA:Acct#898 Name: Crosby Zip:60654 Bal:1821.7246675470344
SA:Acct#235 Name: Nash Zip:60610 Bal:2734.9665579967473
CA:Acct#569 Name: Jacob Zip:60644 Bal:5432.621182238243
CA:Acct#372 Name: Crosby Zip:60654 Bal:9827.615101169255
CA:Acct#346 Name: Crosby Zip:60654 Bal:5250.633314985043
SA:Acct#414 Name: Richard Zip:60611 Bal:5307.25021688474
SA:Acct#631 Name: Stills Zip:60611 Bal:5603.150073810688
SA:Acct#558 Name: Larry Zip:60611 Bal:5876.585306422419

Blah blah debug for sorting by name
CA:Acct#124 Name: Bob Zip:60623 Bal:9330.436335427394
SA:Acct#377 Name: Bob Zip:60623 Bal:5176.021326329733
CA:Acct#130 Name: Crosby Zip:60611 Bal:3519.3535386995
SA:Acct#898 Name: Crosby Zip:60654 Bal:1821.7246675470344
CA:Acct#372 Name: Crosby Zip:60654 Bal:9827.615101169255
CA:Acct#346 Name: Crosby Zip:60654 Bal:5250.633314985043
CA:Acct#801 Name: Jacob Zip:60610 Bal:8675.36939089393
SA:Acct#171 Name: Jacob Zip:60623 Bal:5003.089610170856
CA:Acct#851 Name: Jacob Zip:60651 Bal:3436.8046938701023
CA:Acct#569 Name: Jacob Zip:60644 Bal:5432.621182238243
SA:Acct#558 Name: Larry Zip:60611 Bal:5876.585306422419
CA:Acct#260 Name: Michael Zip:60618 Bal:554.1668215122276
CA:Acct#878 Name: Nash Zip:60651 Bal:3454.156761338234
CA:Acct#493 Name: Nash Zip:60608 Bal:578.3817545856074
SA:Acct#235 Name: Nash Zip:60610 Bal:2734.9665579967473
SA:Acct#292 Name: Richard Zip:60654 Bal:1787.1918223355153
SA:Acct#142 Name: Richard Zip:60610 Bal:2631.9119945488587
SA:Acct#414 Name: Richard Zip:60611 Bal:5307.25021688474
CA:Acct#330 Name: Steve Zip:60609 Bal:4475.829967882866
SA:Acct#631 Name: Stills Zip:60611 Bal:5603.150073810688




 
Rajat Jindal
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to sort on the balance then you can create one more comparator which can sort for you on the basis of balance in the same way as you did for names.
But in case you want to sort on multiple parameters at the same time, I guess what you can do is , first compare names and if both names are equal compare the balance in the same comparator
 
Campbell Ritchie
Marshal
Posts: 56530
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why have you chosen to use longs for your zip codes? What if you go east where zip codes begin with 0? What will happen when you have a zip code beginning 08 or 09? Use Strings for your zips, then you can include leading 0s.
Your balance column shows another disadvantage to using doubles for money. you should use BigDecimals, either by means of random Strings, or, if you must use doubles to create a BigDecimal, work out how to round it to the nearest ¢.
Strings, as used for the zips, and BigDecimal for the amounts both already implement the Comparable<T> interface, so you have a compareTo() method ready-made for use in your Comparator.
You have some poor design putting all your logic in to the main method. In my opinion, there is an ideal length for a main method: one statement. That creates a Bank object and starts it going. The other methods should be in the appropriate classes. You should avoid the keyword static unless you really want something to be static. Writing static because it won’t compile otherwise suggests you don’t want it to be static at all.

I have broken some lines and removed some blanks from your post because it is difficult to read long lines, especially when they are all double-spaced.
 
Campbell Ritchie
Marshal
Posts: 56530
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way, I think there is a risk of collisions (two people with the same account number) if you use that method of allocating account numbers. There is a related discussion here.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:By the way, I think there is a risk of collisions (two people with the same account number) if you use that method of allocating account numbers.


Yes, there absolutely is. And if I did my math right, the Birthday Paradox says the probability of a collision on any given run is about 19%.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!