๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Java

[Java-๊ธฐ์ดˆ] ์‹ค์Šต (ํ•™์  ์‚ฐ์ถœ ํ”„๋กœ๊ทธ๋žจ 2)

by ์ฝ”๋”ฉํ•˜๋Š” ๋ถ•์–ด 2021. 2. 24.
๋ฐ˜์‘ํ˜•

โ–ถ ํ”„๋กœ๊ทธ๋žจ ์—…๊ทธ๋ ˆ์ด๋“œํ•˜๊ธฐ(์–ด์ œ ๋งŒ๋“  ํ•™์  ์‚ฐ์ถœ ํ”„๋กœ๊ทธ๋žจ์„ ๋ฐ”ํƒ•์œผ๋กœ!)

โ€‹

*๊ณผ๋ชฉ๊ณผ ํ•™์  ์ •์ฑ…์ด ์ถ”๊ฐ€๋˜๋Š” ๊ฒฝ์šฐ

โ€‹

Define.java

package utils;

public class Define {
	public static final int KOREAN = 1001;    // ๊ตญ์–ด
	public static final int MATH = 2001;      // ์ˆ˜ํ•™
	public static final int DANCE = 3001;      // ๋ฐฉ์†ก ๋Œ„์Šค
	public static final int ENGLISH = 4001;      // ์˜์–ด
	
	public static final int AB_TYPE = 0;      // ์ผ๋ฐ˜ ๊ณผ๋ชฉ ํ•™์  ์‚ฐ์ถœ ์ •์ฑ…
	public static final int SAB_TYPE = 1;     // ํ•„์ˆ˜ ๊ณผ๋ชฉ ํ•™์  ์‚ฐ์ถœ ์ •์ฑ…
	public static final int PF_TYPE = 2;     // pass/fail ๋ฐฉ์‹ 
}

 

 

TestMain.java

package test;

import school.School;
import school.Score;
import school.Student;
import school.Subject;
import school.report.GenerateGradeReport;
import utils.Define;

public class TestMain {
	School goodSchool = School.getInstance();
	Subject korean;
	Subject math;
	Subject dance;
	Subject english;	

	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);
		math = new Subject("์ˆ˜ํ•™", Define.MATH);
		dance = new Subject("๋ฐฉ์†ก ๋Œ„์Šค", Define.DANCE); 
		english = new Subject("์˜์–ด", Define.ENGLISH); 
		
		dance.setGradeType(Define.PF_TYPE); // ํ•™์  ํ‰๊ฐ€ ์ •์ฑ… ์ง€์ •

		goodSchool.addSubject(korean);
		goodSchool.addSubject(math);
		goodSchool.addSubject(dance);
		goodSchool.addSubject(english);
	}

	public void createStudent() { // ํ•™์ƒ ์ƒ์„ฑ
		Student student1 = new Student(181213, "์•ˆ์„ฑ์›", korean);
		Student student2 = new Student(181518, "์˜คํƒœํ›ˆ", math);
		Student student3 = new Student(171230, "์ด๋™ํ˜ธ", korean);
		Student student4 = new Student(171255, "์กฐ์„ฑ์šฑ", korean);
		Student student5 = new Student(171590, "์ตœํƒœํ‰", math);

		goodSchool.addStudent(student1); // goodSchool์— ํ•™์ƒ ์ถ”๊ฐ€
		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);

		dance.register(student1); // ๋ฐฉ์†ก ๋Œ„์Šค๋ฅผ ์ˆ˜๊ฐ•ํ•˜๋Š” ํ•™์ƒ ๋“ฑ๋ก
		dance.register(student2);
		dance.register(student3);
		
		english.register(student3); // ์˜์–ด ๊ณผ๋ชฉ์„ ์ˆ˜๊ฐ•ํ•˜๋Š” ํ•™์ƒ ๋“ฑ๋ก
		english.register(student4);
		english.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(student3, english, 96);

		addScoreForStudent(student4, korean, 89);
		addScoreForStudent(student4, math, 95);
		addScoreForStudent(student4, english, 80);

		addScoreForStudent(student5, korean, 85);
		addScoreForStudent(student5, math, 56);
		addScoreForStudent(student5, english, 55);
		
		addScoreForStudent(student1, dance, 95);
		addScoreForStudent(student2, dance, 85);
		addScoreForStudent(student3, dance, 55);
		
	}

	public void addScoreForStudent(Student student, Subject subject, int point) { // ๊ณผ๋ชฉ๋ณ„ ์ ์ˆ˜๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๋ฉ”์„œ๋“œ
		Score score = new Score(student.getStudentID(), subject, point);
		student.addSubjectScore(score);
	}
}

 

 

PassFailEvaluation.java

package grade;

public class PassFailEvaluation implements GradeEvaluation {

	@Override
	public String getGrade(int point) {
		if (point >= 70)
			return "P";
		else
			return "F";
	}
}

 

 

GenerateGradeReport.java

package school.report;

import java.util.ArrayList;

import grade.BasicEvaluation;
import grade.GradeEvaluation;
import grade.MajorEvaluation;
import grade.PassFailEvaluation;
import school.School;
import school.Score;
import school.Student;
import school.Subject;
import utils.Define;

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); // ํ•™์ƒ๋ณ„ ์ˆ˜๊ฐ• ๊ณผ๋ชฉ ํ•™์  ๊ณ„์‚ฐ

			buffer.append("\n");
			buffer.append(LINE);
		}
	}

	public void getScoreGrade(Student student, Subject Subject) {
		ArrayList<Score> scoreList = student.getScoreList();
		int majorID = student.getMajorSubject().getSubjectID();

		GradeEvaluation[] gradeEvaluation = { new BasicEvaluation(), new MajorEvaluation(), 
				                              new PassFailEvaluation() }; // ํ•™์  ํ‰๊ฐ€ ํด๋ž˜์Šค																								

		for (int i = 0; i < scoreList.size(); i++) { // ํ•™์ƒ์ด ๊ฐ€์ง„ ์ ์ˆ˜๋ฅผ
			Score score = scoreList.get(i);
			if (score.getSubject().getSubjectID() == Subject.getSubjectID()) { // ํ•™์  ์‚ฐ์ถœํ•  ๊ณผ๋ชฉ
				String grade;
				
				if (Subject.getGradeType() == Define.PF_TYPE) { // pass/fail ๋ฐฉ์‹
					grade = gradeEvaluation[Define.PF_TYPE].getGrade(score.getPoint());
				} else {
					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");
	}
}

<๊ฒฐ๊ณผ๊ฐ’>

-๋จธ์•ผ ๋ฐฉ์†ก๋Œ„์Šค ์ ์ˆ˜ ์™œ ์ถœ๋ ฅ์•ˆ๋ผ?? ๋„์ €ํžˆ ์›์ธ์„ ๋ชจ๋ฅด๊ฒ ๋‹ค

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€