1. 예시) 문제: 학생-강의 수강 시스템
코드 구조
public class Student {
private String name;
private String studentId;
private Lecture registeredLecture;
// 생성자
public Student(String name, String studentId) {
// 학생의 이름과 학번을 초기화합니다.
}
public boolean registerLecture(Lecture lecture) {
// 1. 이미 수강 중인 강의가 있는지 확인합니다.
// 2. 수강 중인 강의가 없다면, 주어진 강의에 학생을 등록합니다.
// 3. 강의 등록이 성공하면 registeredLecture를 업데이트하고 true를 반환합니다.
// 4. 실패하면 (이미 수강 중이거나 강의가 가득 찼을 경우) false를 반환합니다.
}
public boolean cancelLecture() {
// 1. 현재 수강 중인 강의가 있는지 확인합니다.
// 2. 수강 중인 강의가 있다면, 해당 강의에서 학생을 제거합니다.
// 3. registeredLecture를 null로 설정합니다.
// 4. 취소가 성공하면 true, 실패하면 false를 반환합니다.
}
public void printInfo() {
// 학생의 이름, 학번을 출력합니다.
// 수강 중인 강의가 있다면 강의명도 출력합니다.
// 수강 중인 강의가 없다면 "수강 중인 강의 없음"을 출력합니다.
}
}
public class Lecture {
//과목명
private String lectureName;
//최대수강인원
private int maxStudents;
//현재수강인원
private int currentStudents;
//현재 수강인원(private int currentStudents;)은 초기 생성을 할 필요 없음
// 생성자
public Lecture(String lectureName, int maxStudents) {
// 강의명과 최대 수강 인원을 초기화합니다.
}
public boolean addStudent() {
// 1. 현재 수강 인원이 최대 인원보다 적은지 확인합니다.
// 2. 여유가 있다면 currentStudents를 1 증가시키고 true를 반환합니다.
// 3. 강의가 이미 가득 찼다면 false를 반환합니다.
}
public boolean removeStudent() {
// 1. 현재 수강 인원이 0보다 큰지 확인합니다.
// 2. 수강 중인 학생이 있다면 currentStudents를 1 감소시키고 true를 반환합니다.
// 3. 수강 중인 학생이 없다면 false를 반환합니다.
}
public void printInfo() {
// 강의명, 현재 수강 인원, 최대 수강 인원을 출력합니다.
}
}
실습 조건
Student가 Lecture를 등록/취소할 때 Lecture의 학생 수가 변경되어야 함
수강 인원 제한 로직 포함 (최대 수강 인원을 20명으로 하여, 초과하면 등록할 수 없도록 함)
import entity.Lecture; import entity.Student; public class StudentMain { public static void main(String[] args) { // 1. 영어 과목을 생성 Lecture l1 = new Lecture("영어", 20); // 2. 수학 과목을 생성 Lecture l2 = new Lecture("수학", 20); // 3. 홍길동 생성 Student s1 = new Student("홍길동", "123"); // 4. 김나나 생성 Student s2 = new Student("김나나", "456"); // 5. 홍길동이 영어과목 수강신청 s1.registerLecture(l1); s1.printInfo(); // 6. 김나나도 영어과목 수강신청 s2.registerLecture(l1); s2.printInfo(); // 7. 홍길동이 수학과목 수강신청 s1.registerLecture(l2); // 8. 홍길동이 영어과목 취소하고 s1.cancelLecture(); s1.printInfo(); // 9. 수학과목 신청. s1.registerLecture(l2); s1.printInfo(); } } |
2. 풀이
프로젝트 생성 - Student2
클레스 생성 - StudentMain
패키지생성-entety
패키지 내 클래스 생성 - Student / Lecture
- 요청사항 :
코드 구조
public class Student {
private String name;
private String studentId;
private Lecture registeredLecture;
// 생성자
public Student(String name, String studentId) {
// 학생의 이름과 학번을 초기화합니다.
}
public boolean addStudent() {
// 1. 현재 수강 인원이 최대 인원보다 적은 지 확인합니다.
// 2. 여유가 있다면 currentStudents를 1 증가시키고 true를 반환합니다.
// 3. 강의가 이미 가득 찼다면 false를 반환합니다.
package entity; public class Student { // 학생 이름 private String name; // 학번 private String studentId; // 학생은 과목을 하나만 수강 가능. private entity.Lecture registeredLecture; public Student(String name, String studentId) { this.name = name; this.studentId = studentId; } public boolean cancelLecture() { // 1. 현재 수강 중인 강의가 있는지 확인합니다. // 2. 수강 중인 강의가 있다면, 해당 강의에서 학생을 제거합니다. // 3. registeredLecture를 null로 설정합니다. // 4. 취소가 성공하면 true, 실패하면 false를 반환합니다. return true; } // boolean 함수를 사용하기 때문에 return 과 true / false 중 맞는것로 선택하여 마무리합니다. |
- StudentMain 정보 생성
// 1. 영어 과목을 생성
// 2. 수학 과목을 생성
// 3. 홍길동 생성
// 4. 김나나 생성
// 5. 홍길동이 영어과목 수강신청
// 6. 김나나도 영어과목 수강신청
// 7. 홍길동이 수학과목 수강신청
// 8. 홍길동이 영어과목 취소하고
// 9. 수학과목 신청.
public class StudentMain { public static void main(String[] args) { // 1. 영어 과목을 생성 Lecture l1 = new Lecture("영어", 20); // 2. 수학 과목을 생성 Lecture l2 = new Lecture("수학", 20); // 3. 홍길동 생성 Student s1 = new Student("홍길동", "123"); // 4. 김나나 생성 Student s2 = new Student("김나나", "456"); |
- 요청사항
public class Lecture {
//과목명
private String lectureName;
//최대수강인원
private int maxStudents;
//현재수강인원
private int currentStudents;
//현재 수강인원(private int currentStudents;)은 초기 생성을 할 필요 없음
// 생성자
public Lecture(String lectureName, int maxStudents) {
// 강의명과 최대 수강 인원을 초기화합니다.
}
public boolean addStudent() {
// 1. 현재 수강 인원이 최대 인원보다 적은지 확인합니다.
// 2. 여유가 있다면 currentStudents를 1 증가시키고 true를 반환합니다.
// 3. 강의가 이미 가득 찼다면 false를 반환합니다.
}
package entity; public class Lecture { // 과목명 private String lectureName; // 최대 수강 인원 private int maxStudents; // 현재 수강 인원 private int currentStudents; public Lecture(String lectureName, int maxStudents) { this.lectureName = lectureName; this.maxStudents = maxStudents; } public boolean addStudent() { // 1. 현재 수강 인원이 최대 인원보다 적은지 확인합니다. // if문에서는 반대로 생각하여 작성하면 더 효율적으로 사용할수 있습니다. if(currentStudents >= maxStudents){ System.out.println("이과목은 수강인원이 다 차서 수강을 할 수 없습니다."); return false; } // 2. 여유가 있다면 currentStudents를 1 증가시키고 true를 반환합니다. currentStudents = currentStudents + 1; // 3. 강의가 이미 가득 찼다면 false를 반환합니다. return true; } |
- 요청사항
public boolean registerLecture(Lecture lecture) {
// 1. 이미 수강 중인 강의가 있는지 확인합니다.
// 2. 수강 중인 강의가 없다면, 주어진 강의에 학생을 등록합니다.
// 3. 강의 등록이 성공하면 registeredLecture를 업데이트하고 true를 반환합니다.
// 4. 실패하면 (이미 수강 중이거나 강의가 가득 찼을 경우) false를 반환합니다.
package entity; public class Student { // 학생 이름 private String name; // 학번 private String studentId; // 학생은 과목을 하나만 수강 가능. private entity. boolean registeredLecture; => private entity. registeredLecture; // 하단의 registeredLecture가 상단의 registeredLecture 의 앞에 boolean과 연동하여 boolean 사라집니다. public Student(String name, String studentId) { this.name = name; this.studentId = studentId; } public boolean registerLecture(entity.Lecture lecture) { // 1. 이미 수강 중인 강의가 있는지 확인합니다. // 하단의 if문역시 반대로 하여 강의수강자가 아무도 없을경우로 하여 작성합니다. if ( registeredLecture != null){ System.out.println("이미 수강중인 과목이 있습니다. 취소 먼저 하세요."); return false; } |
- 요청사항
public boolean addStudent() {
// 1. 현재 수강 인원이 최대 인원보다 적은지 확인합니다.
// 2. 여유가 있다면 currentStudents를 1 증가시키고 true를 반환합니다.
// 3. 강의가 이미 가득 찼다면 false를 반환합니다.
}
public boolean addStudent() { // 1. 현재 수강 인원이 최대 인원보다 적은지 확인합니다. if(currentStudents >= maxStudents){ System.out.println("이과목은 수강인원이 다 차서 수강을 할 수 없습니다."); return false; } // 2. 여유가 있다면 currentStudents를 1 증가시키고 true를 반환합니다. currentStudents = currentStudents + 1; // 3. 강의가 이미 가득 찼다면 false를 반환합니다. return true; } |
- 요청사항
public boolean registerLecture(Lecture lecture) {
// 2. 수강 중인 강의가 없다면, 주어진 강의에 학생을 등록합니다.
// 3. 강의 등록이 성공하면 registeredLecture를 업데이트하고 true를 반환합니다.
// 4. 실패하면 (이미 수강 중이거나 강의가 가득 찼을 경우) false를 반환합니다.
package entity; public class Lecture { // 과목명 private String lectureName; // 최대 수강 인원 private int maxStudents; // 현재 수강 인원 private int currentStudents; public Lecture(String lectureName, int maxStudents) { // 생성자 생성 String lectureName, maxStudents this.lectureName = lectureName; this.maxStudents = maxStudents; } public boolean addStudent() { // 1. 현재 수강 인원이 최대 인원보다 적은지 확인합니다. if(currentStudents >= maxStudents){ System.out.println("이과목은 수강인원이 다 차서 수강을 할 수 없습니다."); return false; } // 2. 여유가 있다면 currentStudents를 1 증가시키고 true를 반환합니다. currentStudents = currentStudents + 1; // 3. 강의가 이미 가득 찼다면 false를 반환합니다. return true; } |
- 요청사항
public void printInfo() {
// 학생의 이름, 학번을 출력합니다.
// 수강 중인 강의가 있다면 강의명도 출력합니다.
// 수강 중인 강의가 없다면 "수강 중인 강의 없음"을 출력합니다.
}
}
package entity; public class Lecture { // 과목명 private String lectureName; // 최대 수강 인원 private int maxStudents; // 현재 수강 인원 private int currentStudents; public Lecture(String lectureName, int maxStudents) { this.lectureName = lectureName; this. maxStudents= maxStudents; } public boolean addStudent() { // 1. 현재 수강 인원이 최대 인원보다 적은지 확인합니다. if(currentStudents >= maxStudents){ System.out.println("이과목은 수강인원이 다 차서 수강을 할 수 없습니다."); return false; } // 2. 여유가 있다면 currentStudents를 1 증가시키고 true를 반환합니다. currentStudents = currentStudents + 1; // 3. 강의가 이미 가득 찼다면 false를 반환합니다. return true; } public boolean removeStudent() { // 1. 현재 수강 인원이 0보다 큰지 확인합니다. // 2. 수강 중인 학생이 있다면 currentStudents를 1 감소시키고 true를 반환합니다. // 3. 수강 중인 학생이 없다면 false를 반환합니다. return true; } public void printInfo() { // 강의명, 현재 수강 인원, 최대 수강 인원을 출력합니다. } // Gutter/Setter 생성 // Student클래스 에서 // System.out.println("수강중인 강의는 : "+registeredLecture.getLectureName() ); } 중 getLectureName에 사용됩니다. public String getLectureName() { return lectureName; } public void setLectureName(String lectureName) { this.lectureName = lectureName; } public int getMaxStudents() { return maxStudents; } public void setMaxStudents(int maxStudents) { this.maxStudents = maxStudents; } public int getCurrentStudents() { return currentStudents; } public void setCurrentStudents(int currentStudents) { this.currentStudents = currentStudents; } } |
package entity; public class Student { // 학생 이름 private String name; // 학번 private String studentId; // 학생은 과목을 하나만 수강 가능. private entity.Lecture registeredLecture; public Student(String name, String studentId) { this.name = name; this.studentId = studentId; } public boolean registerLecture(entity.Lecture lecture) { // 1. 이미 수강 중인 강의가 있는지 확인합니다. if ( registeredLecture != null){ System.out.println("이미 수강중인 과목이 있습니다. 취소 먼저 하세요."); return false; } // 2. 수강 중인 강의가 없다면, 주어진 강의에 학생을 등록합니다. if ( registeredLecture () == false){ return false; } // 3. 강의 등록이 성공하면 registeredLecture를 업데이트하고 true를 반환합니다. registeredLecture = lecture; // 4. 실패하면 (이미 수강 중이거나 강의가 가득 찼을 경우) false를 반환합니다. return true; } public boolean cancelLecture() { // 1. 현재 수강 중인 강의가 있는지 확인합니다. // 2. 수강 중인 강의가 있다면, 해당 강의에서 학생을 제거합니다. // 3. registeredLecture를 null로 설정합니다. // 4. 취소가 성공하면 true, 실패하면 false를 반환합니다. return true; } public void printInfo() { // 학생의 이름, 학번을 출력합니다. System.out.println(name + " , 학번 : " + studentId); // 수강 중인 강의가 있다면 강의명도 출력합니다. if(registeredLecture == null){ System.out.println("수강 중인 강의 없음"); }else{ System.out.println( "수강중인 강의는 : "+registeredLecture.getLectureName() ); } // 수강 중인 강의가 없다면 "수강 중인 강의 없음"을 출력합니다. // Lecture 클래스 에서 Gutter/Setter 생성 된 getLectureName이 위에 사용됩니다. } } |
'JAVA > 실습' 카테고리의 다른 글
32. [ JAVA ] 상속: 예시 (0) | 2024.12.16 |
---|---|
27. [ JAVA ] 메소드 오버로딩 예시 1 (2) | 2024.12.13 |
23. [ JAVA ] 클래스(Class)와 객체(Instance) : 예시 (0) | 2024.12.12 |