Eric Spencer

projects


← back to index

Free Time Calculator in Java

A Java program to find overlapping free time for up to four people.



Overview

This project finds overlapping free time in people's schedules.

How it works

  1. Enter availability for a given day (for example, 4-6pm and 8-10pm).
  2. Enter a second person's availability (for example, 5-9pm).
  3. The program prints a time spreadsheet and calculates when everyone is available.
  4. It works for up to four people at once.

Workings and caveats


Demo video

The source code was lost, because it lived on a school virtual machine. A sample run of the program was recorded: Watch on YouTube.

Watch on YouTube


Recreation

A recreation by ChatGPT, without the user experience of the original:


import java.util.*;

public class FreeTimeCalculator {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        boolean[] timeSlots = new boolean[24]; // each hour of the day

        Arrays.fill(timeSlots, true); // Start with all available

        for (int person = 1; person <= 4; person++) {
            System.out.println("Enter availability for Person " + person + " (e.g., 4-6,8-10 or 'x' to skip): ");
            String input = scanner.nextLine();
            if (input.equalsIgnoreCase("x")) break;

            boolean[] personSlots = new boolean[24];
            Arrays.fill(personSlots, false);

            String[] ranges = input.split(",");
            for (String range : ranges) {
                String[] hours = range.split("-");
                int start = Integer.parseInt(hours[0].trim());
                int end = Integer.parseInt(hours[1].trim());
                for (int i = start; i < end; i++) {
                    personSlots[i] = true;
                }
            }

            for (int i = 0; i < 24; i++) {
                timeSlots[i] = timeSlots[i] && personSlots[i];
            }
        }

        System.out.println("\nShared Free Time:");
        boolean found = false;
        for (int i = 0; i < 24; i++) {
            if (timeSlots[i]) {
                System.out.print(i + ":00 ");
                found = true;
            }
        }
        if (!found) {
            System.out.println("No common availability.");
        }
    }
}