import java.util.*; import java.io.*; public class Scheduler { //Address of File name relative to code. Replace with request for filename? String fileName = "datafiles/datafile1.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 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.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(); } System.out.println("End Process List."); System.out.println("Begin Random:"); for (int i = 0; i <= randomNumLength; i++) { System.out.println(randomNum[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. */ 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."); //Progress through each line in the text file while(fileReader.hasNextLine()){ String line = fileReader.nextLine(); //If the line has "EOF" written, stop reading lines. if(line.equals("EOF")) { System.out.println("End of File Reached"); break; } //If the Line starts with "ID:", create a new process if(line.startsWith("ID:")) { //Create new proccess processCount++; //Increment the number of processes. 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: " //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()) { 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 { //Delete process? } } } //If we read the line "BEGINRANDOM", we've started the random numbers section. Until the "ENDRANDOM" line, //we'll just keep reading in each number (and it is assumed each line is a single number) and adding it //to the random number array. 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, throw a hissy fit. catch (FileNotFoundException e) { System.out.println("File not found."); //e.printStackTrace(); //Optional, shows more data. } } }