dilan elif basboga commited on
Commit
baf4622
·
1 Parent(s): 5aed724

Initial commit

Browse files
.gitignore ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ### IntelliJ IDEA ###
2
+ out/
3
+ !**/src/main/**/out/
4
+ !**/src/test/**/out/
5
+
6
+ ### Eclipse ###
7
+ .apt_generated
8
+ .classpath
9
+ .factorypath
10
+ .project
11
+ .settings
12
+ .springBeans
13
+ .sts4-cache
14
+ bin/
15
+ !**/src/main/**/bin/
16
+ !**/src/test/**/bin/
17
+
18
+ ### NetBeans ###
19
+ /nbproject/private/
20
+ /nbbuild/
21
+ /dist/
22
+ /nbdist/
23
+ /.nb-gradle/
24
+
25
+ ### VS Code ###
26
+ .vscode/
27
+
28
+ ### Mac OS ###
29
+ .DS_Store
.idea/.gitignore ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ # Default ignored files
2
+ /shelf/
3
+ /workspace.xml
4
+ # Environment-dependent path to Maven home directory
5
+ /mavenHomeManager.xml
.idea/libraries/lib.xml ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <component name="libraryTable">
2
+ <library name="lib">
3
+ <CLASSES>
4
+ <root url="file://$USER_HOME$/Downloads/javafx-sdk-24.0.1/lib" />
5
+ </CLASSES>
6
+ <JAVADOC />
7
+ <NATIVE>
8
+ <root url="file://$USER_HOME$/Downloads/javafx-sdk-24.0.1/lib" />
9
+ </NATIVE>
10
+ <SOURCES />
11
+ <jarDirectory url="file://$USER_HOME$/Downloads/javafx-sdk-24.0.1/lib" recursive="false" />
12
+ </library>
13
+ </component>
.idea/misc.xml ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="ProjectRootManager" version="2" languageLevel="JDK_24" default="true" project-jdk-name="openjdk-24" project-jdk-type="JavaSDK">
4
+ <output url="file://$PROJECT_DIR$/out" />
5
+ </component>
6
+ </project>
.idea/modules.xml ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="ProjectModuleManager">
4
+ <modules>
5
+ <module fileurl="file://$PROJECT_DIR$/project.iml" filepath="$PROJECT_DIR$/project.iml" />
6
+ </modules>
7
+ </component>
8
+ </project>
.idea/vcs.xml ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="VcsDirectoryMappings">
4
+ <mapping directory="$PROJECT_DIR$" vcs="Git" />
5
+ </component>
6
+ </project>
config.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ randomParcelPerTick=2
2
+ interval=3
3
+ Istanbul
4
+ Ankara
5
+ Izmir
6
+ Bursa
route_config.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ Istanbul
2
+ Ankara
3
+ Izmir
4
+ Bursa
smart_package_sortingRouting_simulation.iml ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <module type="JAVA_MODULE" version="4">
3
+ <component name="NewModuleRootManager" inherit-compiler-output="true">
4
+ <exclude-output />
5
+ <content url="file://$MODULE_DIR$">
6
+ <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7
+ </content>
8
+ <orderEntry type="inheritedJdk" />
9
+ <orderEntry type="sourceFolder" forTests="false" />
10
+ <orderEntry type="library" name="lib" level="project" />
11
+ </component>
12
+ </module>
src/src/ParcelSortX.java ADDED
@@ -0,0 +1,409 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package src;
2
+
3
+ import java.io.*;
4
+ import java.util.*;
5
+
6
+ // ------------------ PARCEL CLASS ------------------
7
+ class Parcel {
8
+ String id;
9
+ String destination;
10
+ int priority;
11
+ int arrivalTick;
12
+ int returnCount;
13
+ String status;
14
+
15
+ public Parcel(String id, String destination, int priority, int arrivalTick) {
16
+ this.id = id;
17
+ this.destination = destination;
18
+ this.priority = priority;
19
+ this.arrivalTick = arrivalTick;
20
+ this.returnCount = 0;
21
+ this.status = "Pending";
22
+ }
23
+
24
+ @Override
25
+ public String toString() {
26
+ return "[" + id + ", Dest: " + destination + ", Priority: " + priority +
27
+ ", ArrivalTick: " + arrivalTick + ", ReturnCount: " + returnCount +
28
+ ", Status: " + status + "]";
29
+ }
30
+ }
31
+
32
+ // ------------------ QUEUE ------------------
33
+ class ParcelQueue {
34
+ private Queue<Parcel> queue;
35
+
36
+ public ParcelQueue() {
37
+ this.queue = new LinkedList<>();
38
+ }
39
+
40
+ public void enqueue(Parcel p) {
41
+ queue.add(p);
42
+ }
43
+
44
+ public Parcel dequeue() {
45
+ return queue.poll();
46
+ }
47
+
48
+ public boolean isEmpty() {
49
+ return queue.isEmpty();
50
+ }
51
+ }
52
+
53
+ // ------------------ STACK ------------------
54
+ class ParcelStack {
55
+ private List<Parcel> stack = new ArrayList<>();
56
+
57
+ public void push(Parcel p) {
58
+ stack.add(p);
59
+ }
60
+
61
+ public Parcel pop() {
62
+ if (isEmpty()) return null;
63
+ return stack.remove(stack.size() - 1);
64
+ }
65
+
66
+ public boolean isEmpty() {
67
+ return stack.isEmpty();
68
+ }
69
+ }
70
+
71
+ // ------------------ HASH TABLE ------------------
72
+ class HashEntry {
73
+ String key;
74
+ Parcel value;
75
+ HashEntry next;
76
+
77
+ public HashEntry(String key, Parcel value) {
78
+ this.key = key;
79
+ this.value = value;
80
+ }
81
+ }
82
+
83
+ class ManualHashTable {
84
+ private HashEntry[] buckets;
85
+ private int capacity;
86
+
87
+ public ManualHashTable(int capacity) {
88
+ this.capacity = capacity;
89
+ this.buckets = new HashEntry[capacity];
90
+ }
91
+
92
+ private int getBucketIndex(String key) {
93
+ return Math.abs(key.hashCode()) % capacity;
94
+ }
95
+
96
+ public void put(String key, Parcel value) {
97
+ int index = getBucketIndex(key);
98
+ HashEntry head = buckets[index];
99
+ while (head != null) {
100
+ if (head.key.equals(key)) {
101
+ head.value = value;
102
+ return;
103
+ }
104
+ head = head.next;
105
+ }
106
+ HashEntry newEntry = new HashEntry(key, value);
107
+ newEntry.next = buckets[index];
108
+ buckets[index] = newEntry;
109
+ }
110
+
111
+ public Parcel get(String key) {
112
+ int index = getBucketIndex(key);
113
+ HashEntry head = buckets[index];
114
+ while (head != null) {
115
+ if (head.key.equals(key)) return head.value;
116
+ head = head.next;
117
+ }
118
+ return null;
119
+ }
120
+
121
+ public void printAll() {
122
+ for (HashEntry head : buckets) {
123
+ while (head != null) {
124
+ System.out.println(" → " + head.value);
125
+ head = head.next;
126
+ }
127
+ }
128
+ }
129
+ }
130
+
131
+ class ParcelTracker {
132
+ ManualHashTable table = new ManualHashTable(32);
133
+
134
+ public void track(Parcel p) {
135
+ table.put(p.id, p);
136
+ log("Tracking parcel: " + p);
137
+ }
138
+
139
+ public Parcel get(String id) {
140
+ return table.get(id);
141
+ }
142
+
143
+ public void printAllTracked() {
144
+ table.printAll();
145
+ }
146
+
147
+ private void log(String msg) {
148
+ System.out.println("[Tracker Log] " + msg);
149
+ }
150
+ }
151
+
152
+ // ------------------ CITY BST ------------------
153
+ class CityBST {
154
+ private class TreeNode {
155
+ String cityName;
156
+ List<Parcel> parcels;
157
+ TreeNode left, right;
158
+
159
+ TreeNode(String cityName) {
160
+ this.cityName = cityName;
161
+ this.parcels = new ArrayList<>();
162
+ left = right = null;
163
+ }
164
+ }
165
+
166
+ private TreeNode root;
167
+
168
+ public CityBST() {
169
+ root = null;
170
+ }
171
+
172
+ // Paketi BST'ye ekler
173
+ public void addParcel(Parcel p) {
174
+ root = insert(root, p);
175
+ p.status = "Dispatched";
176
+ }
177
+
178
+ private TreeNode insert(TreeNode node, Parcel p) {
179
+ if (node == null) {
180
+ TreeNode newNode = new TreeNode(p.destination);
181
+ newNode.parcels.add(p);
182
+ return newNode;
183
+ }
184
+
185
+ int cmp = p.destination.compareToIgnoreCase(node.cityName);
186
+ if (cmp < 0) {
187
+ node.left = insert(node.left, p);
188
+ } else if (cmp > 0) {
189
+ node.right = insert(node.right, p);
190
+ } else {
191
+ node.parcels.add(p);
192
+ }
193
+ return node;
194
+ }
195
+
196
+ // Verilen şehirdeki paketleri döner (null değil, boş liste bile olabilir)
197
+ public List<Parcel> getParcels(String city) {
198
+ TreeNode node = search(root, city);
199
+ if (node != null) return node.parcels;
200
+ else return new ArrayList<>();
201
+ }
202
+
203
+ private TreeNode search(TreeNode node, String city) {
204
+ if (node == null) return null;
205
+ int cmp = city.compareToIgnoreCase(node.cityName);
206
+ if (cmp == 0) return node;
207
+ else if (cmp < 0) return search(node.left, city);
208
+ else return search(node.right, city);
209
+ }
210
+
211
+ // Ağacı sıralı şekilde yazdırır
212
+ public void printAllParcels() {
213
+ System.out.println("City BST Parcel Distribution:");
214
+ inOrder(root);
215
+ }
216
+
217
+ private void inOrder(TreeNode node) {
218
+ if (node == null) return;
219
+ inOrder(node.left);
220
+ System.out.println("[" + node.cityName + "] (" + node.parcels.size() + " parcels)");
221
+ for (Parcel p : node.parcels) {
222
+ System.out.println(" → " + p);
223
+ }
224
+ inOrder(node.right);
225
+ }
226
+ }
227
+
228
+ // ------------------ ROUTE ------------------
229
+ class RouteNode {
230
+ String location;
231
+ RouteNode next;
232
+
233
+ public RouteNode(String location) {
234
+ this.location = location;
235
+ }
236
+ }
237
+
238
+ class RouteCircularList {
239
+ private RouteNode head = null;
240
+ private RouteNode current = null;
241
+ public int rotationInterval = 3;
242
+
243
+ public void loadFromConfig(String filePath) {
244
+ try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
245
+ String line;
246
+ RouteNode prev = null;
247
+ while ((line = br.readLine()) != null) {
248
+ line = line.trim();
249
+ if (line.startsWith("interval=")) {
250
+ rotationInterval = Integer.parseInt(line.split("=")[1].trim());
251
+ continue;
252
+ }
253
+
254
+ if (line.isEmpty()) continue;
255
+ RouteNode node = new RouteNode(line);
256
+ if (head == null) head = node;
257
+ else prev.next = node;
258
+ prev = node;
259
+ }
260
+ if (prev != null) {
261
+ prev.next = head;
262
+ current = head;
263
+ } else {
264
+ System.out.println("[Route Log] No cities loaded in route.");
265
+ head = null;
266
+ current = null;
267
+ }
268
+ } catch (IOException e) {
269
+ System.out.println("[Route Log] Error reading config: " + e.getMessage());
270
+ head = null;
271
+ current = null;
272
+ }
273
+ }
274
+
275
+ public boolean isLoaded() {
276
+ return head != null && current != null;
277
+ }
278
+
279
+ public void rotateAndLogDeliveries() {
280
+ if (current == null) {
281
+ System.out.println("[Route Log] No route loaded.");
282
+ return;
283
+ }
284
+
285
+ for (int i = 0; i < rotationInterval; i++) {
286
+ System.out.println("[Route Log] Delivering to: " + current.location);
287
+ current = current.next;
288
+ }
289
+
290
+ System.out.println("[Route Log] Active terminal: " + current.location);
291
+ }
292
+
293
+ public String getCurrentLocation() {
294
+ return current != null ? current.location : null;
295
+ }
296
+ }
297
+
298
+ // ------------------ MAIN SIMULATION ------------------
299
+ public class ParcelSortX {
300
+ private static int randomParcelPerTick = 2;
301
+
302
+ private static int totalGenerated = 0;
303
+ private static int totalDispatched = 0;
304
+ private static int totalReturned = 0;
305
+
306
+ private static void log(String msg) {
307
+ System.out.println("[Main Log] " + msg);
308
+ }
309
+
310
+ private static String[] possibleCities = {"Istanbul", "Ankara", "Izmir", "Bursa"};
311
+
312
+ private static Parcel generateRandomParcel(int tick, int idSuffix) {
313
+ Random rand = new Random();
314
+ String id = "P" + tick + String.format("%02d", idSuffix);
315
+ String dest = possibleCities[rand.nextInt(possibleCities.length)];
316
+ int priority = rand.nextInt(3) + 1;
317
+ totalGenerated++;
318
+ return new Parcel(id, dest, priority, tick);
319
+ }
320
+
321
+ public static void loadGlobalConfig(String filePath) {
322
+ try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
323
+ String line;
324
+ while ((line = br.readLine()) != null) {
325
+ if (line.startsWith("randomParcelPerTick=")) {
326
+ randomParcelPerTick = Integer.parseInt(line.split("=")[1].trim());
327
+ }
328
+ }
329
+ } catch (IOException e) {
330
+ System.out.println("[Config Log] Error reading config: " + e.getMessage());
331
+ }
332
+ }
333
+
334
+ public static void main(String[] args) {
335
+
336
+ loadGlobalConfig("config.txt");
337
+
338
+ ParcelQueue incoming = new ParcelQueue();
339
+ ParcelStack sortingStack = new ParcelStack();
340
+ CityBST cityBST = new CityBST();
341
+ ParcelTracker tracker = new ParcelTracker();
342
+ RouteCircularList route = new RouteCircularList();
343
+ route.loadFromConfig("route_config.txt");
344
+
345
+ int maxTick = 10;
346
+
347
+ for (int tick = 1; tick <= maxTick; tick++) {
348
+ log("Tick " + tick + " started");
349
+
350
+ // 1) Yeni rastgele paket üret
351
+ for (int i = 0; i < randomParcelPerTick; i++) {
352
+ Parcel p = generateRandomParcel(tick, i);
353
+ incoming.enqueue(p);
354
+ tracker.track(p);
355
+ log("Generated and queued: " + p);
356
+ }
357
+
358
+ // 2) Gelen kuyruktan stack'e aktar (sıraya göre)
359
+ while (!incoming.isEmpty()) {
360
+ Parcel p = incoming.dequeue();
361
+ sortingStack.push(p);
362
+ log("Moved from queue to stack: " + p);
363
+ }
364
+
365
+ // 3) Stack'ten paketleri işleyip şehirlere dağıt (BST'ye ekle)
366
+ while (!sortingStack.isEmpty()) {
367
+ Parcel p = sortingStack.pop();
368
+
369
+ // Eğer iade sayısı 3 veya üstü ise paket sistemden atılır
370
+ if (p.returnCount >= 3) {
371
+ log("Discarding parcel due to too many returns: " + p);
372
+ continue;
373
+ }
374
+
375
+ cityBST.addParcel(p);
376
+ totalDispatched++;
377
+ log("Dispatched parcel to BST city: " + p);
378
+ }
379
+
380
+ // 4) Rotasyonu yap ve aktif terminali yazdır
381
+ if (route.isLoaded()) {
382
+ route.rotateAndLogDeliveries();
383
+ } else {
384
+ log("No delivery route loaded.");
385
+ }
386
+
387
+ // 5) Örnek: 1 paket iade edelim (demo amaçlı)
388
+ // Normalde iade mantığı daha kapsamlı olabilir
389
+ if (tick % 4 == 0) { // 4. tickte iade testi
390
+ Parcel someParcel = new Parcel("ReturnedP" + tick, "Ankara", 2, tick);
391
+ someParcel.returnCount++;
392
+ someParcel.status = "Returned";
393
+ sortingStack.push(someParcel);
394
+ totalReturned++;
395
+ log("Demo: Parcel returned and pushed back to stack: " + someParcel);
396
+ }
397
+
398
+ log("Tick " + tick + " ended\n");
399
+ }
400
+
401
+ // Son rapor
402
+ System.out.println("\n===== FINAL REPORT =====");
403
+ System.out.println("Total parcels generated: " + totalGenerated);
404
+ System.out.println("Total parcels dispatched: " + totalDispatched);
405
+ System.out.println("Total parcels returned: " + totalReturned);
406
+ cityBST.printAllParcels();
407
+ tracker.printAllTracked();
408
+ }
409
+ }
src/src/ParcelSortXGUI.java ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ package src;
2
+
3
+ public class ParcelSortXGUI {
4
+ }