WS 07
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
package dev;
|
||||
|
||||
import dev.model.Student;
|
||||
import dev.service.StudentManager;
|
||||
import dev.thread.StudentLoader;
|
||||
import dev.utils.FileManager;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
|
||||
@@ -37,28 +39,56 @@ public class Main {
|
||||
|
||||
// Split data into 3 parts
|
||||
|
||||
List<String> part1 = null;
|
||||
int sp1;
|
||||
switch (data.size() % 3) {
|
||||
case 1: {
|
||||
sp1 = (data.size()-1)/3;
|
||||
break;
|
||||
}
|
||||
case 2: {
|
||||
sp1 = (data.size()-2)/3;
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
sp1 = data.size()/3;
|
||||
break;
|
||||
}
|
||||
}
|
||||
int sp2 = sp1 * 2;
|
||||
|
||||
List<String> part2 = null;
|
||||
List<String> part1 = data.subList(0, sp1);
|
||||
|
||||
List<String> part3 = null;
|
||||
List<String> part2 = data.subList(sp1, sp2);
|
||||
|
||||
List<String> part3 = data.subList(sp2, data.size());
|
||||
|
||||
// TODO:
|
||||
|
||||
StudentLoader t1 = null;
|
||||
StudentLoader t1 = new StudentLoader(part1);
|
||||
|
||||
StudentLoader t2 = null;
|
||||
StudentLoader t2 = new StudentLoader(part2);
|
||||
|
||||
StudentLoader t3 = null;
|
||||
StudentLoader t3 = new StudentLoader(part3);
|
||||
|
||||
// TODO:
|
||||
|
||||
// start()
|
||||
|
||||
t3.start();
|
||||
t2.start();
|
||||
t1.start();
|
||||
|
||||
// join()
|
||||
|
||||
t3.join();
|
||||
t2.join();
|
||||
t1.join();
|
||||
|
||||
// merge results
|
||||
|
||||
manager.addStudents(t1.getStudents());
|
||||
manager.addStudents(t2.getStudents());
|
||||
manager.addStudents(t3.getStudents());
|
||||
}
|
||||
|
||||
case 2 -> {
|
||||
@@ -79,7 +109,12 @@ public class Main {
|
||||
|
||||
boolean removed = manager.removeStudent(id);
|
||||
|
||||
System.out.println(removed);
|
||||
String res = "The student with id: " + id;
|
||||
|
||||
if (removed) res += "is removed successfully. ";
|
||||
else res += "is not found. ";
|
||||
|
||||
System.out.println(res);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -45,9 +45,7 @@ public class Student {
|
||||
// Return custom hash
|
||||
|
||||
// Example:
|
||||
// return id % 97;
|
||||
|
||||
return 0;
|
||||
return id % 97;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -64,7 +62,11 @@ public class Student {
|
||||
// Step 3:
|
||||
// Compare IDs
|
||||
|
||||
return false;
|
||||
if (obj == this) return true;
|
||||
if (obj == null || obj.getClass() != getClass()) return false;
|
||||
|
||||
Student student = (Student) obj;
|
||||
return student.getId() == id;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -2,6 +2,7 @@ package dev.service;
|
||||
|
||||
import dev.model.Student;
|
||||
|
||||
import java.sql.Struct;
|
||||
import java.util.ArrayList;
|
||||
|
||||
|
||||
@@ -18,6 +19,7 @@ public class StudentManager {
|
||||
// Hint:
|
||||
// addAll()
|
||||
|
||||
students.addAll(newStudents);
|
||||
}
|
||||
|
||||
public Student searchStudent(int id) {
|
||||
@@ -28,6 +30,12 @@ public class StudentManager {
|
||||
|
||||
// Compare IDs
|
||||
|
||||
for (Student student : students) {
|
||||
if (student.getId() == id) {
|
||||
return student;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -39,6 +47,13 @@ public class StudentManager {
|
||||
|
||||
// Remove student
|
||||
|
||||
for (Student student : students) {
|
||||
if (student.getId() == id) {
|
||||
students.remove(student);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -47,6 +47,21 @@ public class StudentLoader extends Thread {
|
||||
// TODO:
|
||||
// add to students list
|
||||
|
||||
String[] data = line.split(",");
|
||||
|
||||
try {
|
||||
int id = Integer.parseInt(data[0]);
|
||||
String name = data[1];
|
||||
String major = data[2];
|
||||
double gpa = Double.parseDouble(data[3]);
|
||||
|
||||
Student student = new Student(id, name, major, gpa);
|
||||
|
||||
students.add(student);
|
||||
}
|
||||
catch (Exception e) {
|
||||
//...
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -22,6 +22,8 @@ public class FileManager {
|
||||
|
||||
// Add line to list
|
||||
|
||||
lines.add(line);
|
||||
|
||||
}
|
||||
|
||||
reader.close();
|
||||
|
||||
Reference in New Issue
Block a user