Repository: TheAlgorithms/Java
Pull request #3335 (2022-10-01 16:54:57)

fcfs-scheduling program
### **Describe your change:**

- [x] Add an algorithm? It is for the issue: Fcfs process scheduling algorithm #3291
- [x] Fix a bug or typo in an existing algorithm? New algorithm which is explained in the issue.
- [x] Documentation change? No.

#### References

Issue: Fcfs process scheduling algorithm #3291. Also got it from [Hacktoberfest2022](https://github.com/topics/hacktoberfest).

### **Checklist:**

<!-- Remove items that do not apply. 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.
- [x] I know that pull requests will not be merged if they fail the automated tests.
- [ ] This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
- [ ] All new Java files are placed inside an existing directory.
- [x] All filenames are in all uppercase characters with no spaces or dashes.
- [x] All functions and variable names follow Java naming conventions.
- [ ] All new algorithms have a URL in its comments that points to Wikipedia or other similar explanation.
- [x] If this pull request resolves one or more open issues then the commit message contains `Fixes: #{$ISSUE_NO}`.


smiti-maheshwari: @aitorfi I have made the changes that were requested. Kindly let me know if anything else is required. I have resolved the previous comments.
smiti-maheshwari: Kindly merge it if it's fine. :)

Patch:
diff --git a/src/main/java/com/thealgorithms/devutils/entities/ProcessDetails.java b/src/main/java/com/thealgorithms/devutils/entities/ProcessDetails.java
new file mode 100644
index 00000000..873e4636
--- /dev/null
+++ b/src/main/java/com/thealgorithms/devutils/entities/ProcessDetails.java
@@ -0,0 +1,56 @@
+package com.thealgorithms.devutils.entities;
+
+public class ProcessDetails {
+    private String processId;
+    private int arrivalTime;
+    private int burstTime;
+    private int waitingTime;
+    private int turnAroundTime;
+
+    public ProcessDetails(final String processId, final int arrivalTime, final int burstTime) {
+        this.processId = processId;
+        this.arrivalTime = arrivalTime;
+        this.burstTime = burstTime;
+    }
+
+    public String getProcessId() {
+        return processId;
+    }
+
+    public int getArrivalTime() {
+        return arrivalTime;
+    }
+
+    public int getBurstTime() {
+        return burstTime;
+    }
+
+
+    public int getWaitingTime() {
+        return waitingTime;
+    }
+
+    public int getTurnAroundTimeTime() {
+        return turnAroundTime;
+    }
+
+    public void setProcessId(final String processId) {
+        this.processId = processId;
+    }
+
+    public void setArrivalTime(final int arrivalTime) {
+        this.arrivalTime = arrivalTime;
+    }
+
+    public void setBurstTime(final int burstTime) {
+        this.burstTime = burstTime;
+    }
+
+    public void setWaitingTime(final int waitingTime) {
+        this.waitingTime = waitingTime;
+    }
+
+    public void setTurnAroundTimeTime(final int turnAroundTime) {
+        this.turnAroundTime = turnAroundTime;
+    }
+}
diff --git a/src/main/java/com/thealgorithms/scheduling/FCFSScheduling.java b/src/main/java/com/thealgorithms/scheduling/FCFSScheduling.java
new file mode 100644
index 00000000..b1c3411b
--- /dev/null
+++ b/src/main/java/com/thealgorithms/scheduling/FCFSScheduling.java
@@ -0,0 +1,47 @@
+package com.thealgorithms.scheduling;
+
+import com.thealgorithms.devutils.entities.ProcessDetails;
+
+import java.util.List;
+
+/**
+ * Non-pre-emptive First Come First Serve scheduling. This can be understood here - https://www.scaler.com/topics/first-come-first-serve/
+ */
+public class FCFSScheduling {
+
+    private List<ProcessDetails> processes;
+
+    FCFSScheduling(final List<ProcessDetails> processes) {
+        this.processes = processes;
+    }
+
+    public void scheduleProcesses() {
+        evaluateWaitingTime();
+        evaluateTurnAroundTime();
+    }
+
+    private void evaluateWaitingTime() {
+        int processesNumber = processes.size();
+
+        if(processesNumber == 0) {
+            return;
+        }
+
+        int waitingTime = 0;
+        int burstTime = processes.get(0).getBurstTime();
+
+        processes.get(0).setWaitingTime(waitingTime); // for the first process, waiting time will be 0.
+
+        for(int i=1; i<processesNumber; i++) {
+           processes.get(i).setWaitingTime(waitingTime + burstTime);
+           waitingTime = processes.get(i).getWaitingTime();
+           burstTime = processes.get(i).getBurstTime();
+        }
+    }
+
+    private void evaluateTurnAroundTime() {
+        for(int i=0; i<processes.size(); i++) {
+            processes.get(i).setTurnAroundTimeTime(processes.get(i).getBurstTime() + processes.get(i).getWaitingTime());
+        }
+    }
+}


Test patch:
diff --git a/src/test/java/com/thealgorithms/scheduling/FCFSSchedulingTest.java b/src/test/java/com/thealgorithms/scheduling/FCFSSchedulingTest.java
new file mode 100644
index 00000000..c25a9c93
--- /dev/null
+++ b/src/test/java/com/thealgorithms/scheduling/FCFSSchedulingTest.java
@@ -0,0 +1,50 @@
+package com.thealgorithms.scheduling;
+
+import com.thealgorithms.devutils.entities.ProcessDetails;
+import org.junit.jupiter.api.Test;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+public class FCFSSchedulingTest {
+
+
+    @Test
+    public void testingProcesses() {
+        List<ProcessDetails> processes = addProcessesForFCFS();
+        final FCFSScheduling fcfsScheduling = new FCFSScheduling(processes); // for sending to FCFS
+
+        fcfsScheduling.scheduleProcesses();
+
+        assertEquals(3, processes.size());
+
+        assertEquals("P1", processes.get(0).getProcessId());
+        assertEquals(0, processes.get(0).getWaitingTime());
+        assertEquals(10, processes.get(0).getTurnAroundTimeTime());
+
+        assertEquals("P2", processes.get(1).getProcessId());
+        assertEquals(10, processes.get(1).getWaitingTime());
+        assertEquals(15, processes.get(1).getTurnAroundTimeTime());
+
+        assertEquals("P3", processes.get(2).getProcessId());
+        assertEquals(15, processes.get(2).getWaitingTime());
+        assertEquals(23, processes.get(2).getTurnAroundTimeTime());
+
+    }
+
+    private List<ProcessDetails> addProcessesForFCFS() {
+        final ProcessDetails process1 = new ProcessDetails("P1", 0, 10);
+        final ProcessDetails process2 = new ProcessDetails("P2", 1, 5);
+        final ProcessDetails process3 = new ProcessDetails("P3", 2, 8);
+
+        final List<ProcessDetails> processDetails = new ArrayList<>();
+        processDetails.add(process1);
+        processDetails.add(process2);
+        processDetails.add(process3);
+
+        return processDetails;
+    }
+
+}