본문 바로가기
컴퓨터/java

2017/7/25 java abstract

by 싱판다 2017. 7. 25.

Exam.java과 ExamConsole.java는 뼈대...

새로운 프로젝트를 생성하고 Build path -> Configure Build Path.. 을 눌러 Exam과 ExamConsole이 들어있는 프로젝트를 더해줬다.


Exam.java

package com.newlecture.shinpanda.ems.entity;


public abstract class Exam {

private int kor;

private int eng;

private int math;

public Exam(){

this(0, 0, 0);

}

public Exam(int kor, int eng, int math){

this.kor = kor;

this.eng = eng;

this.math = math;

}

public abstract double avg();

public int total() {

// TODO Auto-generated method stub

return kor + eng + math;

}

public int getKor() {

return kor;

}

public void setKor(int kor) {

this.kor = kor;

}

public int getEng() {

return eng;

}

public void setEng(int eng) {

this.eng = eng;

}

public int getMath() {

return math;

}

public void setMath(int math) {

this.math = math;

}

}


Exam을 상속받은 새로운 프로젝트의 NewlecExam.java를 생성해줬다.

- 컴퓨터 과목을 받는 부분을 새롭게 추가

import com.newlecture.shinpanda.ems.entity.Exam;


public class NewlecExam extends Exam {

private int com;


public NewlecExam() {

this(0,0,0,0);

}

public NewlecExam(int kor, int eng, int math, int com) {

super(kor, eng, math);

this.com = com;

}

public int getCom() {

return com;

}


public void setCom(int com) {

this.com = com;

}


@Override

public int total() {

// TODO Auto-generated method stub

return super.total()+com;

}


@Override

public double avg() {

// TODO Auto-generated method stub

return (double)total()/4.0;

}

}




이제 입출력을 담당하는 콘솔 부분.

ExamConsole.java

package com.newlecture.shinpanda.ems.ui;


import java.util.Scanner;


import com.newlecture.shinpanda.ems.entity.Exam;


public abstract class ExamConsole {

private Exam exam = newExam(); 

// 컴파일러가 생성하는 부분을 생성자로 이동시켜줌

public abstract Exam newExam();

/*public ExamConsole(Exam exam) {

this.exam = exam;

}*/

public void input() {

System.out.print("┌─────────────────┐\n");

System.out.print("│                   성 적 입 력                   │\n");

System.out.print("└─────────────────┘\n");

Scanner scan = new Scanner(System.in);

int kor =0;

int eng =0;

int math =0;

do {

if(kor<0 || kor > 100) {

System.out.println("성적 값을 잘못 입력하셨습니다. 범위: 0~100");

}

System.out.print("국어 : ");

kor = scan.nextInt();


}while(kor<0 || kor>100);

do {

if(eng<0 || eng > 100) {

System.out.println("성적 값을 잘못 입력하셨습니다. 범위: 0~100");

}

System.out.print("영어 : ");

eng = scan.nextInt();

}while(eng<0 || eng>100);

do {

if(math<0 || math > 100) {

System.out.println("성적 값을 잘못 입력하셨습니다. 범위: 0~100");

}

System.out.print("수학 : ");

math = scan.nextInt();

}while(math<0 || math>100);

//onInput();

exam.setKor(kor);

exam.setEng(eng);

exam.setMath(math);


}

//protected abstract void onInput();



public void print() {

System.out.print("┌─────────────────┐\n");

System.out.print("│                   성 적 출 력                   │\n");

System.out.print("└─────────────────┘\n");

int kor = exam.getKor();

int eng = exam.getEng();

int math = exam.getMath();

int total;

double avg;


total = exam.total();//kor + eng + math;

avg = exam.avg();//(double)(total / 3.0);

System.out.printf("국어 : %3d\n", kor);

System.out.printf("영어 : %3d\n", eng); // 무조건 10자리

System.out.printf("수학 : %3d\n", math);

onPrint();

//System.out.printf("총점 : %d %d %1$d\n", total, 22);

System.out.printf("총점 : %d\n", total);

System.out.printf("평균 : %6.2"+ "f\n", avg); // 소수점도 포함되므로 100자리가 처리가 안되서 6.2로 변환

//System.out.printf("평균 : %5.2e\n", avg);//소수점 2자리 까지만

//System.out.printf("평균 : %s\n", "Hi");// 문자열에도 소수점 넣는 거 적용 가능

System.out.print("───────────────────────────────────────────────────\n");

}



protected abstract void onPrint();

}



ExamConsole.java를 기반으로 한 NewlecExamConsole.java 생성

컴퓨터 과목이 추가되었기 때문에 이를 입력받고 출력하는 부분이 필요.

NewlecExamConsole.java

import java.util.Scanner;


import com.newlecture.shinpanda.ems.entity.Exam;

import com.newlecture.shinpanda.ems.ui.ExamConsole;


public class NewlecExamConsole extends ExamConsole{

NewlecExam newexam;


@Override

public Exam newExam() {

newexam = new NewlecExam();

return newexam;

}

@Override

public void input() {

// TODO Auto-generated method stub

super.input();

Scanner scan = new Scanner(System.in);

int com = 0;

do {

if(com<0 || com > 100) {

System.out.println("성적 값을 잘못 입력하셨습니다. 범위: 0~100");

}

System.out.print("컴퓨터 : ");

com = scan.nextInt();

}while(com<0 || com>100);

newexam.setCom(com);

//((NewlecExam)exam).setCom(com);

}


/* @Override

protected void onInput() {

Scanner scan = new Scanner(System.in);

int com = 0;

do {

if(com<0 || com > 100) {

System.out.println("성적 값을 잘못 입력하셨습니다. 범위: 0~100");

}

System.out.print("컴퓨터 : ");

com = scan.nextInt();

}while(com<0 || com>100);

newexam.setCom(com);

//((NewlecExam)exam).setCom(com);

}*/



@Override

protected void onPrint() {

// TODO Auto-generated method stub

int com = newexam.getCom();

System.out.printf("컴퓨터 : %3d\n", com);

}

}



메인부분

NewlecMain.java

import java.util.Scanner;


public class NewlecMain {


public static void main(String[] args) {

NewlecExamConsole console = new NewlecExamConsole();

int menu;

EXIT:

while(true) {

menu = inputMenu();

switch(menu) {

case 1:

console.input();

break;

case 2:

console.print();

break;

case 3:

break EXIT;

default:

System.out.println("잘못된 입력입니다. 번호 : 1~3");

break;

}

}

}

public static int inputMenu() {

Scanner scan = new Scanner(System.in);

System.out.print("┌─────────────────┐\n");

System.out.print("│                   메 인 메 뉴                   │\n");

System.out.print("└─────────────────┘\n");

System.out.println("1.성적 입력");

System.out.println("2.성적 출력");

System.out.println("3.종료");

System.out.print("선택 > ");

return scan.nextInt();

}

}









'컴퓨터 > java' 카테고리의 다른 글

람다식  (0) 2017.07.27
자바 local 클래스, 익명 클래스  (0) 2017.07.27
자바 inner 클래스  (0) 2017.07.27
이차열배열 복사, 행 이동  (0) 2017.07.24
7/20  (0) 2017.07.20

댓글