JAVA/코드

객체 배열- 학생부 프로그램

record2080 2025. 1. 13. 23:56

import Practice.Student;

 

class Student { //학생은 번호, 이름, 점수, 등급 변수를 가지고 있다.

static int studentnum; //학생 번호는 1001부터 시작한다.번호와 학생번호 유의!

int num;

String name;

int score;

char grade;

 

Student(String name) {//생성자 함수- 초기화를 위해

this.name=name; //이름, 점수, 등급은 외부에서 받고 번호는 1001부터 차례로 증가;

this.num=Student.studentnum++;

this.score=0;

this.grade='A';

}

Student(String name,int score) {

this.name=name;

this.num=Student.studentnum++;

this.score=score;

this.grade='C';

if(60<=this.score&&this.score<80) {

this.grade='B';

}

else if(80<=this.score) {

this.grade='A';

}

System.out.println("학생정보생성완료");

}

 

}

 


Student(String name) {
this.name=name; 
this.num=Student.studentnum++;
this.score=0;
this.grade='A';
}
Student(String name){
this(name,0);
}
=>this 자기자신 불러내는 함수로 코드단축

void printInfo() { //번호, 이름 , 점수 , 등급 출력할 정보

System.out.println("번호 : "+this.num);

System.out.println("이름 : "+this.name);

System.out.println("점수 : "+this.score);

System.out.println("등급 : "+this.grade);

}

}

 

 public static void main(String[] args) { //이제부터 메인

 

  public static boolean isFull(int cnt,Student[] datas) { 
   if( cnt>=datas.length ) {
      return true;
}
     return false;
}

      Student[] datas=new Student[3]; //학생부에는 총 3명의 학생을 저장할 수 있다.

      datas[0]=new Student("짱구"); //이름
      datas[1]=new Student("철수",90); //이름, 점수
       int cnt=2;  //학생부에 현재 저장되어 있는 학생의 수

 

 Scanner sc=new Scanner(System.in);
      while(true) {  //번호를 입력하면 번호에 알맞는 기능을 하는 프로그램 

         System.out.println("0. 프로그램 종료");
         System.out.println("1. 전체출력");
         System.out.println("2. 번호로 학생 검색");
         System.out.println("3. 학생추가");
         System.out.println("4. 성적변경");

 

        System.out.println(">>");

        int menu=sc.nextInt();

        if(menu==0) { 

          break;

         }

 

          else if(menu==1) {

            for(int i=0;i<cnt;i++) { //1번을 누르면 학생부에 저장되어 있는 학생을 출력하세요.

            datas[i].printInfo(); 

           }

       }

         else if(menu==2) {  //번호에 해당하는 학생이 있을 때, 없을 때(그렇다면 양자택일?!)

          System.out.print("번호입력>>");

          int num=sc.nextInt();

          

             boolean flag=false; 

         

            for(int i=0;i<cnt;i++) {

            if(num==datas[i].num) { //학생부가 아니라 학생의 번호가 필요!!

             datas[i].printinfo();

             flag=true; 

             break;

      }

     }

datas==배열==학생부
datas[i]==배열[인덱스번호]==배열의 요소==학생==학생객체
datas[i].num==학생.번호==학생객체. 멤버변수

 

             if(!flag) { //

               System.out.println("해당학생은 없습니다");

     }

  }

     else if(menu==3) {  //학생부에 학생추가할 수 있을 때와 없을 때

                                    //주의하세요. 왜?? 학생부의 영역(즉, 프로그램의 영역 학생이랑은 무관)

         if(isFull(cnt,datas)) {

             System.out.println("학생부 데이터에 저장공간이 없습니다");

             continue;

      }

         System.out.println("이름 입력>>");

         String name=sc.next();

         System.out.println("성적을 입력하시겠습니까? 예/아니오>>");

          String ans=sc.next();

           if(ans.equals("Y")) {

              System.out.print("성적입력>>");

              int score=sc.nextInt();

              datas[cnt++]=new student(name,score); 

       }

          else{

           datas[cnt++]=new Student(name);

      }

    } 

     else if(menu==4) {

        System.out.print("번호입력>>");

         int num=sc.nextInt();

         boolean flag=false;

 

      int i

       for(i=0;i<cnt;i++) {

                      if(num==datas[i].num {

                                     datas[i].printinfo();

                                      flag=true;

                                       break;

}

  }

                if(!flag) {

                          System.out.println("해당학생은 없습니다");

                           continue;

}

                        System.out.print("변경할 점수 입력>>");

                        int score=sc.nextInt();

 

                        datas[i].changeScore(score);

 }

}