+ 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;
|
package dev;
|
||||||
|
|
||||||
|
import dev.model.Student;
|
||||||
import dev.service.StudentManager;
|
import dev.service.StudentManager;
|
||||||
import dev.thread.StudentLoader;
|
import dev.thread.StudentLoader;
|
||||||
import dev.utils.FileManager;
|
import dev.utils.FileManager;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
|
||||||
@@ -31,34 +33,36 @@ public class Main {
|
|||||||
|
|
||||||
case 1 -> {
|
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;
|
// Waiting for all threads to finish
|
||||||
|
t1.join();
|
||||||
StudentLoader t3 = null;
|
t2.join();
|
||||||
|
t3.join();
|
||||||
// TODO:
|
|
||||||
|
|
||||||
// start()
|
|
||||||
|
|
||||||
// join()
|
|
||||||
|
|
||||||
// merge results
|
|
||||||
|
|
||||||
|
// Merging all lists
|
||||||
|
manager.addStudents(t1.getStudents());
|
||||||
|
manager.addStudents(t2.getStudents());
|
||||||
|
manager.addStudents(t3.getStudents());
|
||||||
}
|
}
|
||||||
|
|
||||||
case 2 -> {
|
case 2 -> {
|
||||||
@@ -66,8 +70,12 @@ public class Main {
|
|||||||
System.out.print("ID: ");
|
System.out.print("ID: ");
|
||||||
|
|
||||||
int id = scanner.nextInt();
|
int id = scanner.nextInt();
|
||||||
|
try {
|
||||||
System.out.println(manager.searchStudent(id));
|
System.out.println(manager.searchStudent(id).toString());
|
||||||
|
}
|
||||||
|
catch (NullPointerException e){
|
||||||
|
System.err.println(e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -40,31 +40,14 @@ public class Student {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
|
return id % 97;
|
||||||
// TODO:
|
|
||||||
// Return custom hash
|
|
||||||
|
|
||||||
// Example:
|
|
||||||
// return id % 97;
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object obj) {
|
public boolean equals(Object obj) {
|
||||||
|
if (obj == null) { return false; }
|
||||||
// TODO:
|
Student _obj = (Student) obj;
|
||||||
|
return _obj.getId() == this.getId();
|
||||||
// Step 1:
|
|
||||||
// Check null
|
|
||||||
|
|
||||||
// Step 2:
|
|
||||||
// Convert obj to Student
|
|
||||||
|
|
||||||
// Step 3:
|
|
||||||
// Compare IDs
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -10,34 +10,27 @@ 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) {
|
||||||
|
students.addAll(newStudents);
|
||||||
// TODO:
|
|
||||||
|
|
||||||
// Merge students
|
|
||||||
|
|
||||||
// Hint:
|
|
||||||
// addAll()
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Student searchStudent(int id) {
|
public Student searchStudent(int id) {
|
||||||
|
for (Student student : students){
|
||||||
// TODO:
|
if (student.getId() == id){
|
||||||
|
return student;
|
||||||
// Loop through students
|
}
|
||||||
|
}
|
||||||
// Compare IDs
|
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean removeStudent(int id) {
|
public boolean removeStudent(int id) {
|
||||||
|
|
||||||
// TODO:
|
Student student = searchStudent(id);
|
||||||
|
|
||||||
// Find student
|
if (student != null){
|
||||||
|
students.remove(student);
|
||||||
// Remove student
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,41 +18,30 @@ public class StudentLoader extends Thread {
|
|||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Loads the database from the lines and assign its values to students array and return it
|
||||||
@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) {
|
||||||
|
String[] values = line.split(",");
|
||||||
|
|
||||||
// TODO:
|
int id = Integer.parseInt(values[0]);
|
||||||
// split line
|
String name = values[1];
|
||||||
|
String major = values[2];
|
||||||
// TODO:
|
double gpa = Double.parseDouble(values[3]);
|
||||||
// read values
|
|
||||||
|
|
||||||
// TODO:
|
|
||||||
// create Student
|
|
||||||
|
|
||||||
// TODO:
|
|
||||||
// add to students list
|
|
||||||
|
|
||||||
|
students.add(new Student(
|
||||||
|
id,
|
||||||
|
name,
|
||||||
|
major,
|
||||||
|
gpa
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public ArrayList<Student> getStudents() {
|
public ArrayList<Student> getStudents() {
|
||||||
|
|
||||||
return students;
|
return students;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -17,15 +17,10 @@ public class FileManager {
|
|||||||
String line;
|
String line;
|
||||||
|
|
||||||
while ((line = reader.readLine()) != null) {
|
while ((line = reader.readLine()) != null) {
|
||||||
|
lines.add(line);
|
||||||
// TODO:
|
|
||||||
|
|
||||||
// Add line to list
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
reader.close();
|
reader.close();
|
||||||
|
|
||||||
return lines;
|
return lines;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,10 +1,41 @@
|
|||||||
package lecture.multithreading.timeout;
|
package lecture.multithreading.timeout;
|
||||||
|
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args) {
|
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