From 425aee20cadebb486677ae8cb0ef8986760fa06e Mon Sep 17 00:00:00 2001 From: Zach Date: Tue, 28 Sep 2021 17:11:40 +1000 Subject: [PATCH] Pre-thread change --- Code/P1.java | 135 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 133 insertions(+), 2 deletions(-) diff --git a/Code/P1.java b/Code/P1.java index 77dfb5e..e9fefa8 100644 --- a/Code/P1.java +++ b/Code/P1.java @@ -1,4 +1,5 @@ import java.util.*; +import java.util.concurrent.Semaphore; import java.io.*; public class P1 { @@ -9,7 +10,9 @@ public class P1 { * Author: Zach S-B * Student Number: c3262201 */ - + + LinkedList WARs = new LinkedList(); + Semaphore intersection = new Semaphore(1, true); /** * Main. Runs the program */ @@ -20,7 +23,135 @@ public class P1 { 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