1. 전공과목 vs 일반 과목
학생의 전공에 따라 과목의 학점을 다르게 산출함
GradeEvaluation 인터페이스
public interface GradeEvaluation {
public String getGrade(int point);
}
public class BasicEvaluation implements GradeEvaluation{
@Override
public String getGrade(int point) {
String grade;
if(point >=90 && point <=100)
grade = "A";
else if(point >=80 && point <=89)
grade = "B";
else if(point >=70 && point <=79)
grade = "C";
else if(point >=55 && point <=69)
grade = "D";
else
grade = "F";
return grade;
}
}
public class MajorEvaluation implements GradeEvaluation{
@Override
public String getGrade(int point) {
String grade;
if(point >=95 && point <=100)
grade = "S";
else if(point >=90 && point <=94)
grade = new String("A");
else if(point >=80 && point <=89)
grade = "B";
else if(point >=70 && point <=79)
grade = "C";
else if(point >=60 && point <=69)
grade = "D";
else
grade = "F";
return grade;
}
}
1. 리포트 클래스
2. 리포트 클래스 구현
public class GenerateGradeReport {
School school = School.getInstance();
public static final String TITLE = " 수강생 학점 \t\t\n";
public static final String HEADER = " 이름 | 학번 |중점과목| 점수 \n";
public static final String LINE = "-------------------------------------\n";
private StringBuffer buffer = new StringBuffer();
public String getReport(){
ArrayList<Subject> subjectList = school.getSubjectList(); // 모든 과목에 대한 학점 산출
for( Subject subject : subjectList) {
makeHeader(subject);
makeBody(subject);
makeFooter();
}
return buffer.toString(); // String 으로 반환
}
public void makeHeader(Subject subject){
buffer.append(GenerateGradeReport.LINE);
buffer.append("\t" + subject.getSubjectName());
buffer.append(GenerateGradeReport.TITLE );
buffer.append(GenerateGradeReport.HEADER );
buffer.append(GenerateGradeReport.LINE);
}
public void makeBody(Subject subject){
ArrayList<Student> studentList = subject.getStudentList(); // 각 과목의 학생들
for(int i=0; i<studentList.size(); i++){
Student student = studentList.get(i);
buffer.append(student.getStudentName());
buffer.append(" | ");
buffer.append(student.getStudentId());
buffer.append(" | ");
buffer.append(student.getMajorSubject().getSubjectName() + "\t");
buffer.append(" | ");
getScoreGrade(student, subject.getSubjectId()); //학생별 해당과목 학점 계산
buffer.append("\n");
buffer.append(LINE);
}
}
public void getScoreGrade(Student student, int subjectId){
ArrayList<Score> scoreList = student.getScoreList();
int majorId = student.getMajorSubject().getSubjectId();
GradeEvaluation[] gradeEvaluation = {new BasicEvaluation(), new MajorEvaluation()}; //학점 평가 클래스들
for(int i=0; i<scoreList.size(); i++){ // 학생이 가진 점수들
Score score = scoreList.get(i);
if(score.getSubject().getSubjectId() == subjectId) { // 현재 학점을 산출할 과목
String grade;
if(score.getSubject().getSubjectId() == majorId) // 중점 과목인 경우
grade = gradeEvaluation[Define.SAB_TYPE].getGrade(score.getPoint()); //중점 과목 학점 평가 방법
else
grade = gradeEvaluation[Define.AB_TYPE].getGrade(score.getPoint()); // 중점 과목이 아닌 경우
buffer.append(score.getPoint());
buffer.append(":");
buffer.append(grade);
buffer.append(" | ");
}
}
}
public void makeFooter(){
buffer.append("\n");
}
}
1. Main 부분
public class TestMain {
School HongSchool = School.getInstance();
Subject korean;
Subject math;
GenerateGradeReport gradeReport = new GenerateGradeReport();
public static void main(String[] args) {
TestMain test = new TestMain();
test.createSubject();
test.createStudent();
String report = test.gradeReport.getReport();
System.out.println(report);
}
public void createSubject() {
korean = new Subject("국어", Define.KOREAN, Define.AB_TYPE);
math = new Subject("수학", Define.MATH, Define.AB_TYPE);
HongSchool.addSubject(korean);
HongSchool.addSubject(math);
}
public void createStudent() {
Student student1 = new Student(211213, "강감찬", korean );
Student student2 = new Student(212330, "김유신", math );
Student student3 = new Student(201518, "신사임당", korean );
Student student4 = new Student(202360, "이순신", korean );
Student student5 = new Student(201290, "홍길동", math );
HongSchool.addStudent(student1);
HongSchool.addStudent(student2);
HongSchool.addStudent(student3);
HongSchool.addStudent(student4);
HongSchool.addStudent(student5);
korean.register(student1);
korean.register(student2);
korean.register(student3);
korean.register(student4);
korean.register(student5);
math.register(student1);
math.register(student2);
math.register(student3);
math.register(student4);
math.register(student5);
addScoreForStudent(student1, korean, 95);
addScoreForStudent(student1, math, 56);
addScoreForStudent(student2, korean, 95);
addScoreForStudent(student2, math, 95);
addScoreForStudent(student3, korean, 100);
addScoreForStudent(student3, math, 88);
addScoreForStudent(student4, korean, 89);
addScoreForStudent(student4, math, 95);
addScoreForStudent(student5, korean, 85);
addScoreForStudent(student5, math, 56);
}
public void addScoreForStudent(Student student, Subject subject, int point ) {
Score score = new Score(student.getStudentId(), subject, point);
student.addStudentScore(score);
}
}