1. import java.time.LocalDate;
  2. import java.time.temporal.ChronoUnit;
  3. import java.util.HashMap;
  4. import java.util.Map;
  5. public class MaintenanceTracker {
  6. private Map<String, LocalDate> lastExecutionDates; // Task name -> Last execution date
  7. public MaintenanceTracker() {
  8. lastExecutionDates = new HashMap<>();
  9. }
  10. /**
  11. * Records the execution date of a maintenance task.
  12. *
  13. * @param taskName The name of the maintenance task.
  14. * @param executionDate The date the task was executed.
  15. * @return true if the record was updated, false if already recorded.
  16. */
  17. public boolean recordExecution(String taskName, LocalDate executionDate) {
  18. if (taskName == null || taskName.trim().isEmpty()) {
  19. System.err.println("Error: Task name cannot be null or empty.");
  20. return false; // Handle invalid task name.
  21. }
  22. if (executionDate == null) {
  23. System.err.println("Error: Execution date cannot be null.");
  24. return false; //Handle null execution date
  25. }
  26. lastExecutionDates.put(taskName, executionDate);
  27. return true;
  28. }
  29. /**
  30. * Checks if a maintenance task is due for execution based on a specified interval.
  31. *
  32. * @param taskName The name of the maintenance task.
  33. * @param intervalDays The interval in days between executions.
  34. * @return true if the task is due, false otherwise.
  35. */
  36. public boolean isDue(String taskName, long intervalDays) {
  37. if (taskName == null || taskName.trim().isEmpty()) {
  38. System.err.println("Error: Task name cannot be null or empty.");
  39. return false;
  40. }
  41. if (intervalDays <= 0) {
  42. System.err.println("Error: Interval must be a positive number.");
  43. return false; //Handle invalid interval.
  44. }
  45. LocalDate lastExecution = lastExecutionDates.get(taskName);
  46. if (lastExecution == null) {
  47. return true; // Task has never been executed, so it's due.
  48. }
  49. LocalDate today = LocalDate.now();
  50. long daysSinceLastExecution = ChronoUnit.DAYS.between(lastExecution, today);
  51. return daysSinceLastExecution >= intervalDays;
  52. }
  53. /**
  54. * Gets the last execution date for a maintenance task.
  55. *
  56. * @param taskName The name of the maintenance task.
  57. * @return The last execution date, or null if the task has never been executed.
  58. */
  59. public LocalDate getLastExecutionDate(String taskName) {
  60. if (taskName == null || taskName.trim().isEmpty()) {
  61. System.err.println("Error: Task name cannot be null or empty.");
  62. return null;
  63. }
  64. return lastExecutionDates.get(taskName);
  65. }
  66. public static void main(String[] args) {
  67. MaintenanceTracker tracker = new MaintenanceTracker();
  68. // Example usage
  69. LocalDate now = LocalDate.now();
  70. tracker.recordExecution("Filter Change", now);
  71. tracker.recordExecution("Battery Check", now.plusDays(30));
  72. System.out.println("Last execution date for Filter Change: " + tracker.getLastExecutionDate("Filter Change")); // Expected: current date
  73. System.out.println("Last execution date for Battery Check: " + tracker.getLastExecutionDate("Battery Check")); // Expected: date 30 days ago
  74. System.out.println("Is Filter Change due? " + tracker.isDue("Filter Change", 30)); // Expected: false
  75. System.out.println("Is Battery Check due? " + tracker.isDue("Battery Check", 30)); // Expected: true
  76. System.out.println("Is NonExistent Task due? " + tracker.isDue("NonExistentTask", 30)); // Expected: true
  77. }
  78. }

Add your comment