Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6e2370d646 | ||
|
|
160475f978 |
Generated
+10
@@ -0,0 +1,10 @@
|
|||||||
|
# Default ignored files
|
||||||
|
/shelf/
|
||||||
|
/workspace.xml
|
||||||
|
# Ignored default folder with query files
|
||||||
|
/queries/
|
||||||
|
# Datasource local storage ignored files
|
||||||
|
/dataSources/
|
||||||
|
/dataSources.local.xml
|
||||||
|
# Editor-based HTTP Client requests
|
||||||
|
/httpRequests/
|
||||||
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="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>
|
||||||
|
<remote-repository>
|
||||||
|
<option name="id" value="central" />
|
||||||
|
<option name="name" value="Central Repository" />
|
||||||
|
<option name="url" value="https://mirror-maven.runflare.com/maven2" />
|
||||||
|
</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" project-jdk-name="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>
|
||||||
+24
-29
@@ -12,18 +12,17 @@ public class Main {
|
|||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
|
|
||||||
Scanner scanner = new Scanner(System.in);
|
Scanner scanner = new Scanner(System.in);
|
||||||
|
|
||||||
StudentManager manager = new StudentManager();
|
StudentManager manager = new StudentManager();
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
|
System.out.println("\n--- Student Search Engine Menu ---");
|
||||||
|
System.out.println("1. Load students");
|
||||||
|
System.out.println("2. Search student");
|
||||||
|
System.out.println("3. Remove student");
|
||||||
|
System.out.println("4. Print all students");
|
||||||
|
System.out.println("5. Exit");
|
||||||
|
System.out.print("Enter your choice: ");
|
||||||
|
|
||||||
System.out.println("""
|
|
||||||
1.Load students
|
|
||||||
2.Search student
|
|
||||||
3.Remove student
|
|
||||||
4.Print students
|
|
||||||
5.Exit
|
|
||||||
""");
|
|
||||||
|
|
||||||
int choice = scanner.nextInt();
|
int choice = scanner.nextInt();
|
||||||
|
|
||||||
@@ -33,32 +32,28 @@ public class Main {
|
|||||||
|
|
||||||
List<String> data = FileManager.loadStudents("students.txt");
|
List<String> data = FileManager.loadStudents("students.txt");
|
||||||
|
|
||||||
// TODO:
|
int size = data.size();
|
||||||
|
List<String> part1 = data.subList(0, size/3);
|
||||||
|
List<String> part2 = data.subList(size/3, (size/3)*2);
|
||||||
|
List<String> part3 = data.subList((size/3)*2, size);
|
||||||
|
|
||||||
// Split data into 3 parts
|
StudentLoader t1 = new StudentLoader(part1);
|
||||||
|
StudentLoader t2 = new StudentLoader(part2);
|
||||||
|
StudentLoader t3 = new StudentLoader(part3);
|
||||||
|
|
||||||
List<String> part1 = null;
|
t1.start();
|
||||||
|
t2.start();
|
||||||
|
t3.start();
|
||||||
|
|
||||||
List<String> part2 = null;
|
t1.join();
|
||||||
|
t2.join();
|
||||||
|
t3.join();
|
||||||
|
|
||||||
List<String> part3 = null;
|
manager.addStudents(t1.getStudents());
|
||||||
|
manager.addStudents(t2.getStudents());
|
||||||
// TODO:
|
manager.addStudents(t3.getStudents());
|
||||||
|
|
||||||
StudentLoader t1 = null;
|
|
||||||
|
|
||||||
StudentLoader t2 = null;
|
|
||||||
|
|
||||||
StudentLoader t3 = null;
|
|
||||||
|
|
||||||
// TODO:
|
|
||||||
|
|
||||||
// start()
|
|
||||||
|
|
||||||
// join()
|
|
||||||
|
|
||||||
// merge results
|
|
||||||
|
|
||||||
|
System.out.println("Student loaded successfully.");
|
||||||
}
|
}
|
||||||
|
|
||||||
case 2 -> {
|
case 2 -> {
|
||||||
|
|||||||
@@ -40,33 +40,24 @@ 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) {
|
||||||
|
|
||||||
// TODO:
|
if(obj == null){
|
||||||
|
|
||||||
// Step 1:
|
|
||||||
// Check null
|
|
||||||
|
|
||||||
// Step 2:
|
|
||||||
// Convert obj to Student
|
|
||||||
|
|
||||||
// Step 3:
|
|
||||||
// Compare IDs
|
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(getClass() != obj.getClass()){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
Student otherStudent = (Student)obj;
|
||||||
|
|
||||||
|
return this.id == otherStudent.id;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
|
|
||||||
|
|||||||
@@ -4,50 +4,49 @@ import dev.model.Student;
|
|||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
|
||||||
public class StudentManager {
|
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) {
|
||||||
|
if(newStudents != null){
|
||||||
// TODO:
|
this.students.addAll(newStudents);
|
||||||
|
}
|
||||||
// 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) {
|
||||||
|
Student foundStudent = null;
|
||||||
|
for (Student s : students){
|
||||||
|
if (s.getId() == id){
|
||||||
|
foundStudent = s;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// TODO:
|
if (foundStudent != null){
|
||||||
|
students.remove(foundStudent);
|
||||||
// Find student
|
return true;
|
||||||
|
}
|
||||||
// Remove student
|
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void printStudents() {
|
public void printStudents() {
|
||||||
|
if (students.isEmpty()) {
|
||||||
|
System.out.println("No students to display.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
for (Student student : students) {
|
for (Student student : students) {
|
||||||
|
|
||||||
System.out.println(student);
|
System.out.println(student);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,46 +9,38 @@ import java.util.List;
|
|||||||
|
|
||||||
public class StudentLoader extends Thread {
|
public class StudentLoader extends Thread {
|
||||||
|
|
||||||
// Assigned file lines
|
|
||||||
|
|
||||||
private final List<String> lines;
|
private final List<String> lines;
|
||||||
|
|
||||||
// Local student storage
|
|
||||||
|
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
@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) {
|
||||||
|
if(line == null || line.trim().isEmpty()){
|
||||||
// TODO:
|
continue;
|
||||||
// split line
|
|
||||||
|
|
||||||
// TODO:
|
|
||||||
// read values
|
|
||||||
|
|
||||||
// TODO:
|
|
||||||
// create Student
|
|
||||||
|
|
||||||
// TODO:
|
|
||||||
// add to students list
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
String[] parts = line.split(",");
|
||||||
|
|
||||||
|
if(parts.length == 4){
|
||||||
|
try {
|
||||||
|
int id = Integer.parseInt(parts[0].trim());
|
||||||
|
String name = parts[1].trim();
|
||||||
|
String major = parts[2].trim();
|
||||||
|
double gpa = Double.parseDouble(parts[3].trim());
|
||||||
|
|
||||||
|
Student student = new Student(id, name, major, gpa);
|
||||||
|
students.add(student);
|
||||||
|
}catch (Exception e){
|
||||||
|
System.out.println(e.getMessage());
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
System.out.println("skipping malformed line: " + line);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public ArrayList<Student> getStudents() {
|
public ArrayList<Student> getStudents() {
|
||||||
|
|||||||
@@ -13,20 +13,13 @@ public class FileManager {
|
|||||||
List<String> lines = new ArrayList<>();
|
List<String> lines = new ArrayList<>();
|
||||||
|
|
||||||
BufferedReader reader = new BufferedReader(new FileReader(path));
|
BufferedReader reader = new BufferedReader(new FileReader(path));
|
||||||
|
|
||||||
String line;
|
String line;
|
||||||
|
|
||||||
while ((line = reader.readLine()) != null) {
|
while ((line = reader.readLine()) != null) {
|
||||||
|
if (!line.trim().isEmpty()) {
|
||||||
// TODO:
|
lines.add(line.trim());
|
||||||
|
}
|
||||||
// Add line to list
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
reader.close();
|
reader.close();
|
||||||
|
|
||||||
return lines;
|
return lines;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,10 +1,23 @@
|
|||||||
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
|
Scanner scanner = new Scanner(System.in);
|
||||||
|
|
||||||
|
System.out.println("you have 10 seconds: ");
|
||||||
|
System.out.println("enter your name: ");
|
||||||
|
|
||||||
|
MyRunnable myRunnable = new MyRunnable();
|
||||||
|
Thread thread = new Thread(myRunnable);
|
||||||
|
thread.setDaemon(true);
|
||||||
|
thread.start();
|
||||||
|
|
||||||
|
String name = scanner.nextLine();
|
||||||
|
System.out.println("Hello " + name);
|
||||||
|
scanner.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,20 @@
|
|||||||
|
package lecture.multithreading.timeout;
|
||||||
|
|
||||||
|
public class MyRunnable implements Runnable{
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
for (int i = 0; i <= 10; i++) {
|
||||||
|
try {
|
||||||
|
Thread.sleep(1000);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
System.out.println("thread was interrupted");
|
||||||
|
}
|
||||||
|
|
||||||
|
if(i==10){
|
||||||
|
System.out.println("time is up");
|
||||||
|
System.exit(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user