-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP1.java
More file actions
102 lines (84 loc) · 3.85 KB
/
Copy pathP1.java
File metadata and controls
102 lines (84 loc) · 3.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/* COMP2240 Assignment 2
* File: P1.java
* Author: Nicholas Steuart c3330826
* Date Created: 7/9/24
* Date Last Modified: 27/9/24
* Description: MAIN file. Reads in data from file and outputs Semaphore-controlled concurrent thread execution simulating Problem 1.
*/
// PACKAGES //
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class P1
{
// CLASS VARIABLES //
public static int currentCount = 0, totalCount = 0; //Keeps track of the global current unique MAC ID the file is upto, and total count of MACs starting from a particular destination
public static ArrayList<Mac> macList = new ArrayList<>(); //Stores the MAC Objects read in from file
// MAIN //
public static void main(String[] args) throws Exception
{
File file = new File(args[0]); //Read in terminal argument as a new File Object
// READS IN FROM FILE //
try(Scanner sc = new Scanner(file))
{
Intersection intersection = new Intersection(1); //Instantiate the intersection with only one permit
// CREATE MACS //
createMacs(sc.next(), intersection); //Creates CSR1 starting MACs
createMacs(sc.next(), intersection); //Creates CSR2 starting MACs
createMacs(sc.next(), intersection); //Creates ED1 starting MACs
createMacs(sc.next(), intersection); //Creates ED2 starting MACs
int totalCrossings = Integer.parseInt(sc.next().substring(2)); //Reads in and stores from file the total amount of times a MAC must cross the intersection
sc.close(); //Close Scanner
// START THREAD EXECUTION //
for(Mac mac: macList)
{
mac.setTotalCrossings(totalCrossings);
new Thread(mac).start();
}
}
catch(FileNotFoundException e)
{
System.out.println("File " + args[0] + " was not found."); //File defined in the terminal line argument was invalid and doesn't exist in the directory
}
}
// METHODS //
//PRE-CONDITIONS:
//Parameter String token must be of the form "CSR1=1", "CSR2=2", "ED1=1", or "ED2=1"
//Parameter Intersection intersection must have been instantiated
//POST-CONDITIONS: macList populated by new MAC Objects
public static void createMacs(String token, Intersection intersection)
{
int eqPosition = token.indexOf("="); //Returns the position of the equal sign in the token
totalCount += Integer.parseInt(token.substring(eqPosition + 1, token.length() - 1)); //Increments totalCount by the value after the equals sign
String status = ""; //Stores the status of the current MAC being read in
String destination = ""; //Stores the destination of the current MAC being read in
//Determines how to instantiate the MAC(s) based on the token parsed in
switch (token.substring(0, eqPosition))
{
case "CSR1" -> {
status = "Stock";
destination = "ED1";
}
case "CSR2" -> {
status = "Stock";
destination = "ED2";
}
case "ED1" -> {
status = "Empty";
destination = "CSR1";
}
case "ED2" -> {
status = "Empty";
destination = "CSR2";
}
}
//Adds a new mac to the current Macs list.
//currentCount keeps track of the MAC ID we are upto.
for(int i = currentCount; i < totalCount; i++)
{
macList.add(new Mac("MAC-" + (i + 1), status, destination, intersection));
}
currentCount = totalCount; //Allows us to continue from the next available MAC ID of the next token
}
}