-1

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!

Progman
  • 16,827
  • 6
  • 33
  • 48
Esmer Omer
  • 65
  • 5
  • How could you have found [`Collections.sort(List list)`](https://docs.oracle.com/javase/8/docs/api/java/util/Collections.html#sort-java.util.List-) and not seen the other [`Collections.sort(List list, Comparator super T> c)`](https://docs.oracle.com/javase/8/docs/api/java/util/Collections.html#sort-java.util.List-java.util.Comparator-) that is **documented** right next to it, which takes a `Comparator` to apply an alternative sorting method? I mean, you did at least *try* to look for alternates, right? You know, do that research thing you should be doing before asking here. – Andreas Sep 23 '20 at 00:59
  • @Andreas I don't know how to use that without modifying the meeting class, that is the problem. Because someone answered me below, but as I said in my post. I can not modify the Meeting class or prog class in anyway shape or form – Esmer Omer Sep 23 '20 at 05:28

2 Answers2

0

Add this method to the Meeting class:

public static int compareByEndDateAsc(Meeting m1, Meeting m2){
    return m1.getEnding().compareTo(m2.getEnding());
}

Change the plan method providing a Comparator:

public static void plan(List<Meeting> l) {
   Collections.sort(l, Meeting::compareByEndDateAsc);
 }

}

And make new tests covering the edge cases.

Swerts
  • 1
  • Unfortunately, I can not modify the Meeting class, and henceforth I can not add that method there. I only need to send my piece of code that will work with the other part because it's a submission on a platform. – Esmer Omer Sep 23 '20 at 05:38
0

So you can add a Comparator in the Planificator class like this:

class Planificator extends Meeting {

public Planificator(Calendar start, Calendar end) {
    super(start, end);
}

public static int compareByEndDateAsc(Meeting m1, Meeting m2){
    return m1.getEnding().compareTo(m2.getEnding());
}

public static void plan(List<Meeting> l) {
   Collections.sort(l,Planificator::compareByEndDateAsc);
 }

}

Swerts
  • 1