+ Completed every TODOs
+ Implemented the solution for the question asked in lecture + Changed students.txt format to csv
This commit is contained in:
+29
-21
@@ -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;
|
||||
|
||||
@@ -31,34 +33,36 @@ public class Main {
|
||||
|
||||
case 1 -> {
|
||||
|
||||
List<String> data = FileManager.loadStudents("students.txt");
|
||||
List<String> data = FileManager.loadStudents("students.csv");
|
||||
|
||||
// TODO:
|
||||
// Split the file into three parts
|
||||
List<String> part1 = data.subList(0,(data.size()/3 - 1));
|
||||
|
||||
// Split data into 3 parts
|
||||
List<String> part2 = data.subList((data.size()/3 - 1), (2 * data.size()/3 - 1));
|
||||
|
||||
List<String> part1 = null;
|
||||
List<String> part3 = data.subList(((2 * data.size()/3 - 1)), data.size());
|
||||
|
||||
List<String> part2 = null;
|
||||
// Create a thread for each part
|
||||
StudentLoader t1 = new StudentLoader(part1);
|
||||
|
||||
List<String> part3 = null;
|
||||
StudentLoader t2 = new StudentLoader(part2);
|
||||
|
||||
// TODO:
|
||||
StudentLoader t3 = new StudentLoader(part3);
|
||||
|
||||
StudentLoader t1 = null;
|
||||
// Starting all three threads
|
||||
t1.start();
|
||||
t2.start();
|
||||
t3.start();
|
||||
|
||||
StudentLoader t2 = null;
|
||||
|
||||
StudentLoader t3 = null;
|
||||
|
||||
// TODO:
|
||||
|
||||
// start()
|
||||
|
||||
// join()
|
||||
|
||||
// merge results
|
||||
// Waiting for all threads to finish
|
||||
t1.join();
|
||||
t2.join();
|
||||
t3.join();
|
||||
|
||||
// Merging all lists
|
||||
manager.addStudents(t1.getStudents());
|
||||
manager.addStudents(t2.getStudents());
|
||||
manager.addStudents(t3.getStudents());
|
||||
}
|
||||
|
||||
case 2 -> {
|
||||
@@ -66,8 +70,12 @@ public class Main {
|
||||
System.out.print("ID: ");
|
||||
|
||||
int id = scanner.nextInt();
|
||||
|
||||
System.out.println(manager.searchStudent(id));
|
||||
try {
|
||||
System.out.println(manager.searchStudent(id).toString());
|
||||
}
|
||||
catch (NullPointerException e){
|
||||
System.err.println(e.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -40,31 +40,14 @@ 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 (obj == null) { return false; }
|
||||
Student _obj = (Student) obj;
|
||||
return _obj.getId() == this.getId();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -10,34 +10,27 @@ 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 student : students){
|
||||
if (student.getId() == id){
|
||||
return student;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean removeStudent(int id) {
|
||||
|
||||
// TODO:
|
||||
Student student = searchStudent(id);
|
||||
|
||||
// Find student
|
||||
|
||||
// Remove student
|
||||
if (student != null){
|
||||
students.remove(student);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -18,41 +18,30 @@ public class StudentLoader extends Thread {
|
||||
private final ArrayList<Student> students = new ArrayList<>();
|
||||
|
||||
public StudentLoader(List<String> lines) {
|
||||
|
||||
this.lines = lines;
|
||||
}
|
||||
|
||||
// Loads the database from the lines and assign its values to students array and return it
|
||||
@Override
|
||||
public void run() {
|
||||
|
||||
// TODO:
|
||||
|
||||
/*
|
||||
Example line:
|
||||
|
||||
1001,Aryan,Computer Science,3.7
|
||||
*/
|
||||
|
||||
for (String line : lines) {
|
||||
String[] values = line.split(",");
|
||||
|
||||
// TODO:
|
||||
// split line
|
||||
|
||||
// TODO:
|
||||
// read values
|
||||
|
||||
// TODO:
|
||||
// create Student
|
||||
|
||||
// TODO:
|
||||
// add to students list
|
||||
int id = Integer.parseInt(values[0]);
|
||||
String name = values[1];
|
||||
String major = values[2];
|
||||
double gpa = Double.parseDouble(values[3]);
|
||||
|
||||
students.add(new Student(
|
||||
id,
|
||||
name,
|
||||
major,
|
||||
gpa
|
||||
));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public ArrayList<Student> getStudents() {
|
||||
|
||||
return students;
|
||||
}
|
||||
|
||||
|
||||
@@ -17,15 +17,10 @@ public class FileManager {
|
||||
String line;
|
||||
|
||||
while ((line = reader.readLine()) != null) {
|
||||
|
||||
// TODO:
|
||||
|
||||
// Add line to list
|
||||
|
||||
lines.add(line);
|
||||
}
|
||||
|
||||
reader.close();
|
||||
|
||||
return lines;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,10 +1,41 @@
|
||||
package lecture.multithreading.timeout;
|
||||
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
// TODO
|
||||
|
||||
Thread th;
|
||||
Thread th2;
|
||||
|
||||
th2 = new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
|
||||
String name;
|
||||
name = scanner.nextLine();
|
||||
System.out.println("Hello "+ name);
|
||||
System.exit(0);
|
||||
}
|
||||
});
|
||||
th = new Thread(new Runnable(){
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
Thread.sleep(10000);
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
System.out.println("timeout!");
|
||||
System.exit(0);
|
||||
}
|
||||
});
|
||||
|
||||
th.start();
|
||||
th2.start();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user