-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathUniversity.java
More file actions
88 lines (70 loc) · 2.46 KB
/
University.java
File metadata and controls
88 lines (70 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import java.util.*;
class Student implements Comparable<Student> {
private String name;
private String studentId;
private double gpa;
public Student(String name, String studentId, double gpa) {
this.name = name;
this.studentId = studentId;
this.gpa = gpa;
}
public String getName() {
return name;
}
public String getStudentId() {
return studentId;
}
public double getGpa() {
return gpa;
}
@Override
public int compareTo(Student other) {
return this.studentId.compareTo(other.studentId);
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
Student student = (Student) obj;
return studentId.equals(student.studentId);
}
@Override
public int hashCode() {
return Objects.hash(studentId);
}
@Override
public String toString() {
return "name = " + name +
", studentId = " + studentId +
", gpa = " + gpa ;
}
}
public class University {
public static void main(String[] args) {
TreeSet<Student> students = new TreeSet<>();
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the number of students:");
int numberOfStudents = scanner.nextInt();
scanner.nextLine();
for (int i = 0; i < numberOfStudents; i++) {
System.out.println("Enter Info for student " + (i + 1) + ":");
System.out.print("Name: ");
String name = scanner.nextLine();
System.out.print("Student ID: ");
String studentId = scanner.nextLine();
System.out.print("GPA: ");
double gpa = scanner.nextDouble();
scanner.nextLine();
students.add(new Student(name, studentId, gpa));
}
System.out.print("Enter the Student ID to search for: ");
String searchId = scanner.nextLine();
Student searchStudent = new Student("", searchId, 0);
Student foundStudent = students.ceiling(searchStudent);
if (foundStudent != null && foundStudent.getStudentId().equals(searchId)) {
System.out.println(foundStudent);
} else {
System.out.println("Student with ID " + searchId + " not found.");
}
}
}