한 번에 끝내는 Java/Spring 웹 개발 마스터
package School;
import java.util.ArrayList;
public class Student {
private int studentId; // 학번
private String studentName; // 이름
private Subject majorSubject; // 중점 과목
// 학생의 성적 리스트
// addSubjectSocre() 메서드가 호출되면 리스트에 추가 됨
private ArrayList<Score> scoreList = new ArrayList<Score>();
public Student(int studentId, String studentName, Subject majorSubject) {
this.studentId = studentId;
this.studentName = studentName;
this.majorSubject = majorSubject;
}
public void addSubjectScore(Score score) {
scoreList.add(score);
}
public int getStudentId() {
return studentId;
}
public void setStudentId(int studentId) {
this.studentId = studentId;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public Subject getMajorSubject() {
return majorSubject;
}
public void setMajorSubject(Subject majorSubject) {
this.majorSubject = majorSubject;
}
public ArrayList<Score> getScoreList() {
return scoreList;
}
public void setScoreList(ArrayList<Score> scoreList) {
this.scoreList = scoreList;
}
}
package School;
import java.util.ArrayList;
import Utils.Define;
public class Subject {
private String subjectName; // 과목 이름
private int subjectId; // 과목 고유번호
private int gradeType; // 과목 평가 방법 기본은 A,B 방식
// 수강 신청한 학생 리스트
// register() 메서드를 호출하면 리스트에 추가 됨
private ArrayList<Student> studentList = new ArrayList<Student>();
public Subject(String subjectName, int subjectId) {
this.subjectName = subjectName;
this.subjectId = subjectId;
this.gradeType = Define.AB_TYPE; // 기본적으로 A, B 타입
}
public String getSubjectName() {
return subjectName;
}
public void setSubjectName(String subjectName) {
this.subjectName = subjectName;
}
public int getSubjectId() {
return subjectId;
}
public void setSubjectId(int subjectId) {
this.subjectId = subjectId;
}
public ArrayList<Student> getStudentList() {
return studentList;
}
public void setStudentList(ArrayList<Student> studentList) {
this.studentList = studentList;
}
public int getGradeType() {
return gradeType;
}
public void setGradeType(int gradeType) {
this.gradeType = gradeType;
}
public void register(Student student) { // 수강신청
studentList.add(student);
}
}
package School;
public class Score {
int studentId; // 학번
Subject subject; // 과목
int point; // 점수
public Score(int studentId, Subject subject, int point) {
this.studentId = studentId;
this.subject = subject;
this.point = point;
}
public int getStudentId() {
return studentId;
}
public void setStudentId(int studentId) {
this.studentId = studentId;
}
public Subject getSubject() {
return subject;
}
public void setSubject(Subject subject) {
this.subject = subject;
}
public int getPoint() {
return point;
}
public void setPoint(int point) {
this.point = point;
}
public String toString() {
return "학번:" + studentId + "," + subject.getSubjectName() + ":" + point;
}
}
package Utils;
public class Define {
public static final int KOREAN = 1001; // 국어
public static final int MATH = 2001; // 수학
public static final int AB_TYPE = 0; // A, B, C
public static final int SAB_TYPE = 1; // S, A, B, c
public static final int PF_TYPE = 2; // P, F
}
제공된 위의 사진에 맞게, 각자의 클래스가 필요한 변수들을 입력해주었고, 서로 주고 받을수 있는 부분들은 이용해서 Student, Score, Subject 클래스를 구현해보았다.
package Grade;
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;
}
}
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");
}
}
public class School {
private static School instance = new School();
private static String SCHOOL_NAME = "Good School";
private ArrayList<Student> studentList = new ArrayList<Student>();
private ArrayList<Subject> subjectList = new ArrayList<Subject>();
private School(){}
public static School getInstance(){
if(instance == null)
instance = new School();
return instance;
}
public ArrayList<Student> getStudentList(){
return studentList;
}
public void addStudent(Student student){
studentList.add(student);
}
public void addSubject(Subject subject) {
subjectList.add(subject);
}
public ArrayList<Subject> getSubjectList() {
return subjectList;
}
public void setSubjectList(ArrayList<Subject> subjectList) {
this.subjectList = subjectList;
}
}
public class TestMain {
School goodSchool = School.getInstance();
Subject korean;
Subject math;
GenerateGradeReport gradeReport = new GenerateGradeReport();
public static void main(String[] args) {
TestMain test = new TestMain();
test.creatSubject();
test.createStudent();
String report = test.gradeReport.getReport(); //성적 결과 생성
System.out.println(report); // 출력
}
//테스트 과목 생성
public void creatSubject(){
korean = new Subject("국어", Define.KOREAN);
math = new Subject("수학", Define.MATH);
goodSchool.addSubject(korean);
goodSchool.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 );
goodSchool.addStudent(student1);
goodSchool.addStudent(student2);
goodSchool.addStudent(student3);
goodSchool.addStudent(student4);
goodSchool.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.addSubjectScore(score);
}
}