-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPerson.java
More file actions
133 lines (114 loc) · 4.52 KB
/
Copy pathPerson.java
File metadata and controls
133 lines (114 loc) · 4.52 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import java.util.*;
import java.io.*;
// Abstract base class representing a person
public abstract class Person {
//Fields
private String id;
private String name;
private String email;
private String department;
// Constructs a person with given ID, name, email, and department
public Person(String id, String name, String email, String department) {
this.id = id;
this.name = name;
this.email = email;
this.department = department;
}
//Getters
public String getId() {
return id;
}
public String getName() {
return name;
}
public String getEmail() {
return email;
}
public String getDepartment() {
return department;
}
// Returns a string representation of the person
public String toString() {
return name + " (ID: " + id + ")";
}
}
// Represents a student and their course-related data
class Student extends Person implements Reportable {
// Courses the student is currently enrolled in
private List<Course> enrolledCourses;
// Map of completed courses and their corresponding grades
private Map<Course, String> completedCoursesWithGrades;
// Constructs a student and initializes course collections
public Student(String id, String name, String email, String department) {
super(id, name, email, department);
this.enrolledCourses = new ArrayList<>();
this.completedCoursesWithGrades = new LinkedHashMap<>();
}
// Enrolls the student in a course
public void enrollCourse(Course course) {
enrolledCourses.add(course);
}
// Records a grade for a completed course
public void assignGrade(Course course, String grade) {
completedCoursesWithGrades.put(course, grade);
}
// Calculates the student's GPA based on completed courses
public double calculateGPA() {
double total = 0;
int totalCredits = 0;
for (Map.Entry<Course, String> entry : completedCoursesWithGrades.entrySet()) {
double gradeValue = GradeConverter.convert(entry.getValue());
int credit = entry.getKey().getCredits();
total += gradeValue * credit;
totalCredits += credit;
}
return totalCredits == 0 ? 0.0 : total / totalCredits;
}
// Generates a simple report of the student
public String generateReport() {
StringWriter writer = new StringWriter();
writer.write("Student ID: " + getId() + "\n");
writer.write("Name: " + getName() + "\n");
writer.write("Email: " + getEmail() + "\n");
writer.write("Major: " + getDepartment() + "\n");
writer.write("Status: Active\n\n");
return writer.toString();
}
// Generates a detailed report of the student
public String generateDetailedReport() {
StringWriter writer = new StringWriter();
writer.write("Student ID: " + getId() + "\n");
writer.write("Name: " + getName() + "\n");
writer.write("Email: " + getEmail() + "\n");
writer.write("Major: " + getDepartment() + "\n");
writer.write("Status: Active\n\n\n");
writer.write("Enrolled Courses:\n");
for (Course course : enrolledCourses) {
if (!completedCoursesWithGrades.containsKey(course)) {
writer.write("- " + course.getName() + " (" + course.getCode() + ")\n");
}
}
writer.write("\nCompleted Courses:\n");
for (Map.Entry<Course, String> entry : completedCoursesWithGrades.entrySet()) {
Course course = entry.getKey();
String grade = entry.getValue();
writer.write("- " + course.getName() + " (" + course.getCode() + "): " + grade + "\n");
}
writer.write("\nGPA: " + String.format(Locale.ENGLISH, "%.2f", calculateGPA()));
return writer.toString();
}
}
// Represents an academic staff member
class AcademicMember extends Person {
// List of courses the academic member is teaching
private List<Course> taughtCourses;
// Constructs an academic member and initializes course list
public AcademicMember(String id, String name, String email, String department) {
super(id, name, email, department);
this.taughtCourses = new ArrayList<>();
}
// Assigns a course to this academic member
public void assignCourse(Course course) {
taughtCourses.add(course);
}
}