From 0f36bb2440a4909034da5dd28397134fb9ade808 Mon Sep 17 00:00:00 2001 From: Zach S-B Date: Wed, 25 Aug 2021 14:56:52 +1000 Subject: [PATCH] Coverted from array to linked list --- Scheduler.java | 72 +++++++++++++++++++++++++------------------------- 1 file changed, 36 insertions(+), 36 deletions(-) diff --git a/Scheduler.java b/Scheduler.java index e2709d1..5f74b39 100644 --- a/Scheduler.java +++ b/Scheduler.java @@ -3,15 +3,14 @@ import java.io.*; public class Scheduler { //Address of File name relative to code. Replace with request for filename? - String fileName = "datafiles/datafile1.txt"; + String fileName = "datafiles/datafile2.txt"; - //Initialise Processes. Assumes max of 10. - Process[] p = new Process[10]; - private int processCount = -1; + LinkedList processList = new LinkedList(); + LinkedList randomNum = new LinkedList<>(); - //Initialise Array for Random numbers. Assumes max of 50. - int[] randomNum = new int[50]; - int randomNumLength = -1; + + //Initialise Dispatcher time variable + int disp; /** * Main. Runs the program @@ -27,37 +26,32 @@ public class Scheduler { public void run() { readFile(fileName); //Read the file - + //Test Print - System.out.println("Begin Process List:"); - for (int i = 0; i <= processCount; i++) - { - System.out.print("ID: "); - System.out.println(p[i].getId()); + System.out.println("DISP: " + disp + "\n"); - System.out.print("Arrive: "); - System.out.println(p[i].getArrive()); - - System.out.print("ExecSize: "); - System.out.println(p[i].getExecSize()); - - System.out.print("Tickets: "); - System.out.println(p[i].getTickets()); - + System.out.println("Begin Process List:"); + + for (int i = 0; i < processList.size() ; i++) + { + System.out.println("ID: " + processList.get(i).getId()); + System.out.println("Arrive: " + processList.get(i).getArrive()); + System.out.println("ExecSize: " + processList.get(i).getExecSize()); + System.out.println("Tickets: " + processList.get(i).getTickets()); System.out.println(); } - System.out.println("End Process List."); + System.out.println("End Process List.\n"); System.out.println("Begin Random:"); - for (int i = 0; i <= randomNumLength; i++) + for (int i = 0; i < randomNum.size(); i++) { - System.out.println(randomNum[i]); + System.out.println(randomNum.get(i)); } System.out.println("End Random."); } - /** +/** * readFile. Reads the file given, creating processes as needed to save details. * @param fileNameInput address of the file to be read in. */ @@ -82,15 +76,20 @@ public class Scheduler { System.out.println("End of File Reached"); break; } + + else if(line.startsWith("DISP")) + { + disp =Integer.parseInt(line.substring(6)); + } //If the Line starts with "ID:", create a new process - if(line.startsWith("ID:")) + else if(line.startsWith("ID:")) { //Create new proccess - processCount++; //Increment the number of processes. - p[processCount] = new Process(); + Process temp = new Process(); - p[processCount].setId(line.substring(4)); //Set the id of the proccess to whatever remains of the line after the "ID: " + + temp.setId(line.substring(4)); //Set the id of the proccess to whatever remains of the line after the "ID: " //As we've read in the ID, we shall assume the following lines (until "END") are all part of the process definition. while(fileReader.hasNextLine()) @@ -104,18 +103,18 @@ public class Scheduler { else if(line.startsWith("Arrive:")) { - p[processCount].setArrive(Integer.parseInt(line.substring(8))); + temp.setArrive(Integer.parseInt(line.substring(8))); } else if(line.startsWith("ExecSize:")) { - p[processCount].setSize(Integer.parseInt(line.substring(10))); + temp.setSize(Integer.parseInt(line.substring(10))); } else if(line.startsWith("Tickets:")) { - p[processCount].setTickets(Integer.parseInt(line.substring(9))); + temp.setTickets(Integer.parseInt(line.substring(9))); } else @@ -123,6 +122,8 @@ public class Scheduler { //Delete process? } } + + processList.add(temp); } //If we read the line "BEGINRANDOM", we've started the random numbers section. Until the "ENDRANDOM" line, @@ -140,8 +141,7 @@ public class Scheduler { } else { - randomNumLength++; - randomNum[randomNumLength] = Integer.parseInt(line); + randomNum.add(Integer.parseInt(line)); } } } @@ -154,5 +154,5 @@ public class Scheduler { System.out.println("File not found."); //e.printStackTrace(); //Optional, shows more data. } - } + } } \ No newline at end of file