Grade.txt 파일
Main.java
public class Main {
public static void main(String[] args) throws Exception {
// 학생수
int STUDENT_SIZE = 20;
// 학생의 점수를 관리하는 클래스 선언
StudentManager studentManager = new StudentManager(STUDENT_SIZE);
// 파일 경로 세팅
studentManager.setFilePath("D:/Grade.txt");
// 학생 학점 출력
studentManager.printGrades();
}
}
Student.java
/**
* 학생 클래스
*/
public class Student {
// 학번
private String studentId;
// 중간고사 성적
private int midExamScore;
// 기말고사 성적
private int finExamScore;
// 리포트 점수
private int repScore;
// 출석 점수
private int attScore;
// 총점
private int totalScore;
// 평균
private float average;
// 학점
private String grade;
public String getStudentId() {
return studentId;
}
public void setStudentId(String studentId) {
this.studentId = studentId;
}
public int getMidExamScore() {
return midExamScore;
}
public void setMidExamScore(int midExamScore) {
this.midExamScore = midExamScore;
}
public int getFinExamScore() {
return finExamScore;
}
public void setFinExamScore(int finExamScore) {
this.finExamScore = finExamScore;
}
public int getRepScore() {
return repScore;
}
public void setRepScore(int repScore) {
this.repScore = repScore;
}
public int getAttScore() {
return attScore;
}
public void setAttScore(int attScore) {
this.attScore = attScore;
}
public int getTotalScore() {
return totalScore;
}
public void setTotalScore(int totalScore) {
this.totalScore = totalScore;
}
public float getAverage() {
return average;
}
public void setAverage(float average) {
this.average = average;
}
public String getGrade() {
return grade;
}
public void setGrade(String grade) {
this.grade = grade;
}
}
StudentManager.java
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
public class StudentManager {
// 학점 A
private static final String CONST_GRADE_A = "A";
// 학점 B
private static final String CONST_GRADE_B = "B";
// 학점 C
private static final String CONST_GRADE_C = "C";
// 학점 D
private static final String CONST_GRADE_D = "D";
// 학점 F
private static final String CONST_GRADE_F = "F";
private Student[] students;
public StudentManager(int size) {
students = new Student[size];
}
/**
* @param string * @throws Exception * 파일경로를 입력받아 받는 메소드
*/
public void setFilePath(String path) throws Exception {
try {
// 파일을 읽음
BufferedReader br = new BufferedReader(new FileReader(new File(path)));
String line = null;
int index = 0;
// 파일의 한 라인씩 가져와서 Student 객체에 담음.
while ((line = br.readLine()) != null) {
Student student = new Student();
String[] infos = line.split("\t");
student.setStudentId(infos[0]);
student.setMidExamScore(Integer.parseInt(infos[1]));
student.setFinExamScore(Integer.parseInt(infos[2]));
student.setRepScore(Integer.parseInt(infos[3]));
student.setAttScore(Integer.parseInt(infos[4]));
student.setTotalScore(student.getMidExamScore() + student.getFinExamScore() + student.getRepScore() + student.getAttScore());
student.setAverage(student.getTotalScore() / 4.0f);
students[index] = student;
index++;
}
br.close();
} catch (FileNotFoundException e) {
throw new Exception("해당 경로에서 파일을 찾을 수 없습니다.");
}
}
/**
* 학점을 출력하는 메소드
*/
public void printGrades() {
calcGrades();
System.out.println("=================================================== 학 점 출 력 ====================================================");
System.out.println("학번\t\t중간성적\t기말성적\t리포트점수\t출석점수\t총점\t\t평균\t\t학점");
System.out.println("--------------------------------------------------------------------------------------------------------------------");
for (int i = 0; i < students.length; i++) {
System.out.printf("%s\t%s\t\t%s\t\t%s\t\t%s\t\t%s\t\t%s\t\t%s\t\t \n", students[i].getStudentId(), students[i].getMidExamScore(), students[i].getFinExamScore(), students[i].getRepScore(), students[i].getAttScore(), students[i].getTotalScore(), students[i].getAverage(), students[i].getGrade());
}
System.out.println("====================================================================================================================");
}
/**
* 학점을 계산하는 메소드
*/
private void calcGrades() {
// 전체학생의 평균
float allStudentaverage = 0;
// 전체학생의 총점
float allSum = 0;
// 전체 학생의 평균 계산
for (int i = 0; i < students.length; i++) {
allSum += students[i].getAverage();
}
System.out.println(allStudentaverage);
// 학점을 계산
for (int i = 0; i < students.length; i++) {
// 전체평균보다 20% 높으면 A
if (students[i].getAverage() >= allStudentaverage * 1.2) {
students[i].setGrade(CONST_GRADE_A);
// 전체평균보다 높지만 전체평균보다 20%높지는 않다면 B
} else if ((allStudentaverage * 1.2) > students[i].getAverage() && students[i].getAverage() >= allStudentaverage) {
students[i].setGrade(CONST_GRADE_B);
// 전체평균이하이지만 평균보다 20%까지는 C
} else if (students[i].getAverage() < allStudentaverage && students[i].getAverage() >= allStudentaverage * 0.8) {
students[i].setGrade(CONST_GRADE_C);
// A,B,C 학점을 받지 못하고 평균보다 40% 까지는 D
} else if (students[i].getAverage() < allStudentaverage * 0.8 && students[i].getAverage() >= allStudentaverage * 0.6) {
students[i].setGrade(CONST_GRADE_D);
// 그 외의 학점
} else students[i].setGrade(CONST_GRADE_F);
}
}
}
결과