-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStudent.java
More file actions
67 lines (56 loc) · 1.62 KB
/
Copy pathStudent.java
File metadata and controls
67 lines (56 loc) · 1.62 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
import java.util.Objects;
public class Student {
private int id;
private String name;
private double grade;
public Student(int id, String name) {
if (id <= 0) {
throw new IllegalArgumentException("Error: ID must be a positive number");
}
if (name == null || name.trim().isEmpty()) {
throw new IllegalArgumentException("Error: Name cannot be null or empty");
}
this.id = id;
this.name = name.trim();
this.grade = 0.0;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public double getGrade() {
return grade;
}
public void setName(String name) {
if (name == null || name.trim().isEmpty()) {
throw new IllegalArgumentException("Error: Name cannot be null or empty");
}
this.name = name.trim();
}
public void setGrade(double grade) {
if (grade < 0.0 || grade > 100.0) {
throw new IllegalArgumentException("Error: Grade must be between (0-100)");
}
this.grade = grade;
}
@Override
public String toString() {
return "ID: " + id + " Name: " + name + " Grade: " + grade;
}
public void displayInfo() {
System.out.println(this.toString());
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return id == student.id;
}
@Override
public int hashCode() {
return Objects.hash(id);
}
}