This commit is contained in:
Reza
2026-05-25 20:28:54 +03:30
parent 83746b88f6
commit bf54c701ab
11 changed files with 104 additions and 97 deletions
+17 -20
View File
@@ -33,32 +33,29 @@ public class Main {
List<String> data = FileManager.loadStudents("students.txt");
// TODO:
int total = data.size();
int partSize = total / 3;
int rem = total % 3;
// Split data into 3 parts
int end1 = partSize + (rem > 0 ? 1 : 0);
int end2 = end1 + partSize + (rem > 1 ? 1 : 0);
List<String> part1 = null;
List<String> p1 = data.subList(0, end1);
List<String> p2 = data.subList(end1, end2);
List<String> p3 = data.subList(end2, total);
List<String> part2 = null;
StudentLoader t1 = new StudentLoader(p1);
StudentLoader t2 = new StudentLoader(p2);
StudentLoader t3 = new StudentLoader(p3);
List<String> part3 = null;
t1.start(); t2.start(); t3.start();
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.");
}
case 2 -> {
+5 -21
View File
@@ -40,31 +40,15 @@ public class Student {
@Override
public int hashCode() {
// TODO:
// Return custom hash
// Example:
// return id % 97;
return 0;
return id % 97;
}
@Override
public boolean equals(Object obj) {
// TODO:
// Step 1:
// Check null
// Step 2:
// Convert obj to Student
// Step 3:
// Compare IDs
return false;
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
Student other = (Student) obj;
return this.id == other.id;
}
@Override
+5 -23
View File
@@ -10,36 +10,18 @@ public class StudentManager {
private final ArrayList<Student> students = new ArrayList<>();
public void addStudents(ArrayList<Student> newStudents) {
// TODO:
// Merge students
// Hint:
// addAll()
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
return false;
return students.removeIf(s -> s.getId() == id);
}
public void printStudents() {
+8 -24
View File
@@ -9,11 +9,11 @@ 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<>();
@@ -24,29 +24,13 @@ public class StudentLoader extends Thread {
@Override
public void run() {
// TODO:
/*
Example line:
1001,Aryan,Computer Science,3.7
*/
for (String line : lines) {
// TODO:
// split line
// TODO:
// read values
// TODO:
// create Student
// TODO:
// add to students list
String[] parts = line.split(",");
int id = Integer.parseInt(parts[0]);
String name = parts[1];
String major = parts[2];
double gpa = Double.parseDouble(parts[3]);
students.add(new Student(id, name, major, gpa));
}
}
+1 -9
View File
@@ -11,22 +11,14 @@ public class FileManager {
public static List<String> loadStudents(String path) throws Exception {
List<String> lines = new ArrayList<>();
BufferedReader reader = new BufferedReader(new FileReader(path));
String line;
while ((line = reader.readLine()) != null) {
// TODO:
// Add line to list
lines.add(line);
}
reader.close();
return lines;
}
}