StudentManager and StudentLoader completed.
This commit is contained in:
@@ -46,7 +46,7 @@ public class Student {
|
|||||||
@Override
|
@Override
|
||||||
public boolean equals(Object obj) {
|
public boolean equals(Object obj) {
|
||||||
if (obj == null) return false;
|
if (obj == null) return false;
|
||||||
|
|
||||||
if (this == obj) return true;
|
if (this == obj) return true;
|
||||||
|
|
||||||
if (this.getClass() != obj.getClass()) return false;
|
if (this.getClass() != obj.getClass()) return false;
|
||||||
|
|||||||
@@ -10,34 +10,28 @@ 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 && !newStudents.isEmpty()) {
|
||||||
// TODO:
|
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 s = searchStudent(id);
|
||||||
|
|
||||||
// TODO:
|
if (s != null) {
|
||||||
|
students.remove(s);
|
||||||
// Find student
|
return true;
|
||||||
|
}
|
||||||
// Remove student
|
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,35 +24,27 @@ public class StudentLoader extends Thread {
|
|||||||
|
|
||||||
@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) {
|
||||||
|
try {
|
||||||
|
String[] parts = line.split(",");
|
||||||
|
|
||||||
// TODO:
|
if(parts.length == 4) {
|
||||||
// split line
|
int id = Integer.parseInt(parts[0].trim());
|
||||||
|
String name = parts[1].trim();
|
||||||
|
String major = parts[2].trim();
|
||||||
|
double gpa = Double.parseDouble(parts[3].trim());
|
||||||
|
|
||||||
// TODO:
|
Student student = new Student(id,name,major,gpa);
|
||||||
// read values
|
|
||||||
|
|
||||||
// TODO:
|
|
||||||
// create Student
|
|
||||||
|
|
||||||
// TODO:
|
|
||||||
// add to students list
|
|
||||||
|
|
||||||
|
students.add(student);
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.out.println(e.getMessage());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public ArrayList<Student> getStudents() {
|
public ArrayList<Student> getStudents() {
|
||||||
|
|
||||||
return students;
|
return students;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user