In: Computer Science
Add code to the Account class and create a new class called BalanceComparator.
import java.util.*;
public final class Account implements Comparable {
private String firstName;
private String lastName;
private int accountNumber;
private double balance;
private boolean isNewAccount;
public Account(
String firstName,
String lastName,
int accountNumber,
double balance,
boolean isNewAccount
) {
this.firstName = firstName;
this.lastName = lastName;
this.accountNumber = accountNumber;
this.balance = balance;
this.isNewAccount = isNewAccount;
}
/**
* TO DO: override equals
*/
@Override
public boolean equals(Object other) {
return false;
}
/**
* TO DO: override hashCode here
*/
@Override
public int hashCode() {
return 0;
}
/**
* TO DO:
* Write compareTo (natural ordering of class):
* 1. accountNumber in ascending order
* If same, break ties:
* 2. old Accounts before new accounts
* If same, break ties
* 3. LastName
* If same, break ties
* 4. FirstName
* If same, break ties
* 5. Lower Balance before higher balance
*/
@Override
public int compareTo(Account other) {
final int BEFORE = -1;
final int EQUAL = 0;
final int AFTER = 1;
return EQUAL;
}
}
===========================
import java.util.Comparator;
/*
* TO DO:
*This class should compare all accounts by balance only (in ascending order)
*/
The updated code for Account.java class is as below:
import java.util.*;
public final class Account implements Comparable<Account> {
        
    private String firstName;
    private String lastName;
    private int accountNumber;
    private double balance;
    private boolean isNewAccount;
    public Account(String firstName,String lastName, int accountNumber,double balance,boolean isNewAccount) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.accountNumber = accountNumber;
        this.balance = balance;
        this.isNewAccount = isNewAccount;
    }
    
    public String getFirstName() {
                return firstName;
        }
        public String getLastName() {
                return lastName;
        }
        public int getAccountNumber() {
                return accountNumber;
        }
        public double getBalance() {
                return balance;
        }
        public boolean isNewAccount() {
                return isNewAccount;
        }
        public void setFirstName(String firstName) {
                this.firstName = firstName;
        }
        public void setLastName(String lastName) {
                this.lastName = lastName;
        }
        public void setAccountNumber(int accountNumber) {
                this.accountNumber = accountNumber;
        }
        public void setBalance(double balance) {
                this.balance = balance;
        }
        public void setNewAccount(boolean isNewAccount) {
                this.isNewAccount = isNewAccount;
        }
        
        /**
     * TO DO: override equals
     */
    @Override
    public boolean equals(Object other) {
        return false;
    }
    /**
     * TO DO: override hashCode here
     */
    @Override
    public int hashCode() {
        return 0;
    }
    /**
     * TO DO:
     * Write compareTo (natural ordering of class):
     * 1. accountNumber in ascending order
     *      If same, break ties:
     * 2. old Accounts before new accounts
     *      If same, break ties
     * 3. LastName
     *      If same, break ties
     * 4. FirstName
     *      If same, break ties
     * 5. Lower Balance before higher balance
     */
        @Override
        public int compareTo(Account other) {            
                 return Comparator.comparingInt(Account::getAccountNumber)
                         .thenComparing(Account::isNewAccount)
                         .thenComparing(Account::getLastName)
                         .thenComparing(Account::getFirstName)
                         .thenComparingDouble(Account::getBalance)
                         .compare(this, other);          
        }
        
        
        public String toString() {
                String str;
                if(isNewAccount)
                        str = "is new account";
                else
                        str = "is not a new account";
                return accountNumber+" "+str+" with name "+firstName+" "+lastName+" with a balance of "+ balance;
        }
}
The class used to compare any 2 Account class objects as below:
import java.util.Comparator;
public class AccountComparing  implements Comparator<Account>{
        @Override
        public int compare(Account ac1, Account ac2) {
                return ac1.compareTo(ac2);
        }
}
I have tested the above functionality using another class as below:
import java.util.Arrays;
public class Testing {
        public static void main(String[] args){
                Account ac[] = new Account[4];
                ac[0] = new Account("John", "valles", 10, 70000, true);
                ac[1] = new Account("Angel", "Lopez", 11, 8000, false);
                ac[2] = new Account("Angel", "Arun", 11, 900, true);    
                ac[3] = new Account("Angel", "Arun", 11, 700, true);
                Arrays.sort(ac);
                
                for(Account a : ac){
                        System.out.println(a.toString());
                }
                
        }
}
The ouput screenshot of the above program is :

If you have any queries reagrding this answer, please reach out through the comment section.