172 lines
5.1 KiB
Java
172 lines
5.1 KiB
Java
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<P1_War> WARs = new LinkedList<P1_War>(); //Holds the WARs
|
|
LinkedList<Thread> threads = new LinkedList<Thread>(); //For holding threads
|
|
Semaphore intersection = new Semaphore(1, true);
|
|
|
|
int[] tracks = new int[2];
|
|
|
|
|
|
/**
|
|
* Main. Runs the program
|
|
*/
|
|
public static void main(String[] args){
|
|
P1 system = new P1();
|
|
system.run();
|
|
}
|
|
|
|
public void run()
|
|
{
|
|
tracks[0]=0;
|
|
tracks[1]=0;
|
|
|
|
readFile("../P1-1in.txt");
|
|
//Create thread for each war
|
|
for(int i = 0; i < WARs.size(); i++)
|
|
{
|
|
threads.add(new Thread(WARs.get(i)));
|
|
threads.get(i).start();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 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, tracks));
|
|
//WARs.add(new Thread(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, tracks));
|
|
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, tracks));
|
|
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, tracks));
|
|
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);
|
|
}
|
|
} |