โถ ํ์ ์ฐ์ถ ํ๋ก๊ทธ๋จ ๋ง๋ค๊ธฐ
*๋ฌธ์ ์ ์ํ๊ธฐ
โ
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 ํด๋์ค๋ฅผ ์์ฑํ์ฌ ์ฑ์ ๊ณผ ํ์ ์ ์ถ๋ ฅํ๋ค.
โ
โ
<๊ฒฐ๊ณผ๊ฐ>
'Java' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Java-๊ธฐ์ด] ์๊ณ ๋ฆฌ์ฆ ์ค์ต (๋ฒ๋ธ์ ๋ ฌ) (0) | 2021.02.24 |
---|---|
[Java-๊ธฐ์ด] ์ค์ต (ํ์ ์ฐ์ถ ํ๋ก๊ทธ๋จ 2) (0) | 2021.02.24 |
[Java-๊ธฐ์ด] ๊ทธ ์ธ ์ ์ถ๋ ฅ ํด๋์ค (0) | 2021.02.23 |
[Java-๊ธฐ์ด] ์ง๋ ฌํ (0) | 2021.02.23 |
[Java-๊ธฐ์ด] ๋ณด์กฐ ์คํธ๋ฆผ (0) | 2021.02.23 |
๋๊ธ