2021-09-28 17:11:40 +10:00

157 lines
4.7 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<Thread> WARs = new LinkedList<Thread>();
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<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);
}
}