- 학생이 다니는 학원의 지점, 학원 이름, 과정(코스(course)), 전화번호 정보를 관리
- 학생 리스트를 출력, 검색
📢 학생 클래스
멤버 변수
- locate: 학생의 지점 (기본값: "incheon")
- name: 학생의 이름 (기본값: null)
- course: 수강하는 과정
- phone: 전화번호
생성자
- 기본 생성자: locate는 "incheon", name은 "student", course는 "JAVA"로 초기화
- 지점과 이름을 받는 생성자.
- 모든 정보를 받는 생성자: 다른 생성자를 호출하여 초기화.
메서드
- print(): 학생 정보를 출력하는 메서드.
- toString(): 객체의 내용을 문자열로 반환함. 객체를 출력할 때 유용.
public class Student {
private String locate = "incheon"; // 명시적 초기값
private String name;
private String course;
private String phone;
{
// 초기화 블럭
name = "student";
course = "JAVA";
phone = "010-0000-0000";
}
public Student() {
name = "student";
course = "JAVA";
}
public Student(String locate, String name) {
this.locate = locate;
this.name = name;
}
public Student(String locate, String name, String course, String phone) {
this(locate, name);
this.course = course;
this.phone = phone;
}
public void print() {
System.out.println(name + "(" + phone + ") - " + course + " (지점: " + locate + ")");
}
@Override
public String toString() {
return "Student [locate=" + locate + ", name=" + name + ", course=" + course + ", phone=" + phone + "]";
}
// Getter/Setter 메서드 생략
}
📢 메인 클래스
- 메인 클래스에서는 여러 학생 객체를 생성
- 배열에 저장하여 정보를 출력, 검색
package day03;
public class StudentMain {
public static void main(String[] args) {
Student s = new Student("incheon", "홍길동");
s.print();
System.out.println(s); //자동으로 toString 호출
Student s1 = new Student("incheon", "김순이", "AWS 개발자 양성 과정", "010-1111-1111");
s1.print();
//toString이 없으면 객체의 주소가 출력
System.out.println(s1);
Student s2 = new Student("incheon", "김영철", "JAVA", "010-1111-2222");
Student s3 = new Student("seoul", "이순이", "AWS 개발자 양성 과정", "010-1111-3333");
Student s4 = new Student("incheon", "박승이", "AWS 개발자 양성 과정", "010-1111-4444");
Student s5 = new Student("seoul", "최순철", "JAVA", "010-1111-5555");
Student[] studentArr = new Student[6];
studentArr[0]=s;
studentArr[1]=s1;
studentArr[2]=s2;
studentArr[3]=s3;
studentArr[4]=s4;
studentArr[5]=s5;
// 전체 학생명단 출력
for(Student a : studentArr) {
a.print();
// 또는 System.out.println(a);
}
System.out.println("-----------------------------------");
//홍길동의 정보를 출력
//equals() : String의 값이 같은지 확인하는 메서드
String searchName = "홍길동";
for(Student a : studentArr) {
if(a.getName().equals(searchName)){
a.print();
}
}
System.out.println("-----------------------------------");
//incheon 지점 학생들 명단 출력
//학생이 없다면 명단이 없습니다. 출력
boolean isIncheon = false;
String searchLocate = "seoul";
for(int i=0;i<studentArr.length;i++) {
if(studentArr[i].getLocate().equals(searchLocate)) {
isIncheon = true;
studentArr[i].print();
}
}
if(!isIncheon) {
System.out.println("명단이 없습니다.");
}
System.out.println("-----------------------------------");
//홍길동의 group을 AWS로 변경 => 출력
String course = "전산회계";
for(Student a : studentArr) {
if(a.getName().equals(searchName)){
a.setCourse(course);
a.print();
}
}
}
}
결과
홍길동(010-0000-0000) - JAVA (지점: incheon)
Student [locate=incheon, name=홍길동, course=JAVA, phone=010-0000-0000]
김순이(010-1111-1111) - AWS 개발자 양성 과정 (지점: incheon)
Student [locate=incheon, name=김순이, course=AWS 개발자 양성 과정, phone=010-1111-1111]
홍길동(010-0000-0000) - JAVA (지점: incheon)
김순이(010-1111-1111) - AWS 개발자 양성 과정 (지점: incheon)
김영철(010-1111-2222) - JAVA (지점: incheon)
이순이(010-1111-3333) - AWS 개발자 양성 과정 (지점: seoul)
박승이(010-1111-4444) - AWS 개발자 양성 과정 (지점: incheon)
최순철(010-1111-5555) - JAVA (지점: seoul)
-----------------------------------
홍길동(010-0000-0000) - JAVA (지점: incheon)
-----------------------------------
이순이(010-1111-3333) - AWS 개발자 양성 과정 (지점: seoul)
최순철(010-1111-5555) - JAVA (지점: seoul)
-----------------------------------
홍길동(010-0000-0000) - 전산회계 (지점: incheon)'백엔드 > Java' 카테고리의 다른 글
| Java 상속(Extends) (2) | 2024.10.07 |
|---|---|
| Java로 학생 정보 관리 시스템 구현(2) (1) | 2024.10.07 |
| Java로 카드 게임 만들기 (4) | 2024.10.01 |
| Java 클래스(Class) (2) (1) | 2024.09.30 |
| Java 클래스(Class) (1) (0) | 2024.09.29 |