COMP2240-Assignment1/Scheduler.java
2021-08-13 20:42:38 +10:00

129 lines
3.9 KiB
Java

import java.util.*;
import java.io.*;
public class Scheduler {
//Address of File name relative to code. Replace with request for filename?
String fileName = "datafiles/datafile2.txt";
//Initialise Processes. Assumes max of 10.
Process[] p = new Process[10];
private int processCount = -1;
//Initialise Array for Random numbers. Assumes max of 50.
int[] randomNum = new int[50];
int randomNumLength = -1;
/**
* Main. Runs the program
*/
public static void main(String[] args){
Scheduler system = new Scheduler();
system.run();
}
/**
* Run. Most of the program starts from here.
*/
public void run()
{
readFile(fileName); //Read the file
//Test Print
for (int i = 0; i <= randomNumLength; i++)
{
System.out.println(randomNum[i]);
}
}
/**
* readFile. Reads the file given, creating processes as needed to save details.
* @param fileNameInput address of the file to be read in.
*/
public void readFile(String fileNameInput)
{
Scanner fileReader; //Scanner to read the file.
File dataSet = new File(fileNameInput);
//Try and open file. If it works, procede.
try {
fileReader = new Scanner(dataSet);
System.out.println("File Opened.");
while(fileReader.hasNextLine()){
String line = fileReader.nextLine();
if(line.equals("EOF"))
{
System.out.println("End of File Reached");
break;
}
//If it is a new proccess
if(line.startsWith("ID:"))
{
//Create new proccess
processCount++;
p[processCount] = new Process();
p[processCount].setId(line.substring(4));
while(fileReader.hasNextLine())
{
line = fileReader.nextLine();
if(line.equals("END"))
{
break;
}
else if(line.startsWith("Arrive:"))
{
p[processCount].setArrive(Integer.parseInt(line.substring(8)));
}
else if(line.startsWith("ExecSize:"))
{
p[processCount].setSize(Integer.parseInt(line.substring(10)));
}
else if(line.startsWith("Tickets:"))
{
p[processCount].setTickets(Integer.parseInt(line.substring(9)));
}
}
}
else if(line.equals("BEGINRANDOM"))
{
while(fileReader.hasNextLine())
{
line = fileReader.nextLine();
if(line.equals("ENDRANDOM"))
{
break;
}
else
{
randomNumLength++;
randomNum[randomNumLength] = Integer.parseInt(line);
}
}
}
}
fileReader.close();
}
//If the file isn't found, through a hissy fit.
catch (FileNotFoundException e) {
System.out.println("File not found.");
//e.printStackTrace(); //Optional, shows more data.
}
}
}