I need some guidance please :D
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 extends Meeting {
public Planificator(Calendar start, Calendar end) {
super(start, end);
}
public static void plan(List<Meeting> l) {
Collections.sort(l) // This should sort them from the lowest end date to the highest
}
}
public class prog {
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);
} catch (IllegalArgumentException e) {
System.out.print(e.getMessage());
}
}
}
The problem is that I need to somehow override the method in the superclass Meeting, because I need to first sort them from the lowest END date to the Highest, not start date as you can see above in the Meeting class. So in other words, how can I sort this list the way I want to using Collections.sort(or other methods) and without modifying the Meeting class and prog class NOTE: I can't use lambda/ I can add other classes to help me out
Thank you a lot in advance and if there is anything else I need to improve on please let me know(I know the indentation is not perfect but I am still getting used to how you place code in stackoverflow website)
Have a lovely day everyone and happy coding!