-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSPN.java
More file actions
81 lines (67 loc) · 1.88 KB
/
SPN.java
File metadata and controls
81 lines (67 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
Title: SPN.java
Name: Dylan Kapustka (Dlk190000)
Instructor: Professor Ozbirn
Course: CS 4348.001 - S21
Date: 05/01/2021
Description: SPN class implements SPN algorithm
*/
import java.util.ArrayList;
public class SPN
{
//Main algorithm
public void mySchedule(ArrayList<myJob> list)
{
System.out.println("SPN"); //Print algorithm name for graph
int time = 0;
myJob nextJob;
//Copy list
ArrayList<myJob> newList = new ArrayList<>();
for(myJob job: list)
{
newList.add(new myJob(job));
}
int index;
while(!(newList.isEmpty()))
{
nextJob = min(newList,time);
//Get index
index = 0;
for(int i = 0; i < list.size(); i++)
{
if(list.get(i).getName().equals(nextJob.getName()))
{
index = i;
}
}
list.get(index).setSpaceCount(time);
time += nextJob.getDuration();
}
for(myJob job: list)
{
job.printMatrix();
}
}
//Helper function to find min
private myJob min(ArrayList<myJob> listCopy, int time)
{
myJob selected = null;
int index = 0;
int min = 1000;
for(int i = 0; i < listCopy.size(); i++)
{
if(listCopy.get(i).getArrivalTime() <= time)
{
if(listCopy.get(i).getDuration() <= min)
{
min = listCopy.get(i).getDuration();
index = i;
selected = listCopy.get(i);
selected.setSpaceCount(time);
}
}
}
listCopy.remove(index);
return selected;
}
}