Author SHA1 Message Date
AmirAli_Amiri 66dd9c8db5 complete student search engine with multithreading 2026-06-23 16:48:50 +03:30
11 changed files with 149 additions and 108 deletions
+10
View File
@@ -0,0 +1,10 @@
# Default ignored files
/shelf/
/workspace.xml
# Ignored default folder with query files
/queries/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
# Editor-based HTTP Client requests
/httpRequests/
+13
View File
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<annotationProcessing>
<profile name="Maven default annotation processors profile" enabled="true">
<sourceOutputDir name="target/generated-sources/annotations" />
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
<outputRelativeToContentRoot value="true" />
<module name="WS-07-Multithreading-Basics-and-Hashing" />
</profile>
</annotationProcessing>
</component>
</project>
+7
View File
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
</component>
</project>
+20
View File
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RemoteRepositoriesConfiguration">
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Maven Central repository" />
<option name="url" value="https://repo1.maven.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="jboss.community" />
<option name="name" value="JBoss Community repository" />
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
</remote-repository>
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Central Repository" />
<option name="url" value="https://maven.aliyun.com/repository/public" />
</remote-repository>
</component>
</project>
+12
View File
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" project-jdk-name="homebrew-26" project-jdk-type="JavaSDK" />
</project>
Generated
+7
View File
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
+31 -34
View File
@@ -12,7 +12,7 @@ public class Main {
public static void main(String[] args) throws Exception {
Scanner scanner = new Scanner(System.in);
// ساخت شیء مدیریت دانشجویان
StudentManager manager = new StudentManager();
while (true) {
@@ -30,70 +30,67 @@ public class Main {
switch (choice) {
case 1 -> {
// برای جلوگیری از تکرار داده‌ها، مدیر قبلی را با یک ابجکت جدید جایگزین می‌کنیم
manager = new StudentManager();
// خواندن خطوط فایل
List<String> data = FileManager.loadStudents("students.txt");
// TODO:
// تقسیم خطوط به ۳ بخش مساوی
int totalLines = data.size();
int chunkSize = totalLines / 3;
// Split data into 3 parts
List<String> part1 = data.subList(0, chunkSize);
List<String> part2 = data.subList(chunkSize, chunkSize * 2);
List<String> part3 = data.subList(chunkSize * 2, totalLines);
List<String> part1 = null;
// ساختن ۳ ترد مجزا
StudentLoader t1 = new StudentLoader(part1);
StudentLoader t2 = new StudentLoader(part2);
StudentLoader t3 = new StudentLoader(part3);
List<String> part2 = null;
// شروع کار همزمان تردها
t1.start();
t2.start();
t3.start();
List<String> part3 = null;
// انتظار برای پایان کار تردها
t1.join();
t2.join();
t3.join();
// TODO:
StudentLoader t1 = null;
StudentLoader t2 = null;
StudentLoader t3 = null;
// TODO:
// start()
// join()
// merge results
// ادغام نتایج در لیست اصلی
manager.addStudents(t1.getStudents());
manager.addStudents(t2.getStudents());
manager.addStudents(t3.getStudents());
System.out.println("Students loaded successfully.");
}
case 2 -> {
System.out.print("ID: ");
int id = scanner.nextInt();
// جستجوی دانشجو
System.out.println(manager.searchStudent(id));
}
case 3 -> {
System.out.print("ID: ");
int id = scanner.nextInt();
// حذف دانشجو و چاپ نتیجه
boolean removed = manager.removeStudent(id);
System.out.println(removed);
}
case 4 ->
// چاپ همه دانشجویان
manager.printStudents();
case 5 -> {
// خروج از برنامه
return;
}
}
}
}
}
+9 -17
View File
@@ -41,30 +41,22 @@ public class Student {
@Override
public int hashCode() {
// TODO:
// Return custom hash
// Example:
// return id % 97;
return 0;
// بازگرداندن هش سفارشی طبق فرمول خواسته شده
return this.id % 97;
}
@Override
public boolean equals(Object obj) {
// TODO:
// بررسی یکی بودن رفرنس یا نال بودن ابجکت ورودی
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
// Step 1:
// Check null
// تبدیل ابجکت ورودی به کلاس Student
Student student = (Student) obj;
// Step 2:
// Convert obj to Student
// Step 3:
// Compare IDs
return false;
// مقایسه IDها برای تعیین برابری
return this.id == student.id;
}
@Override
+19 -24
View File
@@ -1,44 +1,42 @@
package dev.service;
import dev.model.Student;
import java.util.ArrayList;
public class StudentManager {
private final ArrayList<Student> students = new ArrayList<>();
public void addStudents(ArrayList<Student> newStudents) {
// TODO:
// Merge students
// Hint:
// addAll()
// ادغام لیست دانشجویان جدید با لیست اصلی
if (newStudents != null) {
students.addAll(newStudents);
}
}
public Student searchStudent(int id) {
// TODO:
// Loop through students
// Compare IDs
// چرخیدن بین تک تک دانشجویان لیست
for (Student s : students) {
// مقایسه شناسه‌ها برای پیدا کردن دانشجوی مورد نظر
if (s.getId() == id) {
return s;
}
}
return null;
}
public boolean removeStudent(int id) {
// TODO:
// Find student
// Remove student
// پیدا کردن موقعیت دانشجو در لیست برای حذف آن
for (int i = 0; i < students.size(); i++) {
if (students.get(i).getId() == id) {
// حذف دانشجو از لیست اصلی
students.remove(i);
return true;
}
}
return false;
}
@@ -47,9 +45,6 @@ public class StudentManager {
for (Student student : students) {
System.out.println(student);
}
}
}
+17 -27
View File
@@ -1,59 +1,49 @@
package dev.thread;
import dev.model.Student;
import java.util.ArrayList;
import java.util.List;
public class StudentLoader extends Thread {
// Assigned file lines
// خطوط اختصاص داده شده از فایل به این ترد
private final List<String> lines;
// Local student storage
// محل ذخیره‌سازی محلی دانشجویان برای همین ترد
private final ArrayList<Student> students = new ArrayList<>();
public StudentLoader(List<String> lines) {
this.lines = lines;
}
@Override
public void run() {
// TODO:
/*
Example line:
1001,Aryan,Computer Science,3.7
*/
for (String line : lines) {
// TODO:
// split line
// اگر خط خالی بود از آن رد می‌شویم
if (line.trim().isEmpty()) {
continue;
}
// TODO:
// read values
// قطعه‌بندی خط بر اساس ویرگول
String[] tokens = line.split(",");
// TODO:
// create Student
// خواندن مقادیر از آرایه توکن‌ها
int id = Integer.parseInt(tokens[0].trim());
String name = tokens[1].trim();
String major = tokens[2].trim();
double gpa = Double.parseDouble(tokens[3].trim());
// TODO:
// add to students list
// ساختن شیء دانشجو
Student student = new Student(id, name, major, gpa);
// اضافه کردن به لیست محلی همین ترد
students.add(student);
}
}
public ArrayList<Student> getStudents() {
return students;
}
}
+4 -6
View File
@@ -2,7 +2,6 @@ package dev.utils;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
@@ -18,15 +17,14 @@ public class FileManager {
while ((line = reader.readLine()) != null) {
// TODO:
// Add line to list
// خطوط خوانده شده را به لیست اضافه می‌کنیم
if (!line.trim().isEmpty()) {
lines.add(line);
}
}
reader.close();
return lines;
}
}