I am trying to sort Bank accounts where it first sorts by Name alphabetically, and second by the amount in the account from most to least. Unfortunately the CompareTo method doesn't seem to be working correctly, the only part that works is the second part where it sorts by the amount.
BankAccount class
/**
* A bank account has a balance, the name of the account holder,
* and an account number. The balance can be changed by deposits
* and withdrawals.
*/
public class BankAccount implements Comparable<BankAccount> {
/**
* Constructs a bank account with a zero balance.
* @param name the name of the account holder
*/
public BankAccount(String name) {
this.name = name;
balance = 0;
accountNo = ++lastAccountNo;
}
/**
* Constructs a bank account with a given balance.
* @param initialBalance the initial balance
* @param name the name of the account holder
*/
public BankAccount(String name, double initialBalance) {
this.name = name;
balance = initialBalance;
accountNo = ++lastAccountNo;
}
/**
* Deposits money into the bank account.
* @param amount the amount to deposit
*/
public void deposit(double amount) {
double newBalance = balance + amount;
balance = newBalance;
}
/**
* Withdraws money from the bank account.
* @param amount the amount to withdraw
*/
public void withdraw(double amount) {
double newBalance = balance - amount;
balance = newBalance;
}
/**
* Gets the current balance of the bank account.
* @return the current balance
*/
public double getBalance() {
return balance;
}
/**
* Gets the name of the account holder.
* @returns the name of the account holder
*/
public String getName() {
return name;
}
/**
* Gets the account number of the account.
* @returns the account number of the account
*/
public int getAccountNo() {
return accountNo;
}
/**
* Returns a String representation of the BankAccount. The format
* is "name: accountNo balance"
* @returns a String representation of the BankAccount.
*/
public String toString() {
return name + ": AccountNo:" + accountNo + " balance:" + balance;
}
private double balance;
private String name;
private int accountNo;
private static int lastAccountNo=0;
public int compareTo(BankAccount b) {
if(this.name.compareTo(b.name) == 0 && this.balance > b.balance) return 0;
else if(this.name.compareTo(b.name) < 0 && this.balance < b.balance) return 1;
else if(this.name.compareTo(b.name) > 0 && this.balance == b.balance) return -1;
//else if(this.name.compareTo(b.name) == 0) return 0;
//else if(this.name.compareTo(b.name) < 0) return 1;
//else if(this.name.compareTo(b.name) > 0) return -1;
else if(this.balance == b.balance) return 0;
else if(this.balance < b.balance) return 1;
else if(this.balance > b.balance) return -1;
else return 0;
}
}
BankAccountTester
import java.util.ArrayList;
import java.util.Collections;
class BankAccountTester {
public static void main(String args[]) {
ArrayList<BankAccount> accounts = new ArrayList<BankAccount>();
accounts.add(new BankAccount("Louis Oliphant", 100.0));
accounts.add(new BankAccount("Louis Oliphant", 100.10));
accounts.add(new BankAccount("Louis Oliphant", 100.0));
accounts.add(new BankAccount("Jane Doe", 100.0));
accounts.add(new BankAccount("Jane Doe", 99.0));
accounts.add(new BankAccount("Jane Doe", 100.0));
System.out.println("*****Unsorted******");
for (BankAccount b : accounts) {
System.out.println(b);
}
System.out.println();
Collections.sort(accounts);
System.out.println("******Sorted******");
for (BankAccount b : accounts) {
System.out.println(b);
}
}
}