Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
66dd9c8db5 |
Generated
+10
@@ -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/
|
||||||
Generated
+13
@@ -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>
|
||||||
Generated
+7
@@ -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>
|
||||||
Generated
+20
@@ -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>
|
||||||
Generated
+12
@@ -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
@@ -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
@@ -12,7 +12,7 @@ public class Main {
|
|||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
|
|
||||||
Scanner scanner = new Scanner(System.in);
|
Scanner scanner = new Scanner(System.in);
|
||||||
|
// ساخت شیء مدیریت دانشجویان
|
||||||
StudentManager manager = new StudentManager();
|
StudentManager manager = new StudentManager();
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
@@ -30,70 +30,67 @@ public class Main {
|
|||||||
switch (choice) {
|
switch (choice) {
|
||||||
|
|
||||||
case 1 -> {
|
case 1 -> {
|
||||||
|
// برای جلوگیری از تکرار دادهها، مدیر قبلی را با یک ابجکت جدید جایگزین میکنیم
|
||||||
|
manager = new StudentManager();
|
||||||
|
|
||||||
|
// خواندن خطوط فایل
|
||||||
List<String> data = FileManager.loadStudents("students.txt");
|
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:
|
// ادغام نتایج در لیست اصلی
|
||||||
|
manager.addStudents(t1.getStudents());
|
||||||
StudentLoader t1 = null;
|
manager.addStudents(t2.getStudents());
|
||||||
|
manager.addStudents(t3.getStudents());
|
||||||
StudentLoader t2 = null;
|
|
||||||
|
|
||||||
StudentLoader t3 = null;
|
|
||||||
|
|
||||||
// TODO:
|
|
||||||
|
|
||||||
// start()
|
|
||||||
|
|
||||||
// join()
|
|
||||||
|
|
||||||
// merge results
|
|
||||||
|
|
||||||
|
System.out.println("Students loaded successfully.");
|
||||||
}
|
}
|
||||||
|
|
||||||
case 2 -> {
|
case 2 -> {
|
||||||
|
|
||||||
System.out.print("ID: ");
|
System.out.print("ID: ");
|
||||||
|
|
||||||
int id = scanner.nextInt();
|
int id = scanner.nextInt();
|
||||||
|
// جستجوی دانشجو
|
||||||
System.out.println(manager.searchStudent(id));
|
System.out.println(manager.searchStudent(id));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
case 3 -> {
|
case 3 -> {
|
||||||
|
|
||||||
System.out.print("ID: ");
|
System.out.print("ID: ");
|
||||||
|
|
||||||
int id = scanner.nextInt();
|
int id = scanner.nextInt();
|
||||||
|
// حذف دانشجو و چاپ نتیجه
|
||||||
boolean removed = manager.removeStudent(id);
|
boolean removed = manager.removeStudent(id);
|
||||||
|
|
||||||
System.out.println(removed);
|
System.out.println(removed);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
case 4 ->
|
case 4 ->
|
||||||
|
// چاپ همه دانشجویان
|
||||||
manager.printStudents();
|
manager.printStudents();
|
||||||
|
|
||||||
case 5 -> {
|
case 5 -> {
|
||||||
|
// خروج از برنامه
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -41,30 +41,22 @@ public class Student {
|
|||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
|
|
||||||
// TODO:
|
// بازگرداندن هش سفارشی طبق فرمول خواسته شده
|
||||||
// Return custom hash
|
return this.id % 97;
|
||||||
|
|
||||||
// Example:
|
|
||||||
// return id % 97;
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object obj) {
|
public boolean equals(Object obj) {
|
||||||
|
|
||||||
// TODO:
|
// بررسی یکی بودن رفرنس یا نال بودن ابجکت ورودی
|
||||||
|
if (this == obj) return true;
|
||||||
|
if (obj == null || getClass() != obj.getClass()) return false;
|
||||||
|
|
||||||
// Step 1:
|
// تبدیل ابجکت ورودی به کلاس Student
|
||||||
// Check null
|
Student student = (Student) obj;
|
||||||
|
|
||||||
// Step 2:
|
// مقایسه IDها برای تعیین برابری
|
||||||
// Convert obj to Student
|
return this.id == student.id;
|
||||||
|
|
||||||
// Step 3:
|
|
||||||
// Compare IDs
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -1,44 +1,42 @@
|
|||||||
package dev.service;
|
package dev.service;
|
||||||
|
|
||||||
import dev.model.Student;
|
import dev.model.Student;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
|
||||||
public class StudentManager {
|
public class StudentManager {
|
||||||
|
|
||||||
private final ArrayList<Student> students = new ArrayList<>();
|
private final ArrayList<Student> students = new ArrayList<>();
|
||||||
|
|
||||||
public void addStudents(ArrayList<Student> newStudents) {
|
public void addStudents(ArrayList<Student> newStudents) {
|
||||||
|
|
||||||
// TODO:
|
// ادغام لیست دانشجویان جدید با لیست اصلی
|
||||||
|
if (newStudents != null) {
|
||||||
// Merge students
|
students.addAll(newStudents);
|
||||||
|
}
|
||||||
// Hint:
|
|
||||||
// addAll()
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Student searchStudent(int id) {
|
public Student searchStudent(int id) {
|
||||||
|
|
||||||
// TODO:
|
// چرخیدن بین تک تک دانشجویان لیست
|
||||||
|
for (Student s : students) {
|
||||||
// Loop through students
|
// مقایسه شناسهها برای پیدا کردن دانشجوی مورد نظر
|
||||||
|
if (s.getId() == id) {
|
||||||
// Compare IDs
|
return s;
|
||||||
|
}
|
||||||
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean removeStudent(int id) {
|
public boolean removeStudent(int id) {
|
||||||
|
|
||||||
// TODO:
|
// پیدا کردن موقعیت دانشجو در لیست برای حذف آن
|
||||||
|
for (int i = 0; i < students.size(); i++) {
|
||||||
// Find student
|
if (students.get(i).getId() == id) {
|
||||||
|
// حذف دانشجو از لیست اصلی
|
||||||
// Remove student
|
students.remove(i);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -47,9 +45,6 @@ public class StudentManager {
|
|||||||
for (Student student : students) {
|
for (Student student : students) {
|
||||||
|
|
||||||
System.out.println(student);
|
System.out.println(student);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,59 +1,49 @@
|
|||||||
package dev.thread;
|
package dev.thread;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
import dev.model.Student;
|
import dev.model.Student;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class StudentLoader extends Thread {
|
public class StudentLoader extends Thread {
|
||||||
|
|
||||||
// Assigned file lines
|
// خطوط اختصاص داده شده از فایل به این ترد
|
||||||
|
|
||||||
private final List<String> lines;
|
private final List<String> lines;
|
||||||
|
|
||||||
// Local student storage
|
// محل ذخیرهسازی محلی دانشجویان برای همین ترد
|
||||||
|
|
||||||
private final ArrayList<Student> students = new ArrayList<>();
|
private final ArrayList<Student> students = new ArrayList<>();
|
||||||
|
|
||||||
public StudentLoader(List<String> lines) {
|
public StudentLoader(List<String> lines) {
|
||||||
|
|
||||||
this.lines = lines;
|
this.lines = lines;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
|
|
||||||
// TODO:
|
|
||||||
|
|
||||||
/*
|
|
||||||
Example line:
|
|
||||||
|
|
||||||
1001,Aryan,Computer Science,3.7
|
|
||||||
*/
|
|
||||||
|
|
||||||
for (String line : lines) {
|
for (String line : lines) {
|
||||||
|
|
||||||
// TODO:
|
// اگر خط خالی بود از آن رد میشویم
|
||||||
// split line
|
if (line.trim().isEmpty()) {
|
||||||
|
continue;
|
||||||
// TODO:
|
|
||||||
// read values
|
|
||||||
|
|
||||||
// TODO:
|
|
||||||
// create Student
|
|
||||||
|
|
||||||
// TODO:
|
|
||||||
// add to students list
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// قطعهبندی خط بر اساس ویرگول
|
||||||
|
String[] tokens = line.split(",");
|
||||||
|
|
||||||
|
// خواندن مقادیر از آرایه توکنها
|
||||||
|
int id = Integer.parseInt(tokens[0].trim());
|
||||||
|
String name = tokens[1].trim();
|
||||||
|
String major = tokens[2].trim();
|
||||||
|
double gpa = Double.parseDouble(tokens[3].trim());
|
||||||
|
|
||||||
|
// ساختن شیء دانشجو
|
||||||
|
Student student = new Student(id, name, major, gpa);
|
||||||
|
|
||||||
|
// اضافه کردن به لیست محلی همین ترد
|
||||||
|
students.add(student);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public ArrayList<Student> getStudents() {
|
public ArrayList<Student> getStudents() {
|
||||||
|
|
||||||
return students;
|
return students;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -2,7 +2,6 @@ package dev.utils;
|
|||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.FileReader;
|
import java.io.FileReader;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@@ -18,15 +17,14 @@ public class FileManager {
|
|||||||
|
|
||||||
while ((line = reader.readLine()) != null) {
|
while ((line = reader.readLine()) != null) {
|
||||||
|
|
||||||
// TODO:
|
// خطوط خوانده شده را به لیست اضافه میکنیم
|
||||||
|
if (!line.trim().isEmpty()) {
|
||||||
// Add line to list
|
lines.add(line);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
reader.close();
|
reader.close();
|
||||||
|
|
||||||
return lines;
|
return lines;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user