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