-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOperation.java
234 lines (191 loc) · 7.91 KB
/
Operation.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package code.OSProject;
import java.io.*;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Operation extends PCB{
private Map<String, Double> variables= new HashMap<>();
public Operation(int Pid, int burstTime) {
super(Pid, burstTime);
}
public void assign(String main,String variable1,String variable2, String operation) {
switch (operation) {
case "input":
readInput(main);
break;
case "writeFile":
writeFile(main, variable1);
break;
case "readFile":
double readF = readFile(variable1);
variables.put(main, readF);
break;
case "print":
print(main);
break;
case "add":
double addResult = getVariableValue(variable1) + getVariableValue(variable2);
variables.put(main, addResult);
break;
case "subtract":
double subResult = getVariableValue(variable1) - getVariableValue(variable2);
variables.put(main, subResult);
break;
case "multiply":
double mulResult = getVariableValue(variable1) * getVariableValue(variable2);
variables.put(main, mulResult);
break;
case "divide":
double divisor = getVariableValue(variable2);
if (divisor != 0) {
double divResult = getVariableValue(variable1) / divisor;
variables.put(main, divResult);
} else {
System.out.println("Error: Division by zero");
}
break;
}
}
// Overloaded assign method for cases where no variables are involved
public void assign(String main, String operation) {
assign(main, "", "", operation);
}
public void assign(String main, String var1, String op) {
assign(main,var1,"", op);
}
public static void readTxt(Operation operation, String filename) {
try (BufferedReader reader = new BufferedReader(new FileReader(getResourceFile(filename)))) {
String line;
while ((line = reader.readLine()) != null) {
String[] tokens = line.split(" ");
if (tokens.length == 2) { // print cases
String operationType = tokens[0];
String main = tokens[1];
operation.assign(main, operationType);
}
else if(tokens.length == 5) { // arithmetic cases
String main = tokens[1];
String operationType2 = tokens[2];
String var1 = tokens[3];
String var2 = tokens[4];
operation.assign(main,var1,var2,operationType2);
}
else if(tokens.length == 3) { // writefile we assign+input
String op = tokens[0];
if(op.equals("assign")) { // assign+input
String main = tokens[1];
String opType = tokens[2];
operation.assign(main, opType);
}else { //writeFile
String main = tokens[1];
String var1 = tokens[2];
operation.assign(main,var1, op);
}
}
else if(tokens.length == 4) {
String main = tokens[1];
String op = tokens[2];
String var1 = tokens[3];
operation.assign(main,var1, op);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
private double readFile(String filename) {
File file = new File(System.getProperty("user.dir")+ File.separator +"bin", filename+".txt");
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
String line = reader.readLine();
return Double.parseDouble(line.trim());
} catch (IOException | NumberFormatException e) {
System.out.println("Error reading file: " + filename);
e.printStackTrace();
}
return 0.0;
}
private static File getResourceFile(String filename) {
ClassLoader classLoader = Operation.class.getClassLoader();
URL resourceUrl = classLoader.getResource(filename);
if (resourceUrl != null) {
try {
String filePath = resourceUrl.toURI().getPath();
if (filename.length() == 1) {
filePath = new File(".").getAbsolutePath() + File.separator + filename;
}
return new File(filePath);
} catch (URISyntaxException e) {
e.printStackTrace();
}
} else {
System.out.println("Error: Resource not found - " + filename);
}
return null;
}
public void writeFile(String file1, String file2) {
try {
File outputFile1 = new File("bin"+ File.separator + file1 + ".txt");
System.out.println("Writing to file: " + outputFile1.getAbsolutePath());
try (BufferedWriter writer = new BufferedWriter(new FileWriter(outputFile1))) {
writer.write(getVariableValue(file1) + "");
System.out.println("Contents written to file: " + file1);
} catch (IOException e) {
System.out.println("Error writing to file: " + file1);
e.printStackTrace();
}
File outputFile2 = new File("bin"+ File.separator + file2 + ".txt");
System.out.println("Writing to file: " + outputFile2.getAbsolutePath());
try (BufferedWriter writer = new BufferedWriter(new FileWriter(outputFile2))) {
writer.write(getVariableValue(file2) + "");
System.out.println("Contents written to file: " + file2);
} catch (IOException e) {
System.out.println("Error writing to file: " + file2);
e.printStackTrace();
}
} catch (Exception e) {
System.out.println("Exception: " + e.getMessage());
e.printStackTrace();
}
}
public double getVariableValue(String variable) {
return variables.getOrDefault(variable, 0.0);
}
public void readInput(String variable) {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
try {
System.out.print("Enter value for " + variable + ": ");
double value = Integer.parseInt(reader.readLine());
variables.put(variable, value);
} catch (IOException | NumberFormatException e) {
System.out.println("Error reading input");
}
}
public void print(String varname) {
System.out.println(varname + ": " + getVariableValue(varname));
}
public static void useRoundRobin(List<PCB> opList) {
RoundRobinScheduler.roundRobin(opList, 2);
}
public static void useSJF(List<PCB> opList) {
ShortestJobFirst.shortestJobFirst(opList);
}
public static void main(String[] args) {
Operation operation1 = new Operation(1, 4);
readTxt(operation1, "Program_1.txt");
Operation operation2 = new Operation(2, 2);
readTxt(operation2, "Program_2.txt");
Operation operation3 = new Operation(3, 7);
readTxt(operation3, "Program_3.txt");
List<PCB> li = new ArrayList<>();
li.add(operation1);
li.add(operation2);
li.add(operation3);
System.out.println("---------------------------");
// useRoundRobin(li);
System.out.println("---------------------------");
useSJF(li);
}
}