백엔드/Java

Java로 학생 정보 관리 시스템 구현(2)

짱뚱짱 2024. 10. 7. 19:40

학생 정보 출력 (printInfo()) : 학생의 이름, 생년월일, 전화번호, 나이 출력
학원 정보 출력 (printCompany()) : 학원명, 지점 출력
수강 정보 출력 (printCourse()) : 현재 수강 중인 과목과 그 기간을 출력. 수강 과목이 없는 경우 "수강 과목이 없습니다."
수강 과목 추가 (insertCourse()) : 수강 과목과 기간 추가 최대 5개 과목까지 추가가능, 초과시 "더 이상 수강하실 수 없습니다."

 

package day04;
//패키지가 다르면 클래스명은 같아도 상관 X

/* - 학생 정보를 관리하기 위한 클래스 
 * - 학생 기본정보 : 이름, 생년월일, 전화번호, 나이
 * - 학원 정보 : 학원명 = "EZEN" (final static), 지점
 * - 수강 정보 : 수강과목, 기간 => 수강과목도 배열, 기간도 배열
 * 	=> 여러 과목을 들을 수 있음.(여러과목을 수강하기 위해서는 배열로 처리 5과목까지 가능)
 * 
 * ex) 홍길동(010101) 010-1111-1111 / 25 => 학생정보
 * 		EZEN(인천) => 학원정보
 * 		자바 6개월, 파이썬 1개월, SQLD 1개월 => 수강정보
 * 기능(메서드)
 * - 학생의 기본정보를 출력하는 기능
 * - 학원 정보를 출력하는 기능
 * - 수강정보를 출력하는 기능
 * - 학생이 수강정보를 추가하는 기능
 */
public class Student {
	//멤버변수 선언
	//이름, 생년월일, 전화번호, 나이
	private String name; 
	private String birth;
	private String phone;
	private String age;
	//학원명="EZEN" (final static), 지점
	private final static String COMPANY="EZEN";
	private String branch;
	//수강과목, 기간
	private String[] course = new String[5];
	private String[] period = new String[5];
	//index 처리용 변수
	private int cnt;
	
	//생성자
	public Student() {}

	//phone, branch, name
	public Student(String name, String phone, String branch) {
		this.name = name;
		this.phone = phone;
		this.branch = branch;
	}

	public Student(String name, String birth, String phone, String age, String branch) {
		this.name = name;
		this.birth = birth;
		this.phone = phone;
		this.age = age;
		this.branch = branch;
	}

	public void printInfo() {
//		System.out.println("--학생정보--");
		System.out.println(name+"("+birth+") "+phone+" / "+age);
	}
	public void printCompany() {
		System.out.println(COMPANY+"/"+branch+"지점");
	}
	public void printCourse() {
		if(course.length==0 || cnt == 0) {
			System.out.println("수강과목이 없습니다.");
			return;
		}
		for(int i=0;i<cnt;i++) {
			System.out.print(course[i]+"("+period[i]+")  ");
		}
		System.out.println();
	}
	
	// 수강등록
	// insertCourse()
	// 매개변수 : 등록하고자 하는 course, period
	// 리턴타입 : 배열에 데이터 추가 (리턴되는 값은 없음. ) void
	public void insertCourse(String course, String period) {
		// cnt가 0인 상황(하나도 추가되지 않은 상황
		if(cnt>=5) {
			System.out.println("더이상 수강하실 수 없습니다.");
			//배열을 늘려서 더 많은 수강을 받을 수도 있음. (배열복사)
			return;
		}
		this.course[cnt] = course;
		this.period[cnt] = period;
		cnt++;
	}
	
	
	// getter, setter
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getBirth() {
		return birth;
	}

	public void setBirth(String birth) {
		this.birth = birth;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getAge() {
		return age;
	}

	public void setAge(String age) {
		this.age = age;
	}

	public String getBranch() {
		return branch;
	}

	public void setBranch(String branch) {
		this.branch = branch;
	}

	public String[] getCourse() {
		return course;
	}

	public void setCourse(String[] course) {
		this.course = course;
	}

	public String[] getPeriod() {
		return period;
	}

	public void setPeriod(String[] period) {
		this.period = period;
	}

	public static String getCompany() {
		return COMPANY;
	}
	
}
package day04;

public class StudentMain {

