import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.HashMap;
import java.util.Map;
public class MaintenanceTracker {
private Map<String, LocalDate> lastExecutionDates; // Task name -> Last execution date
public MaintenanceTracker() {
lastExecutionDates = new HashMap<>();
}
/**
* Records the execution date of a maintenance task.
*
* @param taskName The name of the maintenance task.
* @param executionDate The date the task was executed.
* @return true if the record was updated, false if already recorded.
*/
public boolean recordExecution(String taskName, LocalDate executionDate) {
if (taskName == null || taskName.trim().isEmpty()) {
System.err.println("Error: Task name cannot be null or empty.");
return false; // Handle invalid task name.
}
if (executionDate == null) {
System.err.println("Error: Execution date cannot be null.");
return false; //Handle null execution date
}
lastExecutionDates.put(taskName, executionDate);
return true;
}
/**
* Checks if a maintenance task is due for execution based on a specified interval.
*
* @param taskName The name of the maintenance task.
* @param intervalDays The interval in days between executions.
* @return true if the task is due, false otherwise.
*/
public boolean isDue(String taskName, long intervalDays) {
if (taskName == null || taskName.trim().isEmpty()) {
System.err.println("Error: Task name cannot be null or empty.");
return false;
}
if (intervalDays <= 0) {
System.err.println("Error: Interval must be a positive number.");
return false; //Handle invalid interval.
}
LocalDate lastExecution = lastExecutionDates.get(taskName);
if (lastExecution == null) {
return true; // Task has never been executed, so it's due.
}
LocalDate today = LocalDate.now();
long daysSinceLastExecution = ChronoUnit.DAYS.between(lastExecution, today);
return daysSinceLastExecution >= intervalDays;
}
/**
* Gets the last execution date for a maintenance task.
*
* @param taskName The name of the maintenance task.
* @return The last execution date, or null if the task has never been executed.
*/
public LocalDate getLastExecutionDate(String taskName) {
if (taskName == null || taskName.trim().isEmpty()) {
System.err.println("Error: Task name cannot be null or empty.");
return null;
}
return lastExecutionDates.get(taskName);
}
public static void main(String[] args) {
MaintenanceTracker tracker = new MaintenanceTracker();
// Example usage
LocalDate now = LocalDate.now();
tracker.recordExecution("Filter Change", now);
tracker.recordExecution("Battery Check", now.plusDays(30));
System.out.println("Last execution date for Filter Change: " + tracker.getLastExecutionDate("Filter Change")); // Expected: current date
System.out.println("Last execution date for Battery Check: " + tracker.getLastExecutionDate("Battery Check")); // Expected: date 30 days ago
System.out.println("Is Filter Change due? " + tracker.isDue("Filter Change", 30)); // Expected: false
System.out.println("Is Battery Check due? " + tracker.isDue("Battery Check", 30)); // Expected: true
System.out.println("Is NonExistent Task due? " + tracker.isDue("NonExistentTask", 30)); // Expected: true
}
}
Add your comment