I want to use that method to sort the elements of the list by the end date.
Then I would like to delete the overlapping elements.
Basically my code will help me implement a meeting system.
Two meetings with starting dates in ascending order overlap if the start date of the second meeting is less than or equal to the start date of the first meeting.
I've put in my code what would be my example of an expected output to make the things look clear.
class Meeting implements Comparable<Meeting> {
private Calendar start, end;
public Meeting(Calendar start, Calendar end) {
if (start.compareTo(end) > 0)
throw new IllegalArgumentException("Invalid date");
this.start = start;
this.end = end;
}
public Calendar getStarted() {
return start;
}
public Calendar getEnding() {
return end;
}
public int compareTo(Meeting m) {
return this.start.compareTo(m.getStarted());
}
public String toString() {
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");
return sdf.format(start.getTime()) + " -> " + sdf.format(end.getTime());
}
}
class Planificator {
public static void plan(List<Meeting> meetings) {
//My method with the comparator
}
public class Main {
public static void main(String[] args) throws Exception {
try {
List<Meeting> l = new ArrayList<>();
l.add(new Meeting(new GregorianCalendar(1,2,3),
new GregorianCalendar(2,2,3)));
l.add(new Meeting(new GregorianCalendar(1, 2, 3),
new GregorianCalendar(5, 2, 3)));
l.add(new Meeting(new GregorianCalendar(3, 2, 3),
new GregorianCalendar(5, 2, 3)));
Planificator.plan(l);
System.out.println(l); // [03/03/0001 12:00:00 -> 03/03/0002 12:00:00, 03/03/0003 12:00:00 -
> 03/03/0005 12:00:00]
} catch (IllegalArgumentException e) {
System.out.print(e.getMessage());
}
}
}