get,set이용해서 성적입력.

개발/코딩 2013. 10. 1. 17:58

==========main 함수========================================================

package oop.capsule;
import java.util.Scanner;

public class Program2{
 public static void main(String[] args) {
  Record record = new Record();
  int total = 0;
  float avg = 0;
  int menu = 0;
  Scanner a = new Scanner(System.in);
  Scanner in = new Scanner(System.in);
  뿅:while (true) {
    
   switch (RecordView.inputMenu()) {
   
   case 1:
    RecordView.inputRecord(record);
    break;

   case 2:
    RecordView.printRecord(record);
    break;
    
   case 3:
    System.out.println("종료됩니다");
    break 뿅;
    
   default:
    System.out.println("입력이 잘못됐습니다. 다시 입력하세요");
   }
  }
 }
}

=====main을 제외한 나머지 함수================================================================

package oop.capsule;
import java.util.Scanner;


public class RecordView {

 public static int inputMenu(){
  int menu;
  Scanner in = new Scanner(System.in);
  System.out.println("┌─────────────────────────────┐");
  System.out.println("│           메인 메뉴             │");
  System.out.println("└─────────────────────────────┘");
  System.out.println("1.성적 입력");
  System.out.println("2.성적 출력");
  System.out.println("3.종료");
  System.out.print("선택>");
  menu = in.nextInt();
  return menu;
 }

 public static void inputRecord(Record record){
  int kor,eng,math;
  Scanner a = new Scanner(System.in);
  System.out.println("┌─────────────────────────────┐");
  System.out.println("│           성적 입력             │");
  System.out.println("└─────────────────────────────┘");

  do {
   System.out.print("국어:");
   kor = a.nextInt();

   if (kor < 0 || kor > 100)
    System.out.println("성적 범위를 벗어났습니다,다시 입력하세요");
  } while (kor < 0 || kor > 100);

  do {
   System.out.print("영어:");
   eng = a.nextInt();

   if (eng < 0 || eng > 100)
    System.out.println("성적 범위를 벗어났습니다,다시 입력하세요");
  } while (eng < 0 || eng > 100);

  do {
   System.out.print("수학:");
   math = a.nextInt();

   if (math < 0 || math > 100)
    System.out.println("성적 범위를 벗어났습니다,다시 입력하세요");
  } while (math < 0 ||math > 100);
  /*  record.kor=kor;
   record.eng=eng;
   record.math=math;  */
  Record.setKor(record, kor);   //Record 함수에 kor값 setting
  Record.setEng(record, eng);
  Record.setMath(record, math);
 }
 
 public static void printRecord(Record record){
  int total = Record.total(record); //다른클래스의 이름을 변경(kor=>kor1)하면 여기에서만 오류 날 수 있도록
  float avg = total / 3.0f;    
  System.out.println("┌─────────────────────────────┐");
  System.out.println("│           성적 출력              │");
  System.out.println("├────┬────┬────┬────┬────┬────┤");
  System.out.println("│ 번호 │ 국어 │ 영어 │ 수학 │ 총점 │ 평균 │");
  for (int i = 0; i < 3; i++) {
   System.out.println("├────┼────┼────┼────┼────┼────┤");
   System.out.printf("│%3d │%3d │%3d │%3d │%3d │%6.2f │\n",i + 1,
 Record.getKor(record), Record.getEng(record),Record.getMath(record), total, avg);
  }       //Racord 클래스에 과목을 얻어주라는 뜻.get
  System.out.println("└────┴────┴────┴────┴────┴────┘"); 
 }
}
 

===============Record class (get,set 을 이용하여)==========================

(코드의 캡슐화) -> 궁극적인 목적은 데이터에 대한 보호,외부접근 제한등을 위한것.

    접근을 금지하는것이 "정보 은닉화"

package oop.capsule;

public class Record { // 함수안에 선언한게 아니라서 값을 넣는 공간아님.
      // (함수안에있는 것만 선언한것. class에는 정의한것.)
 int kor;
 int eng;
 int math;
 
  /* 데이터가 중첩될수도, 안될수도 있지만 구조변경에서 자유로울 수 있어야한다. 따라서 구조를 변경하기 위해서 함수를 이용함.
  * (구조를 변경했을 때 문제가 되는것은 다 Record 클래스 안에 있음) */

 public static int total(Record record)
 // 데이터변경을 자유롭게하기위해 total함수를 만들어서 사용.
 {
  return (record.kor + record.eng + record.math);
 }

 public static int getKor(Record record) {
  return record.kor;
 }

 public static void setKor(Record record, int kor) {
  record.kor = kor;
 }

 public static int getEng(Record record) {
  return record.eng;
 }

 public static void setEng(Record record, int eng) {
  record.eng = eng;
 }

 public static int getMath(Record record) {
  return record.math;
 }

 public static void setMath(Record record, int math) {
  record.math = math;
 }
}