0

I've been playing around with interfaces lately and was able to successfully implement Comparable and sort an ArrayList of BankAccount objects by their amount.

But what if the ArrayList contained objects of type BankAccount and Country having instance variable "area" (double). They both have value of type double which can be compared and sorted.

So my first question is- How to sort ArrayList containing various objects having a double as instance variable?

Here is my code:

/**
   A bank account has a balance that can be changed by 
   deposits and withdrawals.
*/
public class BankAccount implements Measurable, Comparable<BankAccount>
{  
  private Double balance;

   /**
      Constructs a bank account with a zero balance.
   */
   public BankAccount()
   {   
      balance = 0.0;
   }

   /**
      Constructs a bank account with a given balance.
      @param initialBalance the initial balance
   */
   public BankAccount(Double initialBalance)
   {   
      balance = initialBalance;
   }

   /**
      Deposits money into the bank account.
      @param amount the amount to deposit
   */
   public void deposit(Double amount)
   {  
      balance = balance + amount;
   }

   /**
      Withdraws money from the bank account.
      @param amount the amount to withdraw
   */
   public void withdraw(Double amount)
   {   
      balance = balance - amount;
   }

   /**
      Gets the current balance of the bank account.
      @return the current balance
   */
   public double getBalance()
   {   
      return balance;
   }

   public double getMeasure()
   {
      return balance;
   }
     public int compareTo(BankAccount o)
  {
    return this.balance.compareTo(o.getBalance());


  }
}

/**
   A country with a name and area.
*/
public class Country implements Measurable
{
   private String name;
   private double area;

   /**
      Constructs a country.
      @param aName the name of the country
      @param anArea the area of the country
   */
   public Country(String aName, double anArea) 
   { 
      name = aName;
      area = anArea; 
   }

   /**
      Gets the country name.
      @return the name
   */
   public String getName() 
   {
      return name;
   }

   /**
      Gets the area of the country.
      @return the area
   */
   public double getArea() 
   {
      return area;
   }

   public double getMeasure() 
   {
      return area;


      }
    }


    public interface Measurable
    {
       double getMeasure();  // An abstract method
    }

import java.util.*;
public class MeasurableTester 
{
   public static void main(String[] args)
   {
      // Calling the average method with an array of BankAccount objects
    BankAccount b=new BankAccount(10.0);
    BankAccount c = new BankAccount(2000.0);
     List<BankAccount> accounts = new ArrayList<BankAccount>();

      accounts.add(b);
      accounts.add(c);


     BankAccount x= Collections.max(accounts);
      System.out.println(x.getBalance());
   }


}
7
  • ArrayList contains objects of type BankAccound and Country? Does your BankAccount and Country extend similar class? Can you show the classes ? Commented Mar 3, 2017 at 6:31
  • I edited post to include my code Commented Mar 3, 2017 at 6:37
  • 1
    Can you clarify, do you want to compare objects that implement Measurable i.e. sorting an ArrayList<Measurable>? Commented Mar 3, 2017 at 6:39
  • That's how I tried to solve this problem. But I'm not sure if it would work. After a while I got confused with using Interface type of a class inside of interface...My goal is to somehow have objects in the ArrayList in order Commented Mar 3, 2017 at 6:41
  • The answer is probably a custom Comparator<T> Commented Mar 3, 2017 at 6:41

1 Answer 1

3

You should use List of Measurables and use comparator:

  List<Measurable> measurableList = new ArrayList<>();
  measurableList.add(new BankAccount(10.0));
  measurableList.add(new Country("name", 44.5));

  measurableList = Collections.max(measurableList, Comparator.comparingDouble(Measurable::getMeasure));
Sign up to request clarification or add additional context in comments.

1 Comment

It makes so much more sense this way

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.