[SECTION09-00. 종합 테스트 문제]
지금까지 배운 배열, 반복문, 조건문, 클래스를 활용하여 콘솔 기반 학생 관리 프로그램을 만들어 보세요.
이 실습을 통해 “학생 정보처럼 여러 개의 값을 하나로 묶어 관리할 때 왜 클래스가 필요한지” 를 이해하는 것이 목표입니다.
다음과 같은 기능을 가진 학생 관리 프로그램을 작성하세요.
프로그램 실행 시 아래 메뉴가 반복적으로 출력되어야 합니다.
===== 명령어 목록 =====
1. 학생 등록
2. 학생 목록
3. 학생 검색
4. 학생 수정
5. 학생 삭제
0. 종료
1번) 이름: 홍길동 / 나이: 20 / 점수: 90
Student)Student[])while, for)if, switch)Scanner)import java.util.Scanner;
public class Exam01 {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
final int MAX_STUDENT_COUNT = 3;
Student[] students = new Student[MAX_STUDENT_COUNT];
students[0] = new Student("유신영", 27, 100);
students[1] = new Student("유프로", 28, 92);
students[2] = new Student("유영신", 29, 88);
// 학생 수 관리를 위한 변수
int count = 3;
while(true) {
System.out.println("\n=== 명령어 목록 ===");
System.out.println("1. 학생 등록");
System.out.println("2. 학생 목록");
System.out.println("3. 학생 검색");
System.out.println("4. 학생 수정");
System.out.println("5. 학생 삭제");
System.out.println("0. 종료\n");
System.out.print("명령어:");
int cmd = scn.nextInt();
scn.nextLine(); // 버퍼 방지
switch (cmd) {
case 1:
if (count == 3) {
System.out.println("학생을 더 이상 등록할 수 없습니다.");
continue;
}
System.out.println("학생 등록입니다.");
// 학생 정보를 입력받아야 함
System.out.print("학생 이름:");
String name = scn.nextLine();
System.out.print("학생 나이:");
int age = scn.nextInt();
System.out.print("학생 점수:");
int score = scn.nextInt();
students[count] = new Student(name, age, score);
count++;
break;
case 2:
System.out.println("학생 목록입니다.");
for (int i = 0; i < MAX_STUDENT_COUNT; i++) {
if (students[i] == null) {
continue;
}
System.out.printf("%d번) 이름: %s\n",
i + 1,
students[i].getName());
}
break;
case 3:
System.out.println("학생 검색입니다.");
System.out.print("검색할 학생 번호: ");
// 1번 학생 검색 -> 0번째 인덱스
int searchNum = scn.nextInt(); // 검색할 학생 번호
int searchIndex = searchNum - 1; // 검색할 학생 인덱스
// 0보다 작은 숫자가 들어왔을 때와 등록된 학생 수보다 높은 숫자가 들어 왔을 때
// 되돌려 보낸다
// count가 2라는 건 학생이 두 명 등록되어 있다는 건데
if(searchIndex < 0 || searchIndex > count - 1) {
System.out.println("해당 학생은 존재하지 않습니다.");
continue;
}
Student searchStudent = students[searchIndex];
System.out.printf("%d번) 이름: %s / 나이: %d살 / 점수: %d점\n",
searchNum,
searchStudent.getName(),
searchStudent.getAge(),
searchStudent.getScore());
break;
case 4:
System.out.println("학생 수정입니다.");
System.out.print("수정할 학생 번호: ");
int modifyNum = scn.nextInt();
scn.nextLine();
int modifyIndex = modifyNum - 1;
if(modifyIndex < 0 || modifyIndex > count - 1) {
System.out.println("해당 학생은 존재하지 않습니다.");
continue;
}
System.out.print("수정할 학생 이름:");
String modifyName = scn.nextLine();
System.out.print("수정할 학생 나이:");
int modifyAge = scn.nextInt();
System.out.print("수정할 학생 점수:");
int modifyScore = scn.nextInt();
students[modifyIndex] = new Student(modifyName, modifyAge, modifyScore);
System.out.println("\n" + modifyNum + "번 학생 정보 수정이 완료되었습니다.\n");
break;
case 5:
System.out.println("학생 삭제입니다.");
System.out.print("삭제할 학생 번호: ");
int delNum = scn.nextInt(); // 3
scn.nextLine();
int delIndex = delNum - 1; // 2
if(delIndex < 0 || delIndex > count - 1) {
System.out.println("해당 학생은 존재하지 않습니다.");
continue;
}
for (int i = delIndex; i < count; i++) {
if (i == MAX_STUDENT_COUNT - 1) {
students[i] = null;
continue;
}
students[i] = students[i + 1];
}
System.out.println("\n" + delNum + "번 학생 정보가 삭제되었습니다.\n");
break;
case 0:
System.out.println("프로그램을 종료합니다.");
return;
default:
System.out.println("해당 명령어는 존재하지 않습니다.");
}
}
}
}
public class Student {
private String name;
private int age;
private int score;
public Student(String name, int age, int score) {
this.name = name;
this.age = age;
this.score = score;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public int getScore() {
return score;
}
}