package oop.ex04.UI코드분리하기;
import java.util.Scanner;
public class ExamConsole {
//Composition Has A 일체형
private ExamList list;// = new ExamList();
public ExamConsole() {
list = new ExamList();
}
public void inputList() {
Scanner scan = new Scanner(System.in);
System.out.println("┌────────────────────────┐");
System.out.println(" │ 성적입력 │");
System.out.println("└────────────────────────┘");
System.out.println();
int kor, eng, math;
System.out.printf("%d번 학생\n", list.size() + 1);
do {
System.out.printf("국어 : ");
kor = scan.nextInt();
if (kor < 0 || 100 < kor)
System.out.println("입력오류 : 성적유효범위에 맞게 입력하세요.");
} while (kor < 0 || 100 < kor);
do {
System.out.printf("영어 : ");
eng = scan.nextInt();
if (eng < 0 || 100 < eng)
System.out.println("입력오류 : 성적유효범위에 맞게 입력하세요.");
} while (eng < 0 || 100 < eng);
do {
System.out.printf("수학 : ");
math = scan.nextInt();
if (math < 0 || 100 < math)
System.out.println("입력오류 : 성적유효범위에 맞게 입력하세요.");
} while (math < 0 || 100 < math);
Exam exam = new Exam(kor, eng, math);
/*---add() 입력받은 값을 추가------------------*/
list.add(exam);
}// inputList()
public void printList() {
this.printList(list.size());
}//printList
public void printList(int count) {
System.out.println("┌────────────────────────┐");
System.out.println(" │ 성적출력 │");
System.out.println("└────────────────────────┘");
System.out.println();
for(int i=0; i<count; i++) {
Exam exam =list.get(i);
int kor = exam.getKor();//exam.kor;
int eng = exam.getEng();
int math = exam.getMath();
int total = exam.total();//kor + eng + math; exam에서 하는게 바람직
float avg = exam.avg();//total/3.0f;
System.out.printf("%d번 학생\n",i+1);
System.out.printf("\t국어 : %3d\n", kor);
System.out.printf("\t영어 : %3d\n", eng);
System.out.printf("\t수학 : %3d\n", math);
System.out.println("-----------------------");
System.out.printf("\t총점 : %3d\n",total);
System.out.printf("\t평균 : %6.2f\n",avg);
System.out.println("────────────────────────");
}
}
}
package oop.ex04.UI코드분리하기;
public class ExamList {
private Exam[] exams;
private int current;
public Exam get(int i) {
return this.exams[i];
}
public void add(Exam exam) {
Exam[] exams = this.exams;
int count = this.current;
if (exams.length == current) {
Exam[] temp = new Exam[exams.length + 5];
for (int i = 0; i < count; i++)
temp[i] = exams[i];
exams = temp;
}
exams[current] = exam;
current++;
System.out.println("────────────────────");
}
public ExamList() {
//Aggregation Has A
exams = new Exam[3];
current =0;
}
public int size() {
return current;
}
}
ExamList 클래스에서 inputList(), printList()함수에서 입출력 기능이 아닌 부분을 따로 분리했다.
inputList()에는 사용자에게 입력을 받는 기능만 남기고 Exam리스트에 입력받은 값을 추가 해주는 기능을 add()로 분리하고, printList()에는 출력해주는 기능을 남기고, 입력받아서 추가해 놓았던 값들을 가져오는 기능을 get()로 분리했다.
UI부분은 따로 분리해서 만들어줘야 콘솔외에 입출력시에 변경해도 다른 코드에 오류가 가지 않는다.
'프로그래밍언어 > JAVA' 카테고리의 다른 글
[JAVA] GUI클라이언트 EXE(실행파일)로 만들기(JSMOOTH) (0) | 2022.04.21 |
---|---|
[JAVA] class 재사용하기 (0) | 2021.11.28 |
[JAVA] 캡슐화 - 국영수 성적 입출력 프로그램 (0) | 2021.11.28 |
[JAVA] 함수 Overloading - 국영수 성적 입출력 프로그램 (0) | 2021.11.25 |
[JAVA] 구조체 배열 가변 길이 추가 - 국영수 성적 입출력 프로그램 (0) | 2021.11.24 |