In this code my assignment is to implement Comparable. I know I need to put implements Comparable but then I get an error from compiler saying that "Account is not abstract and does not override abstract method compareTo(Account) in Comparable".
I know that I need to create the compareTo() method but I don't know how.
My exact assignment is to rewrite the class Account so that it implements the interface Comparable. The account that has the lowest balance is the smallest. Create a main and test the class by creating 3 Account objects. Add them in an ArrayList. Sort it with one of the methods from Collctions.sort().
Is the list being sorted the way you thought it would?
Below is my code.
Thanks in advance!
//*******************************************************
// Account.java .
// A bank account class with methods to deposit to, withdraw from,
// change the name on, charge a fee to, and print a summary of the
// account.
//*******************************************************
import java.util.*;
public class Account implements Comparable<Account>{
private double balance;
private String acctNum;
//----------------------------------------------
//Constructor -- initializes balance, owner, and account number
//----------------------------------------------
public Account(String number, double initBal){
balance = initBal;
acctNum = number;
}
//----------------------------------------------
// Checks to see if balance is sufficient for withdrawal.
// If so, decrements balance by amount; if not, prints message.
//----------------------------------------------
public String withdraw(double amount){
String info="Insufficient funds";
if (balance >= amount){
balance=balance- amount;
info= "Succeeded, the new balance is : "+ balance ;
}
return info;
}
//----------------------------------------------
// Adds deposit amount to balance.
//----------------------------------------------
public String deposit(double amount){
String info="" ;
if( amount<0){
info = " Wrong amount";
}
else{
balance=balance+ amount;
info=" Succeeded, the new balance is: " + balance;
}
return info;
}
//----------------------------------------------
// Returns balance.
//----------------------------------------------
public double getBalance(){
return balance;
}
//----------------------------------------------
// Returns a string containing the name, account number, and balance.
//----------------------------------------------
public String toString(){
return " Numer: "+ acctNum+ " Balance: " + balance;
}
//----------------------------------------------
// Returns accoutn number.
//----------------------------------------------
public String getAcctNum(){
return acctNum;
}
public boolean equals(Object other){
// this is what you should do
return true;
}
public int compareTo(Account a){
return;
}
public static void main(String[] args){
}
}