-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathUniversity.java
More file actions
72 lines (60 loc) · 2.12 KB
/
University.java
File metadata and controls
72 lines (60 loc) · 2.12 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
import java.util.*;
class Student {
private String name;
private int studentId;
private double gpa;
Student (String name, int studentId, double gpa) {
this.name = name;
this.studentId = studentId;
this.gpa = gpa;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setStudentId(int studentId) {
this.studentId = studentId;
}
public int getStudentId() {
return studentId;
}
public void setGpa(double gpa) {
this.gpa = gpa;
}
public double getGpa() {
return gpa;
}
public String showDetails(int studentId) {
return "\nname: "+ this.name +"\nID: "+ this.studentId + "\nGPA: "+ this.gpa;
}
}
public class University {
public static void main(String[] args) {
TreeSet<Student> students = new TreeSet<>();
Scanner scan = new Scanner(System.in);
System.out.print("Type the number of students you want to add: ");
int studentNum = scan.nextInt();
for (int i = 0; studentNum > i; i++) {
System.out.print("Enter student's name: ");
String studentName = scan.next();
System.out.print("Enter student's ID: ");
int studentId = scan.nextInt();
System.out.print("Enter student's GPA: ");
double studentGpa = scan.nextDouble();
Student student = new Student(studentName,studentId,studentGpa);
students.add(student);
System.out.println("---------------------------");
}
System.out.print("Enter the student ID you want to search: ");
int search = scan.nextInt();
List<Student> studentList = new ArrayList<>(students);
for (int i = 0; studentNum > i; i++) {
int searchedStudentID = studentList.get(i).getStudentId();
if (search == searchedStudentID) {
System.out.println(studentList.get(i).showDetails(searchedStudentID));
}
}
}
}