JAVA/코드

코드리뷰 ②- 학생부 프로그램 함수화 -V&M

record2080 2025. 1. 10. 10:01

● 메뉴입력을 하나의 기능으로 보는 경우(0번부터 4번까지)

   //Model

   public static int[] selectAll() {
      return stuList;
   }

   public static int selectOne(int num) {
      return stuList[num];
   }

   public static int randNum(int num) {   //중복 없이 난수 생성
      Random rand=new Random();
      int score;
      while(true) {
         score=rand.nextInt(101);   //0~100
         if(score != selectOne(num-1)){
            break;
         }
      }
      return score;
   }
   public static int findMax() {   //데이터를 가져와 비교하기 때문에 Model
      int max=selectOne(0);
      int maxIndex=0;
      for(int i=1;i<selectAll().length;i++) {   
         if(max<selectOne(i)) {
            max=selectOne(i);
            maxIndex=i;
         }
      }
      return maxIndex;
   }

   //View

   public static int inputAct() {//메뉴 선택 입력받기
      Scanner sc=new Scanner(System.in);
      System.out.println("=====학생부 프로그램=====");
      System.out.println("1. 전체 출력");
      System.out.println("2. 1등 출력");
      System.out.println("3. 정보 추가");
      System.out.println("4. 정보 변경");
      System.out.println("0. 프로그램 종료");
      System.out.println("=======================");
      System.out.print(">> ");
      int action=sc.nextInt();

      return action;
   }
   public static boolean emptyList(int cnt) {   //배열이 비어있는지 확인
      if(cnt<=0) {   //UI/UX
         System.out.println("출력할 데이터가 없습니다!");
         return false;
      }
      return true;
   }
   public static void printStuList(int cnt, int[] stuList) {
      for(int i=0;i<cnt;i++) { //stuList.length는 학생부 자체의 크기
         System.out.println((i+1)+"번 학생의 점수는 : "+stuList[i]+"점");
      }
   }
   public static void printFirst(int maxIndex, int max) {
      System.out.println("1등은 "+(maxIndex+1)+"번 학생,"+max+"점 입니다");
   }
   public static boolean overStu(int cnt, int LEN) {
      if(cnt>=LEN) { //최대 학생수만큼 저장된 상황이라면
         System.out.println("학생부에 저장공간이 부족합니다");
         return false;
      }
      return true;
   }
   public static int inputScore() {   //점수 입력
      Scanner sc=new Scanner(System.in);
      int score;
      while(true) {
         System.out.print("추가할 학생의 점수 입력>>");
         score=sc.nextInt();
         if(0<=score && score<=100) {
            break;
         }
         System.out.println("0에서 100점 사이만 입력 가능합니다");
      }
      return score;
   }
   public static void printSuccess() {   //성공메시지
      System.out.println("작업 완료!"); 
   }
   public static int inputNum(int cnt) {   //정보 변경 할 학생 번호 입력
      Scanner sc=new Scanner(System.in);
      int num;
      while(true) {
         System.out.print("정보 변경 할 학생의 번호 입력>>");
         num=sc.nextInt();
         if(1<=num && num<=cnt) { //종료조건
            break;
         }
         System.out.println("해당 번호 학생은 존재하지 않습니다");
      }
      return num;
   }
   public static void printErr() { //에러메시지 
      System.out.println("잘못된 입력입니다. 다시 입력해주세요");
   }
   public static void printStu(int num, int index) {   // 1등 점수랑 1등 자리
      System.out.println("1등은 "+(index+1)+"번, "+num+"점 입니다");
   }


   public static void main(String[] args) {

      int cnt=0;   // 현재 학생부에 저장된 학생 수
      while(true) {
         int action= inputAct();

         if(action==0) {   //종료조건
            break;
         }
         else if(action==1) { //전체 출력
            //현재 저장된 학생들의 점수정보를 출력
            if(emptyList(cnt)) {
               printStuList(cnt, selectAll());
            }
         }
         else if(action==2) { //1등 출력
            emptyList(cnt);
            //최댓값 찾기 알고리즘
            int maxIndex=findMax();

            if(cnt>0) { //비어있지 않을 때
               printStu(selectOne(maxIndex), maxIndex);

            }
         }
         else if(action==3) { //점수 추가
            if(overStu(cnt,selectAll().length)) {      
             //입력받은 점수 정보를 배열에 저장
               selectAll()[cnt]=inputScore();    
               cnt++;

               //저장완료! 안내
               printSuccess();
            }
         }
         else if(action==4) { //정보 변경
            if(!emptyList(cnt)) {
               continue;
            }
            //정보 변경 할 학생의 번호를 입력
            //유효성 검사
            int num=inputNum(cnt); 

            //어떤 점수로 변경할지 결정//랜덤으로 변경
            selectAll()[num-1]=randNum(num);   //U
            //정보 변경 완료 안내
            printSuccess();
         }
         else {   //유효성 검사
            printErr();
         }

      }
   }

 

<코드리뷰>

-함수명을 정확하게 쓴다
-if 의 경우, 변수명은 is(has)empty로 해야한다

-select one이 select one일까??
점수일 경우, 중복이 있지 않을까??
-num이 사용자가 아무리 잘 작성했어도 외부의 공격이 들어올수 도 있으니,
모델은 유효성 검사를 해줘야만 함 (2,3중으로 유효성 검사)
(MODEL은  VIEW를 믿지 않음.)

if(num이 이상한 값이라면) {
retrun 0; 
}

이 있었더라면!! 

 

public static int randNum(int num) {//중복 없이 난수 생성
Random rand=new Random();
int score;
while(true) {
score=rand.nextInt(101); //0~100
if(score != selectOne(num-1))  {
break;
     }
   }
return score;
}

 Model part로 분류했지만, 실질적으로는 C(Ctroller) part.

'JAVA > 코드' 카테고리의 다른 글

학생부 프로그램 마무리와 코드리뷰 - 객체배열  (0) 2025.01.14
객체 배열- 학생부 프로그램  (0) 2025.01.13
코드리뷰 ①- 선택정렬  (0) 2025.01.09
포켓몬을 잡아라!  (1) 2025.01.06
쇼핑몰 프로그램  (1) 2025.01.03