0

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){


   }
}
Viktor V
  • 11
  • 1
  • 1
    Hint: there is a method in the `Double` class for comparing doubles that might help you. – Andy Turner Apr 04 '18 at 20:39
  • This won't even compile. You didn't implement the compareTo method. – duffymo Apr 04 '18 at 20:45
  • @duffymo exactly, I don't know how to create it. That is what I needed help with – Viktor V Apr 04 '18 at 20:49
  • Here's your first lesson as a student: get over that irritation about things not going your way. The computer and language aren't persecuting you. You made a mistake. You have to take a breath, interpret the information you're getting back, and using it to figure out what YOU did wrong. This is an easy problem with lots of tutorials. You should learn how to find them. – duffymo Apr 04 '18 at 20:49
  • "Help"? Sounds like "do it for me". How will you learn that way? You don't sound like you desperately want to learn coding. You want to get grades and move along. Look at this: https://examples.javacodegeeks.com/java-basics/java-comparable-example/ – duffymo Apr 04 '18 at 20:50
  • @duffymo Thank you, im just looking for tips. I have tried searching all over the web but I still don't understand it. That's why I came here. I know I made a mistake and im trying to understand why. I actually think coding is really fun and I actually want to learn it. I just wrote my question in a way that it looks like I just want the answer. My bad. But I will continue my search and hopefully maybe your example can make me understand. Edit: Your example actually helped me understand! Thank you very much! – Viktor V Apr 04 '18 at 20:57

0 Answers0