0

I am working on an assignment in java where I have Prisoner class which have date of offence (dd-MM-yyyy) localDate format, name and years in prison. I also have Cell class where I have to add prisoners in the cell.

I have to make a method to show which Prisoner should be release first from the Cell.

Unfortunately, I have no idea how to do that. I already made a method for adding the prisoners in the cell and I use HashSet but I have not idea how to calculate who should be released first.

This is my code for adding the prisoners

public Boolean addPrisoner(Prisoner prisoner) {
  if (this.prisonerList.size() <= this.cellSize) {
    return this.prisonerList.add(prisoner);
  }
  return false;
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
GEORGI DIMITROV
  • 134
  • 1
  • 10
  • Just sort your list of prisoners by the date property. See [Sort ArrayList of custom Objects by property](https://stackoverflow.com/questions/2784514/sort-arraylist-of-custom-objects-by-property). Edit, just saw that you are using a HashSet, in that case just create a temporary ArrayList from your set using the copy constructor. – OH GOD SPIDERS Oct 16 '20 at 14:29
  • @OHGODSPIDERS hmmm yes that could work , I will try it – GEORGI DIMITROV Oct 16 '20 at 14:31
  • Or if you only need the single prisoner, `Collections.min(prisoners, Comparator.comparing(Prisoner::getReleaseDate))`. – chrylis -cautiouslyoptimistic- Oct 16 '20 at 14:38
  • @chrylis-cautiouslyoptimistic- quick question, do I have to iterate throw the whole list first ? – GEORGI DIMITROV Oct 16 '20 at 14:44
  • If your prisoners are only stored in a hashset then it must iterate through the whole list first. If it was stored in another data structure that might not be the case. A heap for example where items are inserted based on release date would be O(1) but that pushes the costs of computing to other operations (like insert). – Omar Abdel Bari Oct 16 '20 at 14:54
  • @GEORGIDIMITROV That method does the iteration for you, no sorting required, but you only get the single "least" result, and you have to tell it how to compare (by release date). – chrylis -cautiouslyoptimistic- Oct 16 '20 at 16:56
  • @chrylis-cautiouslyoptimistic- I did it, I have to ask , is there a way to do it without Collection and Comparator ? – GEORGI DIMITROV Oct 17 '20 at 11:00
  • Of course. You can write your own loop and call `prisoner.getReleaseDate().compareTo(...)` yourself. – chrylis -cautiouslyoptimistic- Oct 17 '20 at 23:19

1 Answers1

0

Have your prisoner calculate his or her own release date (in real life this might tempt to calculate fraudulent release dates, but as long as you write the method, it’s OK in a Java program). For example:

public class Prisoner {

    private LocalDate dateOfOffence;
    private int yearsOfSentence;
    
    public LocalDate getReleaseDate() {
        return dateOfOffence.plusYears(yearsOfSentence);
    }
    
}

Now Collections.min can find the prisoner having the earliest release date:

        Prisoner nextPrisonerToBeReleased = Collections.min(
                prisonerList, Comparator.comparing(Prisoner::getReleaseDate));
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161