Coverted from array to linked list

This commit is contained in:
Zach S-B 2021-08-25 14:56:52 +10:00
parent ab4ee99b78
commit 0f36bb2440

View File

@ -3,15 +3,14 @@ import java.io.*;
public class Scheduler { public class Scheduler {
//Address of File name relative to code. Replace with request for filename? //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. LinkedList<Process> processList = new LinkedList<Process>();
Process[] p = new Process[10]; LinkedList<Integer> randomNum = new LinkedList<>();
private int processCount = -1;
//Initialise Array for Random numbers. Assumes max of 50.
int[] randomNum = new int[50]; //Initialise Dispatcher time variable
int randomNumLength = -1; int disp;
/** /**
* Main. Runs the program * Main. Runs the program
@ -29,29 +28,24 @@ public class Scheduler {
readFile(fileName); //Read the file readFile(fileName); //Read the file
//Test Print //Test Print
System.out.println("DISP: " + disp + "\n");
System.out.println("Begin Process List:"); System.out.println("Begin Process List:");
for (int i = 0; i <= processCount; i++)
for (int i = 0; i < processList.size() ; i++)
{ {
System.out.print("ID: "); System.out.println("ID: " + processList.get(i).getId());
System.out.println(p[i].getId()); System.out.println("Arrive: " + processList.get(i).getArrive());
System.out.println("ExecSize: " + processList.get(i).getExecSize());
System.out.print("Arrive: "); System.out.println("Tickets: " + processList.get(i).getTickets());
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(); System.out.println();
} }
System.out.println("End Process List."); System.out.println("End Process List.\n");
System.out.println("Begin Random:"); 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."); System.out.println("End Random.");
@ -83,14 +77,19 @@ public class Scheduler {
break; break;
} }
else if(line.startsWith("DISP"))
{
disp =Integer.parseInt(line.substring(6));
}
//If the Line starts with "ID:", create a new process //If the Line starts with "ID:", create a new process
if(line.startsWith("ID:")) else if(line.startsWith("ID:"))
{ {
//Create new proccess //Create new proccess
processCount++; //Increment the number of processes. Process temp = new Process();
p[processCount] = 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. //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()) while(fileReader.hasNextLine())
@ -104,18 +103,18 @@ public class Scheduler {
else if(line.startsWith("Arrive:")) 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:")) 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:")) else if(line.startsWith("Tickets:"))
{ {
p[processCount].setTickets(Integer.parseInt(line.substring(9))); temp.setTickets(Integer.parseInt(line.substring(9)));
} }
else else
@ -123,6 +122,8 @@ public class Scheduler {
//Delete process? //Delete process?
} }
} }
processList.add(temp);
} }
//If we read the line "BEGINRANDOM", we've started the random numbers section. Until the "ENDRANDOM" line, //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 else
{ {
randomNumLength++; randomNum.add(Integer.parseInt(line));
randomNum[randomNumLength] = Integer.parseInt(line);
} }
} }
} }