试图创建一个简单的课程管理应用程序使用坚实的原则和最佳实践的面向对象编程。寻找关于我在下面的github项目的src/main/lib中创建的库的反馈。示例用法显示在src/main/app中,如果您有兴趣的话,但是我不太关心这个部分的反馈。
应用程序的一些要求:
除了一般的代码审查之外,我还有一些具体的问题:
一些示例代码(完整的代码可以在github中找到):
public class Course {
private String name;
private Calendar time;
private HashSet<Student> students;
private Room room;
public Course(String name, Calendar time) {
this.name = name;
this.time = time;
this.students = new HashSet<>();
}
public HashSet<Student> students() {
return students;
}
public void enroll(Student student) {
students.add(student);
}
@Override public String toString() {
return String.format("Course name: %s%nCourse Time: %s", this.name, this.time.getTime().toString());
}
public Calendar getTime() {
return time;
}
public void setRoom(Room room) {
this.room = room;
}
public Room getRoom() {
return room;
}
}学生:
public class Student {
private String name;
private HashSet<Course> courses;
public Student(String name) {
this.name = name;
this.courses = new HashSet<>();
}
@Override public String toString() {
return name;
}
public HashSet<Course> courses() {
return this.courses;
}
public void enroll(Course course) {
courses.add(course);
}
}书记官长的执行情况:
public class RegistrarImpl implements Registrar {
private HashSet<Course> courses = new HashSet<>();
private HashSet<Student> students = new HashSet<>();
@Override
public void registerCourse(Course course) {
courses.add(course);
}
@Override
public void registerStudent(Student student) {
students.add(student);
}
@Override
public HashSet<Course> allCourses() {
return courses;
}
@Override
public HashSet<Student> allStudents() {
return students;
}
@Override
public void enroll(Course course, Student student) throws EnrollmentException {
if(!courses.contains(course)) {
throw new EnrollmentException("Course has not been registered.");
} else if(!students.contains(student)) {
throw new EnrollmentException("Student has not been registered.");
}
for(Course c : student.courses()) {
if(c.getTime().getTime().getTime() == course.getTime().getTime().getTime()) {
throw new EnrollmentException("Student already has class at this time.");
}
}
course.enroll(student);
student.enroll(course);
}
}发布于 2018-05-18 09:21:11
学生和课程之间的关系应该是怎样的?
就像在现实世界中一样:)一门课程并不是学生的一部分,所以我不会在学生中拥有Set<Course>。
相反,它应该类似于Enrollment,它将一个Student耦合到一个Course (也可能在给定的时间点)。并将其存储在Registrar中。(例如,作为Set<Enrollment>或某些Maps (如果性能很重要))
Registrar可以检查Student是否已经注册或占用了特定的时间。
https://codereview.stackexchange.com/questions/194677
复制相似问题