-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMain.java
125 lines (90 loc) · 3.7 KB
/
Main.java
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import java.util.InputMismatchException;
import java.util.Scanner;
import java.util.concurrent.atomic.AtomicInteger;
public class Main{
static int numOfAssistants = 1;
static int numOfCustomers = 1;
static int tickLength = 1000;
static int daysToRun = 10;
public static void main(String[] args){
//Takes in User input for number of assistants and customers and for tick length with Error Handling.
Scanner scanner = new Scanner(System.in);
boolean validInput = false;
while(!validInput){
try{
System.out.print("Number of Assistants: ");
numOfAssistants = scanner.nextInt();
validInput = true;
}catch (InputMismatchException e) {
System.out.println("Invalid input! Please enter an integer.");
scanner.next(); // Consume the invalid input
}
}
validInput = false;
while(!validInput){
try{
System.out.print("Number of Customers: ");
numOfCustomers = scanner.nextInt();
validInput = true;
}catch (InputMismatchException e) {
System.out.println("Invalid input! Please enter an integer.");
scanner.next(); // Consume the invalid input
}
}
validInput = false;
while(!validInput){
try{
System.out.print("Tick Length in Milliseconds: ");
tickLength = scanner.nextInt();
validInput = true;
}catch (InputMismatchException e){
System.out.println("Invalid input! Please enter an integer.");
scanner.next();
}
}
validInput = false;
while(!validInput){
try{
System.out.print("Days to Run: ");
daysToRun = scanner.nextInt();
validInput = true;
}catch (InputMismatchException e){
System.out.println("Invalid input! Please enter an integer.");
scanner.next();
}
}
scanner.close();
//Using this to syncronize ticks between threads
AtomicInteger tick = new AtomicInteger(0);
//Create Thrift Store Object
ThriftStore thriftStore = new ThriftStore();
//Create Assistant Threads
Thread[] assistants = new Thread[numOfAssistants];
for (int i = 0; i<numOfAssistants; i++){
assistants[i] = new Thread(new Assistant(thriftStore, tick,daysToRun));
}
//Create Customer Threads
Thread[] customers = new Thread[numOfCustomers];
for (int i = 0; i<numOfCustomers; i++){
customers[i] = new Thread(new Customer(thriftStore, tick));
}
//Create a Deliverer (Only need one for Project Specifications however using the same methods as Customers and Assistants can create multiple)
Deliverer deliverer = new Deliverer(thriftStore, tick);
Thread delivererThread = new Thread(deliverer);
//Start all the assistants
for (Thread thread : assistants) {
thread.start();
}
//Start all the Customers
for (Thread thread : customers) {
thread.start();
}
//Start the deliverer
delivererThread.start();
//Main while loop that will continue running forever since thrift store is open forever.
while(true){
//ThriftStore method to simulate one day
thriftStore.thriftStoreDay(tick,tickLength,daysToRun);
}
}
}