Question: Write a class AccountComparator to implement the Comparator interface for class Account in Figure 15.9 based on the accounts balance. Use this class in order
Write a class AccountComparator to implement the Comparator interface for class Account in Figure 15.9 based on the account’s balance. Use this class in order to sort a list of accounts in descending order based on the accounts’ balance.
Figure 15.9
I 2 4 5 6 7 8 9 10 II 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 // Fig. 15.9: Account.java // Account class for storing records as objects. public class Account { 4 45 45 private int account Number; private String firstName; private String lastName; private double balance; 46 } // initializes an Account with default values public Account() {this(0, "", "", 0.0);} // initializes an Account with provided values public Account (int account Number, String firstName, String lastName, double balance) { this.account Number = account Number; this.firstName = firstName; this.lastName = lastName; this.balance = balance; } // get account number public int getAccountNumber() {return account Number;} // set account number public void setAccount Number(int accountNumber) {this.account Number = account Number; } //get first name public String getFirstName() {return firstName; } // set first name public void setFirstName(String firstName) {this.firstName = firstName; } // get last name public String getLastName() {return lastName; } // get balance 42 public double getBalance() {return balance; } 43 44 45 // set last name public void setLastName (String lastName) {this.lastName = lastName; } // set balance public void setBalance (double balance) {this.balance = balance;}
Step by Step Solution
3.38 Rating (154 Votes )
There are 3 Steps involved in it
To implement a Comparator for the Account class to sort accounts based on their balances in descendi... View full answer
Get step-by-step solutions from verified subject matter experts
