Pre-thread change
This commit is contained in:
parent
156548641d
commit
425aee20ca
133
Code/P1.java
133
Code/P1.java
@ -1,4 +1,5 @@
|
||||
import java.util.*;
|
||||
import java.util.concurrent.Semaphore;
|
||||
import java.io.*;
|
||||
|
||||
public class P1 {
|
||||
@ -10,6 +11,8 @@ public class P1 {
|
||||
* Student Number: c3262201
|
||||
*/
|
||||
|
||||
LinkedList<Thread> WARs = new LinkedList<Thread>();
|
||||
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<WARs.size(); i++)
|
||||
{
|
||||
if((WARs.get(i).getTrackNum()==1)&&(WARs.get(i).getAtDock()==false))
|
||||
{
|
||||
storage1++;
|
||||
System.out.print("WAR"+WARs.get(i).getID()+", ");
|
||||
}
|
||||
}
|
||||
System.out.println("Total: " + storage1);
|
||||
|
||||
System.out.print("(W) WARs at Dock 1: ");
|
||||
int dock1 = 0;
|
||||
for (int i = 0; i<WARs.size(); i++)
|
||||
{
|
||||
if((WARs.get(i).getTrackNum()==1)&&(WARs.get(i).getAtDock()==true))
|
||||
{
|
||||
dock1++;
|
||||
System.out.print("WAR"+WARs.get(i).getID()+", ");
|
||||
}
|
||||
}
|
||||
System.out.println("Total: " + dock1);
|
||||
System.out.print("(N) WARs at Storage 2: ");
|
||||
int storage2 = 0;
|
||||
for (int i = 0; i<WARs.size(); i++)
|
||||
{
|
||||
if((WARs.get(i).getTrackNum()==2)&&(WARs.get(i).getAtDock()==false))
|
||||
{
|
||||
storage2++;
|
||||
System.out.print("WAR"+WARs.get(i).getID()+", ");
|
||||
}
|
||||
}
|
||||
System.out.println("Total: " + storage2);
|
||||
|
||||
System.out.print("(S) WARs at Dock 2: ");
|
||||
int dock2 = 0;
|
||||
for (int i = 0; i<WARs.size(); i++)
|
||||
{
|
||||
if((WARs.get(i).getTrackNum()==2)&&(WARs.get(i).getAtDock()==true))
|
||||
{
|
||||
dock2++;
|
||||
System.out.print("WAR"+WARs.get(i).getID()+", ");
|
||||
}
|
||||
}
|
||||
System.out.println("Total: " + dock2);
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user