자바실전_Day_17_01(클래스 - 학생정보출력 - 리팩토링)
-----------------------------------------------------------------------------------
1번째 예제
●결과값
public class Student {
//#1. 필드
private int number; //학번
private String name; //이름
private String[]subjectName = {"국어","영어","수학"}; //과목종류
private int[] subjectScore; //과목점수
private int allScore; //총점
private double avg; //평균
//합치고 쪼개는 기준
//사용이 많으면 따로 빼는것이 좋다.
//#2. 생성자
public Student(int number, String name, int[] subjectScore) {
super();
this.number = number;
this.name = name;
this.subjectScore = subjectScore;
}
//#3. getter, setter
//#4. 메소드
public void printSubjectInfo() {
System.out.println("*** "+name+"님 수강과목과 점수 확인 ***");
System.out.println("수강과목 점수");
for(int i = 0 ; i < subjectScore.length; i++) { //subjectScore 점수를 입력하면, 과목만큼 호출한다.
allScore += subjectScore[i];//총점 계산
if(subjectScore[i]>-1) { //0부터 입력된다.
System.out.println(subjectName[i]+"\t\t"+subjectScore[i]+"점");
}//end of if
}//end of for
avg = (double)allScore/subjectScore.length;
System.out.println();
System.out.println("*** "+name+" 님 성적 ***" );
System.out.println("학 번 : " + number);
System.out.printf("총 점 : %s점\n" ,allScore);
System.out.printf("평 균 : %.2f점",avg);
}//end of printSubjectInfo
}
public class StudentMain {
public static void main(String[] args) {
int[] score = {30,40,60}; // 국어,영어,수학 점수입력
Student student1 = new Student(201301001,"홍길동",score); //(학번,이름,점수(배열))
student1.printSubjectInfo();//객체생성후 클래스에서 메소드 처리
}
}
-----------------------------------------------------------------------------------
1번째 예제 - 다른 방
●결과값
public class Student {
//#1.필드 - 인스턴스 필드,
String name; //이름
String ssn; //학번
int kor; //국어
int eng; //영어
int mat; //수학
int sum; //합계
double avg; //평균
//#2.생성자
public Student(String name, String ssn, int kor, int eng, int mat) {
this.name = name;
this.ssn = ssn;
this.kor = kor;
this.eng = eng;
this.mat = mat;
}
//#3. 성적 계산 및 과목별 점수 출력 메소드
public void scoreCalc() {
sum = kor + eng + mat;
avg = (double)sum/3;
System.out.println("*** " + name + "님 수강과목과 점수 확인 ***" );
System.out.println("수강과목\t 점수");
System.out.println("국어\t " + kor);
System.out.println("영어\t " + eng);
System.out.println("수학\t " + mat);
}//end of scoreCalck()
//#4. 성적 출력 메소드
public void studentInfo() {
System.out.println("*** "+ name + "님 성적 ***");
System.out.println("학번 : " + ssn);
System.out.println("총점 : " + sum);
System.out.println("평균 : " + avg);
}
}
public class StudentMain {
public static void main(String[] args) {
//변경전
Student hong = new Student("홍길동", "1234", 50, 80, 70);
hong.scoreCalc();
System.out.println("-----------------------");
hong.studentInfo();
System.out.println();
Student kim = new Student("김자바", "4567", 55, 96, 95);
hong.scoreCalc();
System.out.println("-----------------------");
hong.studentInfo();
//변경후
Student[] std = {
new Student("홍길동", "1234", 50, 80, 70),
new Student("김자바", "4567", 55, 96, 95),
new Student("이강산", "5678", 95, 100, 98)
};
/*
//변경전
//for문 이용
for(int i = 0 ; i <std.length; i++) {
std[i].scoreCalc();
System.out.println("--------------------");
std[i].studentInfo();
System.out.println("--------------------");
}
*/
//변경후
//향산된 for문(for each문)
for(Student sarry : std) {
sarry.scoreCalc();
System.out.println("--------------------");
sarry.studentInfo();
System.out.println("--------------------");
}
}
}