Reputation: 134
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;
}
Upvotes: 0
Views: 767
Reputation: 86359
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));
Upvotes: 0