2008. 9. 11. 18:18

[기타] 성적 처리 프로그램 - 포스팅 중

학생의 번호, 이름, 국어, 영어, 수학 점수를 입력 받아 총점, 평균, 등급(A~F), 랭킹을 구하시오.
설계 과정
1. 기본값 출력 테스트
각 필드의 변수명을 정하고 출력 되는지 확인한다.
2. 변수 입력 받은 후 출력
입력 받은 변수명을 연산하고 출력한다.
3. 유효성 검사
유효하지 않은 값과 입력 시, 에러를 해결한다.
4. (1~3까지의 1명의 성적을 처리했다면) 여러 명의 점수를 입력 받아서 처리한다.
5. Swing로 대체한다.
===========1. 기본값 출력 테스트 ===================================================
===============================
Score.java
package scorecalc;
public class Score {
 protected int num;
 protected String name;
 protected int kor;
 protected int eng;
 protected int math;
 protected int total;
 protected double avg;
 protected char grade='*';
 protected int rank;
 public void outPut(){
  System.out.println(num+" "+name+" "+kor+" "+eng+" "+math+" "+total+" "+avg+" "+grade+" "+rank);
 }
 public void titlePrint(){
  System.out.println("번호"+" "+"이름"+" "+"국어"+" "+"영어"+" "+"수학"+" "+"총점"+" "+"평균"+" "+"등급"+" "+"랭킹");
 }
}
===========================================
ScoreMain.java
package scorecalc;
public class ScoreMain {
 public static void main(String[] args) {
  Score sc = new Score();
  sc.titlePrint();
  sc.outPut();
 }
}
===========2. 변수를 입력 받은 후 출력 ==============================================
=======================
Score.java
package scorecalc;
public class Score {
 protected int num;
 protected String name;
 protected int kor;
 protected int eng;
 protected int math;
 protected int total;
 protected double avg;
 protected char grade='*';
 protected int rank;
}
=======================
ScoreIO.java
package scorecalc;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class ScoreIO extends Score{
 public void outPut(){
  System.out.println(num+" "+name+" "+kor+" "+eng+" "+math+" "+total+" "+avg+" "+grade+" "+rank);
 }
 public void titlePrint(){
  System.out.println("성적처리를 시작합니다.");
  System.out.println("번호"+" "+"이름"+" "+"국어"+" "+"영어"+" "+"수학"+" "+"총점"+" "+"평균"+" "+"등급"+" "+"랭킹");
 }
 public void standInput() throws IOException {
  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  System.out.println("번호를 입력하세요.");
  num = Integer.parseInt(br.readLine().trim());
  System.out.println("이름을 입력하세요.");
  name = br.readLine().trim();
  System.out.println("국어점수를 입력하세요.");
  kor = Integer.parseInt(br.readLine().trim());
  System.out.println("영어점수를 입력하세요.");
  eng = Integer.parseInt(br.readLine().trim());
  System.out.println("수학점수를 입력하세요.");
  math = Integer.parseInt(br.readLine().trim());
  calc();
 }
 public void calc(){
  total = kor + eng + math;
  avg = (double)total/3.;
  if(avg>100 || avg <0)grade='*';  
  else if(avg>=90)grade='A';
  else if(avg>=80)grade='B';
  else if(avg>=70)grade='C';
  else if(avg>=60)grade='D';
  else grade='F';
 }
}
=======================
ScoreMain.java
package scorecalc;
import java.io.IOException;
public class ScoreMain {
 public static void main(String[] args) throws IOException {
  ScoreIO sc = new ScoreIO();
  sc.standInput();
  sc.titlePrint();
  sc.outPut();
 }
}
============ 3. 유효성 검사 ========================================================
유효성 검사를 위해서 별도의 메서드(inputCheck)를 만들어 ScoreIO.java 파일의 standInput() 메서드를 다시 기술한다.
파일명 ScoreIO.java

protected void standInput() throws IOException {
  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  num = inputCheck("번호?", br);
  System.out.println("이름을 입력하세요."); // 이름은 String형이므로 inputCheck에서 제외
  name = br.readLine().trim();
  kor = inputCheck("국어?",br);
  eng = inputCheck("영어",br);
  math = inputCheck("수학?", br);
  calc();
 }
 private int inputCheck(String str, BufferedReader br) throws IOException{
  System.out.println(str);
  int temp=-1;
  try {
   temp=Integer.parseInt(br.readLine().trim());
   if(str.startsWith("번호")) return temp;
   else if (temp>=0 && temp<=100) return temp;
  } catch (NumberFormatException e) {
   System.out.println("숫자를 다시 입력 해 보세요");
  }
  temp=inputCheck(str,br);
  return temp;    
 }
=============== 4. 여러명 처리 =====================================================
여러명의 데이터를 입력 받아 처리하기 위해 ArrayList로 처리한다.
파일명 ScoreArray.java
package scorecalc;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
public class ScoreArray {
 private ArrayList<Score> stus = new ArrayList<Score>();
 public void init(){
  stus.add(new Score());
  BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
  System.out.println("추가로 입력? y/n");
  try {
   String yn=br.readLine().trim();
   if(yn.equalsIgnoreCase("y")) init();
   else {
    Collections.sort(stus);
    output();
   }
  } catch (IOException e) {
   e.printStackTrace();
  }
 }
 public void output(){
  if(stus==null || stus.size()==0){
   System.out.println("출력할 자료가 없음");
   return;
  }
  for(Score s:stus)
   s.output();
  /*for(int i=0;i<stus.size();i++){ // for문 해석문
   stus.get(i).outPut();
  }*/
 }
}
==========================
파일명 ScoreArrayUse.java
package scorecalc;
public class ScoreArrayUse {
 public static void main(String[] args) {
  ScoreArray stus=new ScoreArray();
  stus.init();
 }
}
============ 5. Swing으로 화면 연결 ================================================
===================================================================================
===================================================================================
===================================================================================
===================================================================================
===================================================================================
===================================================================================
===================================================================================