abstract
2022. 6. 4. 17:50ㆍPrograming Language/Java
# main abstract package [program.java]
package ex2.abstr;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Scanner;
import ex2.abstr.data.NewlecExam;
import ex2.abstr.ui.NewlecExamConsole;
public class program {
public static void main(String[] args) throws IOException {
// Exam exam1 = new Exam(); <- Exam 클래스가 abstract 이므로 Exam 의 객체생성 불가능함
//Exam exam1 = new NewlecExam(1,1,1,1); // <- 자식 클래스의 객체를 통해 Exam 에 접근해야됨
//int total = exam1.total();
//double avg = exam1.avg();
//System.out.println(total);
//System.out.println(avg);
// NewlecExam exam = new NewlecExam(1,1,1,1);
// int total = exam.total();
// double avg = exam.avg();
// System.out.println(total);
// System.out.println(avg);
NewlecExam exam = new NewlecExam(); // exam 클래스가 추상형이라서 exam 객체 생성이 불가능 하여 자식클래스인 NewlecExam 으로 객체선언
NewlecExamConsole console = new NewlecExamConsole(); // NewlecExam 기업에서는 컴퓨터 과목이 추가되기 때문에 NewlecExam 의 전용 출력 용 인 NewlecExamConsole 클래스 객체 생성
console.setExam(exam);
console.input();
console.print();
}
}
# abstract.entitiy package [Exam.java & NewlecExam]
package ex2.abstr.data;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Scanner;
public abstract class Exam { // 뼈대생성
// 그룹핑
private int kor;
private int eng;
private int math;
// 생성자
public Exam() {
this(1,1,1);
}
public Exam(int kor, int eng, int math) {
this.kor = kor;
this.eng = eng;
this.math = math;
}
public void load() throws IOException {
File file = new File("res/data.txt");
FileInputStream fis = new FileInputStream(file);
Scanner scan = new Scanner(fis);
String[] tmps = scan.nextLine().split(",");
scan.close();
fis.close();
kor = Integer.parseInt(tmps[0]);
eng = Integer.parseInt(tmps[1]);
math = Integer.parseInt(tmps[2]);
}
public void save() throws IOException {
File file = new File("res/data.txt");
FileOutputStream fos = new FileOutputStream(file);
PrintStream ps = new PrintStream(fos);
ps.printf("$d,%d,%d\n", kor, eng, math);
ps.close();
fos.close();
}
public int total() {
return kor+eng+math;
}
public abstract double avg();
// 내가 지금 구현할게 없어서 몸통을 지웠는데 하지만
// 목록만큼은 공통분모로 있어야 되기 때문에 추상형으로 했다
// 자식은 무조건 이것을 오버라이드하여 구현 해야된다 [가업을 이어라]
public void setKor(int kor) {
this.kor = kor;
}
public int getKor() {
return kor;
}
public void setEng(int eng) {
this.eng = eng;
}
public int getEng() {
return eng;
}
public void setMath(int math) {
this.math = math;
}
public int getMath() {
return math;
}
}
package ex2.abstr.data;
public class NewlecExam extends Exam {
private int com;
public void setCom(int com) {
this.com = com;
}
public int getCom() {
return com;
}
// public void setCom(int com) {
// this.com = com;
// }
public NewlecExam() {
this(0,0,0,0);
}
public NewlecExam(int kor, int eng, int math, int computer) {
super(kor, eng, math);
this.com = com;
}
@Override
public int total() {
return super.total()+com; // Exam 의 total 함수 호출
// kor+eng+math+com
}
@Override
public double avg() {
return total() / 4.0;
}
}
# abstract.ui package [ExamConsole.java & NewlecExamConsole.java]
package ex2.abstr.ui;
import java.util.Scanner;
import ex2.abstr.data.Exam;
public abstract class ExamConsole {
private Exam exam;
public ExamConsole() {
// 1. Compositon Has A 관계
// this.exam = new Exam();
}
public ExamConsole(Exam exam) {
this.exam = exam;
}
public Exam getExam() {
return exam;
}
//1. Association Has A 관계
public void setExam(Exam exam) {
this.exam = exam;
}
public void print() {
Scanner scan = new Scanner(System.in);
System.out.println("┌───────────────────────────────────┐");
System.out.println("│ 성적 출력 │");
System.out.println("└───────────────────────────────────┘");
System.out.println("국어 : " + exam.getKor());
System.out.println("영어 : " + exam.getEng());
System.out.println("수학 : " + exam.getMath());
onPrint();
int total = this.exam.total();
double avg = this.exam.avg();
System.out.println("총점 :" + total);
System.out.printf("평균 : %.2f\n", avg);
}
protected abstract void onPrint();
public void input() {
Scanner scan = new Scanner(System.in);
System.out.println("┌────────────────────────────────┐");
System.out.println("│ 성적 입력 │");
System.out.println("└────────────────────────────────┘");
System.out.print("국어 >");
int kor = scan.nextInt();
System.out.print("영어 >");
int eng = scan.nextInt();
System.out.print("수학 >");
int math = scan.nextInt();
exam.setKor(kor);
exam.setEng(eng);
exam.setMath(math);
}
}
package ex2.abstr.ui;
import java.util.Scanner;
import ex2.abstr.data.NewlecExam;
public class NewlecExamConsole extends ExamConsole {
@Override
protected void onPrint() {
NewlecExam exam = (NewlecExam)getExam();
System.out.println("컴퓨터 :" + exam.getCom());
}
@Override
public void input() {
super.input();
Scanner scan = new Scanner(System.in);
System.out.print("컴퓨터 > ");
int com = scan.nextInt();
NewlecExam exam = (NewlecExam)getExam();
exam.setCom(com);
}
}
'Programing Language > Java' 카테고리의 다른 글
captulation (0) | 2022.05.29 |
---|---|
데이터구조화 (0) | 2022.05.28 |
File (0) | 2022.05.28 |