728x90
목표
- Student 클래스를 만들어 학생 정보(이름, 학번, 점수 등) 저장
- ArrayList<Student>로 여러 학생 저장
- 메뉴를 통해 학생 정보 추가 / 전체 출력 / 검색 / 종료 기능 구현
1. Student 클래스
public class Student {
String name;
String studentId;
int score;
public Student(String name, String studentId, int score) {
this.name = name;
this.studentId = studentId;
this.score = score;
}
public void printInfo() {
System.out.println("이름: " + name + ", 학번: " + studentId + ", 점수: " + score);
}
}
2. Main 클래스 – 메뉴 시스템 구현
import java.util.ArrayList;
import java.util.Scanner;
public class StudentManager {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
ArrayList<Student> students = new ArrayList<>();
while (true) {
System.out.println("\n[학생 정보 관리 시스템]");
System.out.println("1. 학생 추가");
System.out.println("2. 전체 학생 목록 보기");
System.out.println("3. 이름으로 학생 검색");
System.out.println("4. 종료");
System.out.print("선택: ");
int choice = sc.nextInt();
sc.nextLine(); // 줄바꿈 처리
switch (choice) {
case 1:
System.out.print("이름: ");
String name = sc.nextLine();
System.out.print("학번: ");
String id = sc.nextLine();
System.out.print("점수: ");
int score = sc.nextInt();
students.add(new Student(name, id, score));
System.out.println("학생이 추가되었습니다.");
break;
case 2:
System.out.println("\n📋 전체 학생 목록:");
for (Student s : students) {
s.printInfo();
}
break;
case 3:
System.out.print("검색할 이름: ");
String searchName = sc.nextLine();
boolean found = false;
for (Student s : students) {
if (s.name.equals(searchName)) {
s.printInfo();
found = true;
}
}
if (!found) {
System.out.println("❗ 해당 이름의 학생이 없습니다.");
}
break;
case 4:
System.out.println("프로그램을 종료합니다.");
sc.close();
return;
default:
System.out.println("⚠️ 올바른 번호를 선택해주세요.");
}
}
}
}
실행 예시
[학생 정보 관리 시스템]
1. 학생 추가
2. 전체 학생 목록 보기
3. 이름으로 학생 검색
4. 종료
선택: 1
이름: 영희
학번: 202401
점수: 95
학생이 추가되었습니다.
확장 아이디어
- 평균/최고점/최저점 계산 기능 추가
- 점수순 정렬
- 학점(A~F) 출력 기능
- CSV 파일로 저장 및 불러오기 (고급 기능)
배운 개념 정리
기능 | 사용된 문법 |
클래스 | Student 정의 |
객체 생성 | new Student(...) |
리스트 | ArrayList<Student> |
조건문 | if, switch |
반복문 | for, while |
입력 처리 | Scanner 사용 |
문자열 비교 | .equals() |
LIST
'JAVA > 객체지향 프로그래밍 (OOP) 기초' 카테고리의 다른 글
간단한 RPG 캐릭터 설계 (1) | 2025.04.13 |
---|---|
강아지/고양이 클래스 만들기 (2) | 2025.04.13 |
부모 타입으로 자식 객체 다루기 (0) | 2025.04.13 |
오버라이딩 개념 (0) | 2025.04.13 |
extends 키워드로 상속하기 (0) | 2025.04.13 |