|
| 1 | +public class Student { |
| 2 | + |
| 3 | + private String id; |
| 4 | + private String name; |
| 5 | + private String email; |
| 6 | + private String department; |
| 7 | + private double gpa; |
| 8 | + |
| 9 | + // No-arg constructor |
| 10 | + public Student() { |
| 11 | + } |
| 12 | + |
| 13 | + // Parameterized constructor |
| 14 | + public Student(String id, String name, String email, String department, double gpa) { |
| 15 | + this.id = id; |
| 16 | + this.name = name; |
| 17 | + this.email = email; |
| 18 | + this.department = department; |
| 19 | + this.gpa = gpa; |
| 20 | + } |
| 21 | + |
| 22 | + // Copy constructor |
| 23 | + public Student(Student other) { |
| 24 | + if (other != null) { |
| 25 | + this.id = other.id; |
| 26 | + this.name = other.name; |
| 27 | + this.email = other.email; |
| 28 | + this.department = other.department; |
| 29 | + this.gpa = other.gpa; |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + // Getters |
| 34 | + public String getId() { return id; } |
| 35 | + public String getName() { return name; } |
| 36 | + public String getEmail() { return email; } |
| 37 | + public String getDepartment() { return department; } |
| 38 | + public double getGpa() { return gpa; } |
| 39 | + |
| 40 | + // Setters |
| 41 | + public void setId(String id) { this.id = id; } |
| 42 | + public void setName(String name) { this.name = name; } |
| 43 | + public void setEmail(String email) { this.email = email; } |
| 44 | + public void setDepartment(String department) { this.department = department; } |
| 45 | + public void setGpa(double gpa) { |
| 46 | + if (gpa < 0.0) gpa = 0.0; |
| 47 | + if (gpa > 4.0) gpa = 4.0; |
| 48 | + this.gpa = gpa; |
| 49 | + } |
| 50 | + |
| 51 | + // Convenience builder-style methods (optional chaining) |
| 52 | + public Student withId(String id) { setId(id); return this; } |
| 53 | + public Student withName(String name) { setName(name); return this; } |
| 54 | + public Student withEmail(String email) { setEmail(email); return this; } |
| 55 | + public Student withDepartment(String department) { setDepartment(department); return this; } |
| 56 | + public Student withGpa(double gpa) { setGpa(gpa); return this; } |
| 57 | + |
| 58 | + @Override |
| 59 | + public String toString() { |
| 60 | + return "Student{id='" + id + '\'' + |
| 61 | + ", name='" + name + '\'' + |
| 62 | + ", email='" + email + '\'' + |
| 63 | + ", department='" + department + '\'' + |
| 64 | + ", gpa=" + gpa + |
| 65 | + '}'; |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public boolean equals(Object o) { |
| 70 | + if (this == o) return true; |
| 71 | + if (!(o instanceof Student)) return false; |
| 72 | + Student s = (Student) o; |
| 73 | + return (id != null && id.equals(s.id)); |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public int hashCode() { |
| 78 | + return id == null ? 0 : id.hashCode(); |
| 79 | + } |
| 80 | +} |
0 commit comments