When I call shortestJobFirst method it doesn't print anything, so I think I passed it as reference to the firstComeFirstServe method, not value, so it deletes all the nodes. I want to keep my LinkedList jobOrder for 4 tasks. I had tried to use subList() but it won't be allowed me to remove any node, so I think that just for view only. Is there any easy way to fix this? Maybe I Should just create 4 LinkedList? Please get me a hint thank you very much.
txt file:
Job1
5
Job2
2
Job3
7
Job4
4
Job5
3
Job6
8
Below is my code
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.Scanner;
public class Main {
public static String filePath;
public static void main(String[] args) throws Exception {
LinkedList<Job> jobOrder = new LinkedList<Job>();
System.out.println("Please enter input txt file path");
System.out.println("Example: C:/Users/d/OneDrive/Desktop/Job.txt \n" );
@SuppressWarnings("resource")
//Create scanner to scan path for txt file
Scanner input = new Scanner(System.in);
//For user input
System.out.print(">> " );
filePath = input.nextLine();
//Read input txt file
fileReader(jobOrder, filePath );
//Call first come first serve algorithm
firstComeFirstServe(jobOrder );
//Shortest job first algorithm
shortestJobFirst (jobOrder);
}
//My FCFS method
public static void firstComeFirstServe(LinkedList<Job> jList )
{
int startTime = 0;
int endTime = 0;
System.out.println("(a) First Come First Service ");
System.out.println("Job#" + " | " + " Start time" + " | " + " End Time" +" \t | " + "Job Completion ");
while( !jList.isEmpty())
{
endTime = endTime + jList.getFirst().getId();
System.out.println( jList.getFirst().getName() + " | \t" + startTime + " \t | "
+ endTime +" \t | " + jList.getFirst().getName() + " completed @ " + endTime);
startTime = endTime;
jList.remove();
}
System.out.println(" ");
}
//My SJF method
public static void shortestJobFirst (LinkedList<Job> jList )
{
Collections.sort(jList, new ascendingComparator());
int startTime = 0;
int endTime = 0;
System.out.println("(b) Shortest Job First ");
System.out.println("Job#" + " | " + " Start time" + " | " + " End Time" +" \t | " + "Job Completion ");
while( !jList.isEmpty())
{
endTime = endTime + jList.getFirst().getId();
System.out.println( jList.getFirst().getName() + " | \t" + startTime + " \t | "
+ endTime +" \t | " + jList.getFirst().getName() + " completed @ " + endTime);
startTime = endTime;
jList.remove();
}
System.out.println(" ");
}
//This method will read txt file and copy it to LinkedList
public static void fileReader(LinkedList<Job> jList, String path ) throws Exception
{
//Scan txt file
Scanner sc = new Scanner(new BufferedReader(new FileReader(path)));
String tempName = "";
int tempLength = 0;
int lineCount = 0;
//Read while the scan still has next integers
while(sc.hasNextLine())
{
lineCount = lineCount + 1;
if (lineCount % 2 != 0)
{
tempName = sc.next();
}
else
{
tempLength = sc.nextInt();
jList.add( new Job(tempName, tempLength) );
}
}
sc.close();
}
}
////
import java.util.Comparator;
class Job{
private String jobNum;
private int jobLength;
public Job(String jobNum, int jobLength){
this.jobLength = jobLength;
this.jobNum = jobNum;
}
//Change this for display later
public String toString(){
return "[" + this.jobNum + "=>" + this.jobLength + "]";
}
public int getId(){
return this.jobLength;
}
public String getName(){
return this.jobNum;
}
}
class ascendingComparator implements Comparator<Job>{
public int compare(Job Job1, Job Job2) {
return Job1.getId() - Job2.getId();
}
}