import java.util.*; import java.util.concurrent.Semaphore; import java.io.*; public class P1 { /** * COMP2240 Assignment 2 * Date: * Author: Zach S-B * Student Number: c3262201 */ LinkedList WARs = new LinkedList(); Semaphore intersection = new Semaphore(1, true); /** * Main. Runs the program */ public static void main(String[] args){ P1 system = new P1(); system.run(); } public void run() { readFile("../P1-1in.txt"); printState(); } /** * 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 input) { Scanner fileReader; //Scanner to read the file. File dataInput;// = new File(fileNameInput); dataInput = new File(input); //Try and open file. If it works, procede. try { fileReader = new Scanner(dataInput); //Assuming only one line in file. String line = fileReader.nextLine(); int warNum = 1; for (int charNum = 0; charNum < line.length(); charNum++) { if(line.charAt(charNum)=='N') { charNum+=2; int inputID = line.charAt(charNum) - '0'; for(int i = 0; i< inputID; i++) { //WARs.add(new P1_War(warNum, 2, false, intersection)); warNum++; } } else if(line.charAt(charNum)=='S') { charNum+=2; int inputID = line.charAt(charNum) - '0'; for(int i = 0; i< inputID; i++) { WARs.add(new P1_War(warNum, 2, true, intersection)); warNum++; } } else if(line.charAt(charNum)=='E') { charNum+=2; int inputID = line.charAt(charNum) - '0'; for(int i = 0; i< inputID; i++) { WARs.add(new P1_War(warNum, 1, false, intersection)); warNum++; } } else if(line.charAt(charNum)=='W') { charNum+=2; int inputID = line.charAt(charNum) - '0'; for(int i = 0; i< inputID; i++) { WARs.add(new P1_War(warNum, 1, true, intersection)); warNum++; } } } fileReader.close(); } //If the file isn't found, throw a hissy fit. catch (FileNotFoundException e) { System.out.println("File not found."); System.exit(0); //e.printStackTrace(); //Optional, shows more data. } } public void printState() { System.out.print("(E) WARs at Storage 1: "); int storage1 = 0; for (int i = 0; i