Search code examples
javajavafxlocaltimeobservablelist

How can I add the missing time using LocalTime in ObservableList?


I am trying to create a scheduling, where I can only get the inputted time and name of schedule.

I wanted to put the schedule in a List but if there's no other schedule, the List should be add a new empty schedule starting from 6:00 AM if there's no other early time.

I have ObservableList<DataClass>, it contains LocalTime and some String.

Example 1: Let's say the list contains 3 items:

[04:00 AM] [Some String]
[06:30 AM] [Some String]
[05:00 PM] [Some String]

I want to add the missing time in the list from 4:00 AM to 5:00 PM, so the list will be:

[04:00 AM] [Some String]
[05:00 AM] [Some String]
[06:30 AM] [Some String]
[07:00 AM] [Some String]
[08:00 AM] [Some String]
[09:00 AM] [Some String]
[10:00 AM] [Some String]
[11:00 AM] [Some String]
[12:00 PM] [Some String]
[01:00 PM] [Some String]
[02:00 PM] [Some String]
[03:00 PM] [Some String]
[04:00 PM] [Some String]
[05:00 PM] [Some String]

Example 2: Let's say the list contains 2 items:

[08:30 AM] [Some String]
[02:00 PM] [Some String]

I want to add the missing time in the list from 6:00 AM to 5:00 PM, so the list will be:

[06:00 AM] [Some String]
[07:00 AM] [Some String]
[08:00 AM] [Some String]
[09:00 AM] [Some String]
[10:00 AM] [Some String]
[11:00 AM] [Some String]
[12:00 PM] [Some String]
[01:00 PM] [Some String]
[02:00 PM] [Some String]
[03:00 PM] [Some String]
[04:00 PM] [Some String]
[05:00 PM] [Some String]

Example 3: Let's say the list contains 1 items:

[08:00 PM] [Some String]

I want to add the missing time in the list from 6:00 AM to 8:00 PM, so the list will be.

[06:00 AM] [Some String]
[07:00 AM] [Some String]
[08:00 AM] [Some String]
[09:00 AM] [Some String]
[10:00 AM] [Some String]
[11:00 AM] [Some String]
[12:00 PM] [Some String]
[01:00 PM] [Some String]
[02:00 PM] [Some String]
[03:00 PM] [Some String]
[04:00 PM] [Some String]
[05:00 PM] [Some String]
[06:00 PM] [Some String]
[07:00 PM] [Some String]
[08:00 PM] [Some String]

The time should be start at 06:00 AM if there's no other early time, otherwise the time will start at that early time.

The time should be end at 5:00 PM if there's no other time, otherwise the time will end at that specific time, I want to add only HOUR like increment hour so there shouldn't be 6:30, 5:30 unless it is manually inputted.

I am thinking of the following logic but I can't proceed because of lock on idea.

  1. Sort the list base on the time from AM to PM to get the first time

  2. Check if the time of the first data is equal or less than 6:00 AM
    if true, then start from that time and continue adding the missing time until the 5:00 PM or the last time is reach.
    if false, then start from 6:00 AM and continue adding the missing time until the 5:00 PM or the last time is reach.

the below code is currently what I have and puzzling.

private void fillTheList(){
        ObservableList<DataClass> data = FXCollections.observableArrayList();
        Comparator<DataClass> comparator = Comparator.comparing(DataClass::getLocalTime);

        data.add(new DataClass(convertTime("05:00 PM"), "Sample Content"));
        data.add(new DataClass(convertTime("06:30 AM"), "Sample Content"));

        FXCollections.sort(data,comparator); //Sort the list from AM to PM

        for (DataClass list : data){
            if(list.getLocalTime().isBefore(LocalTime.of(6,0))){
                //The time is less than 6:00 AM then it should start here and Add the missing time but I don't know what to do next...
            }else{
                //the time is not less than 6:00 AM... I don't know what to do next..
            }
        }
        FXCollections.sort(data,comparator); //Sort the list from AM to PM again
}

private LocalTime convertTime(String timeString){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("hh:mm a");
        return LocalTime.parse(timeString, formatter);
}

PS: I'm not actually sure what I'm going to ask so feel free to suggest an edit if necessary.

UPDATE: DataClass.class

public class DataClass {

    private LocalTime localTime;
    private String content;

    public DataClass(LocalTime localTime, String content){
        this.localTime = localTime;
        this.content = content;
    }

    public LocalTime getLocalTime(){
        return localTime;
    }

    public String getContent(){
        return content;
    }
}

Solution

  • You’re well on the way yourself. Furthermore azro is correct that you need to find the min and max hour to consider for insertion by comparing to the min and max time already in the data. Jai is correct that you should check whether a time is already in the list before inserting it and that a stream is convenient for this purpose. My version uses an int for iterating over the hours, but all the others are correct that a LocalTime works too.

        int minHour = 6;
        int maxHour = 17;
        if (! data.isEmpty()) {
            // adjust min and max from list contents
            int minExisintgHour = data.get(0).getLocalTime().getHour();
            if (minExisintgHour < minHour) {
                // if the list already contained 4:00 or 4:30,
                // we only need to add hours from 5, so add 1
                minHour = minExisintgHour + 1;
            }
            int maxExisintgHour = data.get(data.size() - 1).getLocalTime().getHour();
            if (maxExisintgHour > maxHour) {
                maxHour = maxExisintgHour;
            }
        }
        for (int hour = minHour; hour <= maxHour; hour++) {
            LocalTime time = LocalTime.of(hour, 0);
            boolean alreadyInData = data.stream().anyMatch(d -> d.getLocalTime().equals(time));
            if (! alreadyInData) {
                data.add(new DataClass(time, "Added beacuse time was missing"));
            }
        }
    

    I am assuming that you are sorting the list before and after the above code as in the question. Sorting before could be omitted if you do a linear traversal for min and max (something a couple of streams can do too).

    Sample resulting list:

    [06:00 AM] [Added beacuse time was missing]
    [06:30 AM] [Sample Content]
    [07:00 AM] [Added beacuse time was missing]
    [08:00 AM] [Added beacuse time was missing]
    [09:00 AM] [Added beacuse time was missing]
    [10:00 AM] [Added beacuse time was missing]
    [11:00 AM] [Added beacuse time was missing]
    [12:00 PM] [Added beacuse time was missing]
    [01:00 PM] [Added beacuse time was missing]
    [02:00 PM] [Added beacuse time was missing]
    [03:00 PM] [Added beacuse time was missing]
    [04:00 PM] [Added beacuse time was missing]
    [05:00 PM] [Sample Content]