0

I have a class like this:

    static class Transactions{
    private int type;
    private String to;
    private String from;
    private double amount;
    public Transactions (int type, String to, String from, double amount) {
        if(type>=1 || type<=3) {
            this.type=type;
            this.to=to;
            this.from=from;
            this.amount=amount;
        }
        else
            throw new InvalidParamaterException(type);
    }
//This is for deposits and withdrawal
    public Transactions (int type, String para, double amount) {
        
    }
    public int getType() {
        return type;
    }
    public String getTo() {
        return to;
    }
    public String getFrom() {
        return from;
    }
    public double getAmount() {
        return amount;
    }
    
        
    }

static class Bank {
private String Name;
private ArrayList<Customer> customers = new ArrayList<>();
private ArrayList<Company> companies = new ArrayList<>();
private ArrayList<Account> accounts = new ArrayList<>();
private String Address;
public Bank(String Name, String Address) {
    this.Name=Name;
    this.Address=Address;
}
public void processTransactions(Collections ts) {
    Comparator<Transactions> byTypeAndTo = 
            Comparator.comparing(Transactions::getType)
                .thenComparing(Transactions::getTo);
    ts.sort(byTypeAndTo);
}

What I want to do is create a Transactions collection and then sort that collection by type. Type only have values 1, 2, 3, and sorting should happen in that order.

If two transactions have the same type, I want to sort them by String attribute to (that is an account number, so it is all numerical).

How can I sort a collection with two parameters like this?

processTransactions() method should take unsorted collection as a parameter, sort them and process them.

But the last line of code gives an error:

The method sort(List<T>) in the type Collections is not 
applicable for the arguments (Comparator<Assignment02_20190808022.Transactions>)
Alexander Ivanchenko
  • 25,667
  • 5
  • 22
  • 46
Vecta
  • 31
  • 5

1 Answers1

1

Use Java 8 methods of the Comparator interface comparing() and thenComparing():

Comparator<Transactions> byTypeAndTo = 
            Comparator.comparing(Transactions::getType)
                .thenComparing(Transactions::getTo);

In order to sort a list of objects, you can apply method sort() on it, available since Java 8:

transactions.sort(byTypeAndTo);

You can play around with this Online demo.

For more information on how to build comparators using Java 8, have a look at this tutorial.

But the last line of code gives an error:

The method signature is incorrect, I believe it should be:

public void processTransactions(Collection<Transactions> ts)

Don't confuse utility class Collections with the Collection interface, and don't omit the generic type parameter.

You can invoke method sort() only on the object of type List (the same with Collections.sort() it expects a list and comparator).

In order to sort the list provided in the guise of Collection you need to apply casting. Since you have a requirement that you method should expect an argument of type Collection there's no other way around it (but it's not good design). That's how it might look like:

public void processTransactions(Collection<Transactions> ts) {
    if (!(ts instanceof List<Transactions>)) {
        throw new IllegalArgumentException();
    }
    List<Transactions> transactions = (List<Transactions>) tr;
    
    transactions.sort(byTypeAndTo);

    // process the `transactions` list
}

Note: it's better to define a comparator inside the Bank class as a public static final field.

Alexander Ivanchenko
  • 25,667
  • 5
  • 22
  • 46
  • Couldn't figure it out. When I use Collections.sort(ts, byTypeAndTo); it says "The method sort(List, Comparator super T>) in the type Collections is not applicable for the arguments (Collections, Comparator)" – Vecta May 22 '22 at 12:38
  • 1
    @Vecta Have a look at this [Online demo](https://www.jdoodle.com/ia/r9y). – Alexander Ivanchenko May 22 '22 at 12:50
  • The method sort(List) in the type Collections is not applicable for the arguments (Comparator) This error comes now when I write what you wrote last time. I am trying to figure it out now. I am new to all of this. – Vecta May 22 '22 at 12:50
  • @Vecta You should invoke `sort()` on a list and pass a comparator as an argument, not the opposite. – Alexander Ivanchenko May 22 '22 at 12:52
  • I updated my question. For some reason I couldn't make your solution work. – Vecta May 22 '22 at 13:04
  • @Vecta Can you also add the line that causes the error. – Alexander Ivanchenko May 22 '22 at 13:18
  • It is the "transactions.sort(byTypeAndTo); When I change public void processTransactions(Collections ts) ----> public void processTransactions(Liststs) It goes away. But my teacher said: Do not assume that the Collection type passed will be of a sortable type on its own.Therefore, you should import java.util.* to make sure any type of Collection that gets passed will be accepted by your method." So I am not sure if this will met the requirements – Vecta May 22 '22 at 13:27
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/244937/discussion-between-vecta-and-alexander-ivanchenko). – Vecta May 22 '22 at 13:33
  • Methods–all methods from Project02 plus the following i.processTransactions(transactions: Collection, outFile: String): Takes an unsorted List of Transaction objects and sorts them according to the description above. Then processes each Transaction by type. If an exception is encountered, writes to the file given in String an error log according to the following:1.“ERROR: “ + Exception type + “: “ + Transaction type + {tab} + Account number(s)separated by tab + {tab} + amount This is the method details. – Vecta May 22 '22 at 13:37