성적관리->> 함수 쓰고, 클래스 써서

개발/코딩 2013. 9. 25. 18:53

package struct;

import java.util.Scanner;

import function.Record;

public class Program2 {
 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;
 }

 static void inputRecord(Record record){  // 변수3개 모두 리턴으로 넘기지 못하기때문에 클래스를 선언한다.
  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;
  
 }
 
 static void printRecord(Record record){
  int total = record.kor + record.eng + record.math;   //total, avg는 자체적으로 가능하고
  float avg = total / 3.0f;      //kor,eng,math는 서로 공유 하고 있기때문에
  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.kor, record.eng, record.math, total, avg);
  }
  System.out.println("└────┴────┴────┴────┴────┴────┘"); 
 }
 
 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 (inputMenu()) {
   
   case 1:
   inputRecord(record);
    break;

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

public class Record{      //class 선언

int kor;

int eng;

int math;

}