简体   繁体   中英

Sorting with abstract class

I had some difficulties in sorting in decreasing order the elements of the following abstract class and its extensions.

package BankServices;

public abstract class Operation {

       public Operation (int date, double value){
       }
       public abstract double getValue();
       public abstract int getDate();
       public abstract String toString();
}

package BankServices;

public class Deposit extends Operation {
private int date;
private int value;

public Deposit(int date, double value) {
    super(date, value);
    // TODO Auto-generated constructor stub
}

@Override
public String toString() {
    // TODO Auto-generated method stub
    return date + "," + value + "+";
}

@Override
public double getValue() {
    // TODO Auto-generated method stub
    return value;
}

@Override
public int getDate() {
    // TODO Auto-generated method stub
    return date;
}
}
package BankServices;

public class Withdrawal extends Operation{
private int date;
private double value;

public Withdrawal(int date, double value) {
    super(date, value);
    // TODO Auto-generated constructor stub
}

@Override
public String toString() {
    // TODO Auto-generated method stub
    return date + "," + value + "-";
}

@Override
public double getValue() {
    // TODO Auto-generated method stub
    return value;
}

@Override
public int getDate() {
    // TODO Auto-generated method stub
    return date;
}   
}

I had to implement these methods of the main class returning sorted lists in descending order:

public List<Operation> getMovements() {
    Collections.sort(operations, new Comparator<Operation>(){
        public int compare(Operation a, Operation b){
            return (int) (b.getDate() - a.getDate());
        }
    });
    return operations;
}

public List<Deposit> getDeposits() {
    Collections.sort(deposits, new Comparator<Operation>(){
        public int compare(Operation a, Operation b){
            return (int) (b.getValue() - a.getValue());
        }
    });
    return deposits;

}

public List<Withdrawal> getWithdrawals() {
    Collections.sort(withdrawals, new Comparator<Operation>(){
        public int compare (Operation a, Operation b){
            return (int) (b.getValue() - a.getValue());
        }
    });
    return withdrawals;
}

the first one returns a List ordered by date, while getDeposits() and getWithdrawals() return List and List ordered by value.. Could you please suggest how to make it work without mistakes and failures? Thank you very much in advance.

Instead of this - use compareTo(...) method which is both on Double and Date , so for example:

Collections.sort(withdrawals, new Comparator<Operation>(){
    public int compare (Operation a, Operation b){
        return Double.valueOf(b.getValue()).compareTo(Double.valueOf(a.getValue()));
    }
});

Still, your code should work, except for some weird data you can have there... But not for real life data I think EDIT: I was wrong, your code would only work if difference between doubles would be over 1.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM