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

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

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

โ–ถ ํ•™์  ์‚ฐ์ถœ ํ”„๋กœ๊ทธ๋žจ ๋งŒ๋“ค๊ธฐ

 

*๋ฌธ์ œ ์ •์˜ํ•˜๊ธฐ

โ€‹

Student.java

package school;

import java.util.ArrayList;

public class Student {
	private int studentID;        // ํ•™๋ฒˆ
	private String studentName;   // ์ด๋ฆ„
	private Subject majorSubject; // ํ•„์ˆ˜ ๊ณผ๋ชฉ

	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;
	}
}

-์ ์ˆ˜ ๋ฆฌ์ŠคํŠธ๋Š” ๋ฐฐ์—ด๋กœ ๊ด€๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ArrayList๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

โ€‹

โ€‹

Subject.java

package school;

import java.util.ArrayList;
import utils.Define;

public class Subject {
	private String subjectName;
	private int subjectID;
	private int gradeType;

	private ArrayList<Student> studentList = new ArrayList<Student>();

	public Subject(String subjectName, int subjectID) {
		this.subjectName = subjectName;
		this.subjectID = subjectID;
		this.gradeType = Define.AB_TYPE;
	}

	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 int getGradeType() {
		return gradeType;
	}

	public void setGradeType(int gradeType) {
		this.gradeType = gradeType;
	}

	public ArrayList<Student> getStudentList() {
		return studentList;
	}

	public void setStudentList(ArrayList<Student> studentList) {
		this.studentList = studentList;
	}

	public void register(Student student) {
		studentList.add(student);
	}
}

 

 

Score.java

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;
	}
}

 

 

Define.java

package utils;

public class Define {
	public static final int KOREAN = 1001;    // ๊ตญ์–ด
	public static final int MATH = 2001;      // ์ˆ˜ํ•™
	
	public static final int AB_TYPE = 0;      // ์ผ๋ฐ˜ ๊ณผ๋ชฉ ํ•™์  ์‚ฐ์ถœ ์ •์ฑ…
	public static final int SAB_TYPE = 1;     // ํ•„์ˆ˜ ๊ณผ๋ชฉ ํ•™์  ์‚ฐ์ถœ ์ •์ฑ…
}

 

 

GradeEvaluation.java

package grade;

public interface GradeEvaluation {
	public String getGrade(int point); // ์ ์ˆ˜๊ฐ€ ์žˆ์œผ๋ฉด  ๊ทธ ์ ์ˆ˜์— ๋”ฐ๋ฅธ ํ•™์ ์„ ๋ฌธ์ž์—ด๋กœ ๋ฐ˜ํ™˜ํ•ด์คŒ
}	

 

 

BasicEvaluation.java

package grade;

public class BasicEvaluation implements GradeEvaluation {
	
	@Override
	public String getGrade(int point) {
		String grade;

		if (point >= 90 && point <= 100)
			grade = "A";
		else if (point >= 80)
			grade = "B";
		else if (point >= 70)
			grade = "C";
		else if (point >= 55)
			grade = "D";
		else
			grade = "F";
		return grade;
	}
}

 

 

MajorEvaluation.java

package 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)
			grade = new String("A");
		else if (point >= 80)
			grade = "B";
		else if (point >= 70)
			grade = "C";
		else if (point >= 60)
			grade = "D";
		else
			grade = "F";
		return grade;
	}
}

 

 

GenerateGradeReport.java

package school.report;

import java.util.ArrayList;

import grade.BasicEvaluation;
import grade.GradeEvaluation;
import grade.MajorEvaluation;
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.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");
	}
}

-ํ•™์ ์„ ์‚ฐ์ถœํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ํด๋ž˜์Šค๋ฅผ ๋ฐฐ์—ด๋กœ ์ •์˜ํ–ˆ๋‹ค.

 

 

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

-ํ•™์  ํ‰๊ฐ€ ํด๋ž˜์Šค๋Š” GradeEvaluation ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜์˜€์œผ๋ฏ€๋กœ GradeEvaluationํ˜•์œผ๋กœ ์„ ์–ธํ•ด ๋‘๊ณ  ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•œ ํ›„ ํ•„์š”ํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

 

 

if (score.getSubject().getSubjectID() == majorID) // ํ•„์ˆ˜ ๊ณผ๋ชฉ์ธ ๊ฒฝ์šฐ
	grade = gradeEvaluation[Define.SAB_TYPE].getGrade(score.getPoint()); // ํ•„์ˆ˜ ๊ณผ๋ชฉ ํ•™์  ํ‰๊ฐ€ ๋ฐฉ๋ฒ•

-ํ•™์ ์„ ๊ณ„์‚ฐํ•˜๋ ค๋Š” ์ ์ˆ˜์˜ ๊ณผ๋ชฉ์ด ํ•„์ˆ˜์ผ ๊ฒฝ์šฐ Define.SAB_TYPE ๊ฐ’์„ ๋ฐฐ์—ด์— ๋„ฃ๋Š”๋‹ค. ๊ทธ๋Ÿฌ๋ฉด MajorEvaluation( ) ๋ฉ”์„œ๋“œ ๊ณ„์‚ฐ ๋ฐฉ๋ฒ•์ด ์ ์šฉ๋œ๋‹ค.

 

 

 

*ํ”„๋กœ๊ทธ๋žจ ํ…Œ์ŠคํŠธํ•˜๊ธฐ

โ€‹

School.java

package school;

import java.util.ArrayList;

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;
	}
}

-ํ•™๊ต๋Š” ์œ ์ผํ•œ ๊ฐ์ฒด์ด๋ฏ€๋กœ '06-5 static ์‘์šฉ'์—์„œ ๋ฐฐ์šด ์‹ฑ๊ธ€ํ†ค ํŒจํ„ด์œผ๋กœ ๊ตฌํ˜„ํ•œ๋‹ค.

โ€‹

โ€‹

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;

	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);

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

	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);
		
		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);
	}
}

-ํ•™์ƒ, ๊ณผ๋ชฉ, ์ ์ˆ˜ ๋“ฑ์„ ๊ฐ๊ฐ ์ƒ์„ฑํ•˜๊ณ  report ํด๋ž˜์Šค๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ์„ฑ์ ๊ณผ ํ•™์ ์„ ์ถœ๋ ฅํ•œ๋‹ค.

โ€‹

โ€‹

<๊ฒฐ๊ณผ๊ฐ’>

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€