So I've tried the following:
1) I fixed isLastDateInDates() so that it compares the getTime() values rather than the object hashcodes. It now returns true. I left updateLastDate as is, as it should change the variable last date to the updated one. Validator won't accept the last 2 conditions.
2) I made an iterator in updateLastDate so that I could "remove" the object instead of just changing its value. Then I added a new value to the set with the updated time. Validator didn't like it.
3) I wondered if they wanted this to always work no matter how many new dates were added, so I changed the dates HashSet to a TreeSet and iterated through it to set the last element to lastDate. This way, if a new more recent date was added, all the other functions should work on that Date object instead of just hardcoding one of them. The validator didn't like this either.
Am I misunderstanding the problem? Is there another option I haven't considered yet?
(my code reflects #3 above)
package com.codegym.task.task36.task3612;
import java.util.*;
/*
Why doesn't the set contain an element?
*/
public class Solution {
private Set<Date> dates;
private Date lastDate;
public static void main(String[] args) {
Solution solution = new Solution();
solution.initializeDates();
solution.updateLastDate(3_600_000L);
System.out.println(solution.isLastDateInDates());
}
public boolean isLastDateInDates() {
for (Date currentDate : dates) {
if (currentDate.getTime() == lastDate.getTime()) {
return true;
}
}
return false;
}
private void initializeDates() {
dates = new TreeSet<Date>();
dates.add(new Date(9999999L));
dates.add(new Date(2222222L));
dates.add(new Date(3333333L));
dates.add(new Date(4444444L));
dates.add(new Date(5555555L));
Iterator<Date> iterator = dates.iterator();
while (iterator.hasNext()) {
lastDate = iterator.next();
}
}
protected void updateLastDate(long delta) {
Iterator<Date> iter = dates.iterator();
Date oldLastDate = new Date();
while (iter.hasNext()) {
Date currentDate = iter.next();
if (currentDate.getTime() == lastDate.getTime()) {
oldLastDate = (Date) currentDate.clone();
iter.remove();
}
}
lastDate = oldLastDate;
oldLastDate.setTime(oldLastDate.getTime() + delta);
dates.add(lastDate);
}
}