WS7 #1
Generated
+10
@@ -0,0 +1,10 @@
|
||||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# Editor-based HTTP Client requests
|
||||
/httpRequests/
|
||||
# Ignored default folder with query files
|
||||
/queries/
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
||||
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="Central Repository" />
|
||||
<option name="url" value="https://repo.maven.apache.org/maven2" />
|
||||
</remote-repository>
|
||||
<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>
|
||||
</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" languageLevel="JDK_25" default="true" project-jdk-name="ms-25" project-jdk-type="JavaSDK" />
|
||||
</project>
|
||||
Generated
+6
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
||||
+21
-43
@@ -8,92 +8,70 @@ import java.util.List;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
|
||||
StudentManager manager = new StudentManager();
|
||||
|
||||
while (true) {
|
||||
|
||||
System.out.println("""
|
||||
1.Load students
|
||||
2.Search student
|
||||
3.Remove student
|
||||
4.Print students
|
||||
5.Exit
|
||||
""");
|
||||
5.Exit""");
|
||||
|
||||
int choice = scanner.nextInt();
|
||||
|
||||
switch (choice) {
|
||||
|
||||
case 1 -> {
|
||||
|
||||
List<String> data = FileManager.loadStudents("students.txt");
|
||||
|
||||
// TODO:
|
||||
int size = data.size();
|
||||
int part1Size = size / 3;
|
||||
int part2Size = 2 * size / 3;
|
||||
|
||||
// Split data into 3 parts
|
||||
List<String> part1 = data.subList(0, part1Size);
|
||||
List<String> part2 = data.subList(part1Size, part2Size);
|
||||
List<String> part3 = data.subList(part2Size, size);
|
||||
|
||||
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:
|
||||
|
||||
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 successfully.");
|
||||
}
|
||||
|
||||
case 2 -> {
|
||||
|
||||
System.out.print("ID: ");
|
||||
|
||||
int id = scanner.nextInt();
|
||||
|
||||
System.out.println(manager.searchStudent(id));
|
||||
|
||||
}
|
||||
|
||||
case 3 -> {
|
||||
|
||||
System.out.print("ID: ");
|
||||
|
||||
int id = scanner.nextInt();
|
||||
|
||||
boolean removed = manager.removeStudent(id);
|
||||
|
||||
System.out.println(removed);
|
||||
|
||||
}
|
||||
|
||||
case 4 ->
|
||||
manager.printStudents();
|
||||
case 4 -> manager.printStudents();
|
||||
|
||||
case 5 -> {
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,21 +1,12 @@
|
||||
package dev.model;
|
||||
|
||||
public class Student {
|
||||
|
||||
private final int id;
|
||||
|
||||
private final String name;
|
||||
|
||||
private final String major;
|
||||
|
||||
private final double gpa;
|
||||
|
||||
public Student(
|
||||
int id,
|
||||
String name,
|
||||
String major,
|
||||
double gpa) {
|
||||
|
||||
public Student(int id, String name, String major, double gpa) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.major = major;
|
||||
@@ -40,36 +31,18 @@ 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 student = (Student) obj;
|
||||
return id == student.id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
|
||||
return "Student{" +
|
||||
"id=" + id +
|
||||
", name='" + name + '\'' +
|
||||
@@ -77,5 +50,4 @@ public class Student {
|
||||
", gpa=" + gpa +
|
||||
'}';
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,55 +1,37 @@
|
||||
package dev.service;
|
||||
|
||||
import dev.model.Student;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
|
||||
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:
|
||||
|
||||
// Find student
|
||||
|
||||
// Remove student
|
||||
|
||||
for (int i = 0; i < students.size(); i++) {
|
||||
if (students.get(i).getId() == id) {
|
||||
students.remove(i);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void printStudents() {
|
||||
|
||||
for (Student student : students) {
|
||||
|
||||
System.out.println(student);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,59 +1,33 @@
|
||||
package dev.thread;
|
||||
|
||||
|
||||
|
||||
import dev.model.Student;
|
||||
|
||||
import java.util.ArrayList;
|
||||
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<>();
|
||||
|
||||
public StudentLoader(List<String> lines) {
|
||||
|
||||
this.lines = lines;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
|
||||
// TODO:
|
||||
|
||||
/*
|
||||
Example line:
|
||||
|
||||
1001,Aryan,Computer Science,3.7
|
||||
*/
|
||||
|
||||
for (String line : lines) {
|
||||
String[] data = line.split(",");
|
||||
if (data.length == 4) {
|
||||
int id = Integer.parseInt(data[0].trim());
|
||||
String name = data[1].trim();
|
||||
String major = data[2].trim();
|
||||
double gpa = Double.parseDouble(data[3].trim());
|
||||
|
||||
// TODO:
|
||||
// split line
|
||||
|
||||
// TODO:
|
||||
// read values
|
||||
|
||||
// TODO:
|
||||
// create Student
|
||||
|
||||
// TODO:
|
||||
// add to students list
|
||||
|
||||
students.add(new Student(id, name, major, gpa));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public ArrayList<Student> getStudents() {
|
||||
|
||||
return students;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -2,31 +2,21 @@ package dev.utils;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileReader;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,10 +1,31 @@
|
||||
package lecture.multithreading.timeout;
|
||||
|
||||
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
// TODO
|
||||
}
|
||||
}
|
||||
Thread worker = new Thread(() -> {
|
||||
try {
|
||||
System.out.println("Worker thread started...");
|
||||
Thread.sleep(5000);
|
||||
System.out.println("Worker thread finished its job.");
|
||||
} catch (InterruptedException e) {
|
||||
System.out.println("Worker thread was interrupted (Timeout reached)!");
|
||||
}
|
||||
});
|
||||
|
||||
worker.start();
|
||||
|
||||
try {
|
||||
worker.join(2000);
|
||||
|
||||
if (worker.isAlive()) {
|
||||
System.out.println("Main thread: Task took too long. Interrupting...");
|
||||
worker.interrupt();
|
||||
} else {
|
||||
System.out.println("Main thread: Task completed within the time limit.");
|
||||
}
|
||||
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user