	public static void main(String[] args) {
		Student s = new Student();
		s.setName("홍길동");
		s.setPhone("010-1111-1111");
		s.printInfo();
		s.printCompany();
		s.printCourse();
		
		Student s1 = new Student("hong", "010-111-2222", "incheon");
		s1.printInfo();
		s1.printCompany();
		s1.printCourse();
		s1.insertCourse("java", "5 Month");
		s1.insertCourse("python", "1 Month");
		s1.printCourse();
		
		Student s2 = new Student("pack", "010101", "010-2222-2222", "25", "incheon");
		s2.printInfo();
		s2.printCompany();
		s2.insertCourse("java", "5 Month");
		s2.insertCourse("HTML", "1 Month");
		s2.insertCourse("React", "1 Month");
		s2.printCourse();
		
		//5명 정보를 추가
		Student[] std = new Student[8];
		std[0] = s;
		std[1] = s1;
		std[2] = s2;
		std[3] = new Student("choi", "040404", "010-2222-3333", "20", "seoul");
		std[3].insertCourse("java", "5 Month");
		std[3].insertCourse("React", "1 Month");
		std[4] = new Student("kim", "900101", "010-3333-4444", "34", "incheon");
		std[4].insertCourse("HTML", "1 Month");
		std[5] = new Student("lee", "010202", "010-4444-5555", "23", "seoul");
		std[5].insertCourse("python", "1 Month");
		std[5].insertCourse("C", "5 Month");
		std[5].insertCourse("PHP", "3 Month");		
		std[6] = new Student("jung", "030101", "010-5555-6666", "21", "incheon");
		std[7] = new Student("kang", "020202", "010-6666-7777", "22", "seoul");
		std[7].insertCourse("React", "1 Month");
		std[7].insertCourse("java", "5 Month");
		
		
		System.out.println("-------------학생 전체 명단---------------");
		//학생 전체 명단 출력(학생 정보만)
		for(Student a : std) {
			a.printInfo();
		}
		
		System.out.println("----------------------------");
		//hong 학생의 학생정보, 학원정보, 수강정보 출력
		String searchName = "hong";
		for(int i=0; i<std.length; i++) {
			if (std[i].getName().equals(searchName)) {
				std[i].printInfo();
				std[i].printCompany();
				std[i].printCourse();
			}
		}
		
		//incheon지점의 학생 명단 출력(학생 정보만)
		String searchBranch = "incheon";
		System.out.println("-------------"+searchBranch+" 검색 정보---------------");
		for(int i=0; i<std.length; i++) {
			if(std[i].getBranch() != null) {	//nullPointException 방지용	
				if(std[i].getBranch().equals(searchBranch)) {
					std[i].printInfo();
					std[i].printCompany();
				}
			}
		}
		System.out.println("----------------------------");
		//java 과목을 수강하는 학생명단 출력(학생정보, 학원정보, 수강정보전부 출력)
		String searchCourse = "java";
		System.out.println("--------------"+searchCourse+"과목 수강학생 정보------------");
		//전체 학생 배열에서 개개인의 과목 배열로 2중 검색
		int cnt = 0;
		while(cnt < std.length) {
			for(int i=0; i<std[cnt].getCourse().length; i++) {
				if(std[cnt].getCourse()[i] !=null) {
					if(std[cnt].getCourse()[i].equals(searchCourse)) {
						std[cnt].printInfo();
						std[cnt].printCompany();
						std[cnt].printCourse();
					}
				}
			}
			cnt++;
		}
		
		//수강하지 않는 학생 명단 출력 (학생정보만)..
		System.out.println("------------수강과목이 없는 학생 정보-------------");
		cnt = 0;
		while(cnt<std.length) {
			if(std[cnt].getCourse()[0]==null) {
				std[cnt].printInfo();
			}
			cnt++;
		}
		


	}

}

 

 

결과

홍길동(null) 010-1111-1111 / null
EZEN/null지점
수강과목이 없습니다.
hong(null) 010-111-2222 / null
EZEN/incheon지점
수강과목이 없습니다.
java(5 Month)  python(1 Month)  
pack(010101) 010-2222-2222 / 25
EZEN/incheon지점
java(5 Month)  HTML(1 Month)  React(1 Month)  
-------------학생 전체 명단---------------
홍길동(null) 010-1111-1111 / null
hong(null) 010-111-2222 / null
pack(010101) 010-2222-2222 / 25
choi(040404) 010-2222-3333 / 20
kim(900101) 010-3333-4444 / 34
lee(010202) 010-4444-5555 / 23
jung(030101) 010-5555-6666 / 21
kang(020202) 010-6666-7777 / 22
----------------------------
hong(null) 010-111-2222 / null
EZEN/incheon지점
java(5 Month)  python(1 Month)  
-------------incheon 검색 정보---------------
hong(null) 010-111-2222 / null
EZEN/incheon지점
pack(010101) 010-2222-2222 / 25
EZEN/incheon지점
kim(900101) 010-3333-4444 / 34
EZEN/incheon지점
jung(030101) 010-5555-6666 / 21
EZEN/incheon지점
----------------------------
--------------java과목 수강학생 정보------------
hong(null) 010-111-2222 / null
EZEN/incheon지점
java(5 Month)  python(1 Month)  
pack(010101) 010-2222-2222 / 25
EZEN/incheon지점
java(5 Month)  HTML(1 Month)  React(1 Month)  
choi(040404) 010-2222-3333 / 20
EZEN/seoul지점
java(5 Month)  React(1 Month)  
kang(020202) 010-6666-7777 / 22
EZEN/seoul지점
React(1 Month)  java(5 Month)  
------------수강과목이 없는 학생 정보-------------
홍길동(null) 010-1111-1111 / null
jung(030101) 010-5555-6666 / 21

'백엔드 > Java' 카테고리의 다른 글

Java 추상 클래스(abstract)  (0) 2024.10.08
Java 상속(Extends)  (2) 2024.10.07
Java로 학생 정보 관리 시스템 구현(1)  (2) 2024.10.02
Java로 카드 게임 만들기  (4) 2024.10.01
Java 클래스(Class) (2)  (1) 2024.09.30