class Test {
private String exam;
private Student[] students;
public Test(String exam, Student...students) {
setExam(exam);
setStudents(students);
}
public int subSum(String subName) {
int sum = 0;
for (Student student : students) {
int score = student.findSubScore(subName);
if(score > 0) {
sum += score;
}
}
return sum;
}
public int subAvg(String subName) {
return subSum(subName) / students.length;
}
@Override
public String toString() {
String info = "<< " + exam + " >>\n";
for(Student student : students) {
info += student.toString() + "\n";
}
Subject[] scores = students[0].getSubjects();
for(Subject subject : scores) {
String name = subject.getName();
info += name + " [과목 총합: " + subSum(name) + "]\n";
info += name + " [과목 총평균: " + subAvg(name) + "]\n";
}
return info;
}
public String getExam() {
return exam;
}
public Student[] getStudents() {
return students;
}
public void setExam(String exam) {
this.exam = exam;
}
public void setStudents(Student[] students) {
this.students = students;
}
}
class Student {
private String stuName;
private Subject[] subjects;
public Student(String stuName, Subject ... subjects) {
setSubjects(subjects);
setStuName(stuName);
}
public String total(Subject[] subjects) {
int sum = 0;
for(Subject sub : subjects) {
sum += sub.getScore();
}
String info = "[과목 총합: " + sum + "]\n";
info += "[과목 총평균: " + sum / subjects.length + "]\n\n";
return info;
}
public int findSubScore(String name) {
Subject subName = new Subject(name);
int score = -1;
for(Subject subject : subjects) {
if(subject.equals(subName)) {
score = subject.getScore();
}
}
return score;
}
@Override
public String toString() {
String info = "< " + stuName + " >\n";
for(Subject sub : subjects) {
info += sub;
}
info += total(subjects);
return info;
}
public String getStuName() {
return stuName;
}
public Subject[] getSubjects() {
return subjects;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public void setSubjects(Subject[] subjects) {
this.subjects = subjects;
}
}
class Subject {
private String name;
private int score;
public Subject(String name, int score) {
setName(name);
setScore(score);
}
public Subject(String name) {
setName(name);
}
@Override
public String toString() {
String info = name + ": " + score + "\n";
return info;
}
@Override
public boolean equals(Object o) {
if(o == null || !(o instanceof Subject)) {
return false;
}
Subject temp = (Subject) o;
return name.equals(temp.getName());
}
public int getScore() {
return score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setScore(int score) {
this.score = score;
}
}
public class TestResult {
public static void main(String[] args) {
Student in = new Student(
"박정인",
new Subject("국어", 100),
new Subject("수학", 90),
new Subject("영어", 95)
);
Student min = new Student(
"박정민",
new Subject("국어", 100),
new Subject("수학", 80),
new Subject("영어", 75)
);
Student go = new Student(
"박정고",
new Subject("국어", 70),
new Subject("수학", 80),
new Subject("영어", 75)
);
Test test = new Test("중간고사", in, min, go);
System.out.println(test);
}
}
[결과값]
<< 중간고사 >>
< 박정인 >
국어: 100
수학: 90
영어: 95
[과목 총합: 285]
[과목 총평균: 95]
< 박정민 >
국어: 100
수학: 80
영어: 75
[과목 총합: 255]
[과목 총평균: 85]
< 박정고 >
국어: 70
수학: 80
영어: 75
[과목 총합: 225]
[과목 총평균: 75]
국어 [총 과목 합계: 270]
국어 [총 과목 평균: 90]
수학 [총 과목 합계: 250]
수학 [총 과목 평균: 83]
영어 [총 과목 합계: 245]
영어 [총 과목 평균: 81]