Repository: TheAlgorithms/Java
Pull request #4999 (2024-01-07 04:28:20)

chore: simplified minimizing lateness
<!--
Thank you for your contribution!
In order to reduce the number of notifications sent to the maintainers, please:
- create your PR as draft, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests#draft-pull-requests,
- make sure that all of the CI checks pass,
- mark your PR as ready for review, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/changing-the-stage-of-a-pull-request#marking-a-pull-request-as-ready-for-review
-->

<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
- [x] All new code is formatted with `clang-format -i --style=file path/to/your/file.java`

inishantjain: I'll do it. ☑️

Patch:
diff --git a/src/main/java/com/thealgorithms/greedyalgorithms/MinimizingLateness.java b/src/main/java/com/thealgorithms/greedyalgorithms/MinimizingLateness.java
new file mode 100644
index 00000000..938ae79b
--- /dev/null
+++ b/src/main/java/com/thealgorithms/greedyalgorithms/MinimizingLateness.java
@@ -0,0 +1,43 @@
+package com.thealgorithms.greedyalgorithms;
+
+import java.util.Arrays;
+
+public class MinimizingLateness {
+
+    public static class Job {
+        String jobName;
+        int startTime = 0;
+        int lateness = 0;
+        int processingTime;
+        int deadline;
+
+        public Job(String jobName, int processingTime, int deadline) {
+            this.jobName = jobName;
+            this.processingTime = processingTime;
+            this.deadline = deadline;
+        }
+
+        public static Job of(String jobName, int processingTime, int deadline) {
+            return new Job(jobName, processingTime, deadline);
+        }
+
+        @Override
+        public String toString() {
+            return String.format("%s, startTime: %d, endTime: %d, lateness: %d", jobName, startTime, processingTime + startTime, lateness);
+        }
+    }
+
+    static void calculateLateness(Job... jobs) {
+
+        // sort the jobs based on their deadline
+        Arrays.sort(jobs, (a, b) -> a.deadline - b.deadline);
+
+        int startTime = 0;
+
+        for (Job job : jobs) {
+            job.startTime = startTime;
+            startTime += job.processingTime;
+            job.lateness = Math.max(0, startTime - job.deadline); // if the job finishes before deadline the lateness is 0
+        }
+    }
+}
diff --git a/src/main/java/com/thealgorithms/minimizinglateness/MinimizingLateness.java b/src/main/java/com/thealgorithms/minimizinglateness/MinimizingLateness.java
deleted file mode 100644
index fc7eae6a..00000000
--- a/src/main/java/com/thealgorithms/minimizinglateness/MinimizingLateness.java
+++ /dev/null
@@ -1,55 +0,0 @@
-package com.thealgorithms.minimizinglateness;
-
-import java.io.BufferedReader;
-import java.io.FileReader;
-import java.io.IOException;
-import java.util.StringTokenizer;
-
-public class MinimizingLateness {
-
-    private static class Schedule { // Schedule class
-
-        int t = 0; // Time required for the operation to be performed
-        int d = 0; // Time the job should be completed
-        public Schedule(int t, int d) {
-            this.t = t;
-            this.d = d;
-        }
-    }
-
-    public static void main(String[] args) throws IOException {
-        StringTokenizer token;
-
-        BufferedReader in = new BufferedReader(new FileReader("MinimizingLateness/lateness_data.txt"));
-        String ch = in.readLine();
-        if (ch == null || ch.isEmpty()) {
-            in.close();
-            return;
-        }
-        int indexCount = Integer.parseInt(ch);
-        System.out.println("Input Data : ");
-        System.out.println(indexCount); // number of operations
-        Schedule[] array = new Schedule[indexCount]; // Create an array to hold the operation
-        int i = 0;
-        while ((ch = in.readLine()) != null) {
-            token = new StringTokenizer(ch, " ");
-            // Include the time required for the operation to be performed in the array and the time
-            // it should be completed.
-            array[i] = new Schedule(Integer.parseInt(token.nextToken()), Integer.parseInt(token.nextToken()));
-            i++;
-            System.out.println(array[i - 1].t + " " + array[i - 1].d);
-        }
-
-        int tryTime = 0; // Total time worked
-        int lateness = 0; // Lateness
-        for (int j = 0; j < indexCount - 1; j++) {
-            tryTime = tryTime + array[j].t; // Add total work time
-            // Lateness
-            lateness = lateness + Math.max(0, tryTime - array[j].d);
-        }
-        System.out.println();
-        System.out.println("Output Data : ");
-        System.out.println(lateness);
-        in.close();
-    }
-}
diff --git a/src/main/java/com/thealgorithms/minimizinglateness/lateness_data.txt b/src/main/java/com/thealgorithms/minimizinglateness/lateness_data.txt
deleted file mode 100644
index e2bac0d1..00000000
--- a/src/main/java/com/thealgorithms/minimizinglateness/lateness_data.txt
+++ /dev/null
@@ -1,7 +0,0 @@
-6
-3 6
-2 8
-1 9
-4 9
-3 14
-2 15
\ No newline at end of file


Test patch:
diff --git a/src/test/java/com/thealgorithms/greedyalgorithms/MinimizingLatenessTest.java b/src/test/java/com/thealgorithms/greedyalgorithms/MinimizingLatenessTest.java
new file mode 100644
index 00000000..04f6900d
--- /dev/null
+++ b/src/test/java/com/thealgorithms/greedyalgorithms/MinimizingLatenessTest.java
@@ -0,0 +1,43 @@
+package com.thealgorithms.greedyalgorithms;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import com.thealgorithms.greedyalgorithms.MinimizingLateness.Job;
+import org.junit.jupiter.api.Test;
+
+public class MinimizingLatenessTest {
+
+    @Test
+    void testCalculateLateness() {
+        // Test case with three jobs
+        Job job1 = new Job("Job1", 4, 6);
+        Job job2 = new Job("Job2", 2, 8);
+        Job job3 = new Job("Job3", 1, 9);
+        Job job4 = new Job("Job4", 5, 9);
+        Job job5 = new Job("Job5", 4, 10);
+        Job job6 = new Job("Job6", 3, 5);
+
+        MinimizingLateness.calculateLateness(job1, job2, job3, job4, job5, job6);
+
+        // Check lateness for each job
+        assertEquals(6, job4.lateness);
+        assertEquals(0, job6.lateness);
+        assertEquals(1, job2.lateness);
+    }
+
+    @Test
+    void testCheckStartTime() {
+
+        Job job1 = new Job("Job1", 2, 5);
+        Job job2 = new Job("Job2", 1, 7);
+        Job job3 = new Job("Job3", 3, 8);
+        Job job4 = new Job("Job4", 2, 4);
+        Job job5 = new Job("Job5", 4, 10);
+
+        MinimizingLateness.calculateLateness(job1, job2, job3, job4, job5);
+
+        assertEquals(2, job1.startTime);
+        assertEquals(5, job3.startTime);
+        assertEquals(8, job5.startTime);
+    }
+}