Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Club extends Student{
- private String clubName; //แต่ละชมรมต้องมีชื่อชมรม
- private Student[] studentInClub; //นักศึกษาที่เป็นสมาชิกชมรม(แต่ละคน)
- private int countMember; //จำนวนนักศึกษาของแต่ละชมรม มีนักศึกษาเข้าร่วมได้มากกว่า1คน
- public Club(String clubName, int size) {
- this.clubName = clubName;
- this.studentInClub = new Student[size]; //การเปิดชมรมใหม่ จะต้องระบุจำนวนสมาชิกที่รับได้สูงสุด
- }
- //CHECK♥
- public boolean addStudent(Student addS) { //เพิ่มนักศึกษาเข้าชมรม
- if (addS == null || clubIsFull() || searchStudent(addS) != -1) {
- return false;
- }
- this.studentInClub[countMember] = addS; //นักศึกษาลงทะเบียนเข้าชมรม1คน
- countMember++; //เมื่อลงทะเบียนเสร็จแล้ว จำนวนนักศึกษาก็จะเพิ่มเข้าชมรม1คน
- return true;
- }
- //CHECK♥
- public boolean removeStudent(Student s) { //ลบนักศึกษาออกจากชมรม
- if (s == null || studentInClub.length == 0) {
- return false;
- }
- for (int i = 0; i < countMember; i++) { //วนหานักศึกษาในชมรม
- if (studentInClub[i].equals(s)) {
- if (i != (countMember - 1)) {
- this.studentInClub[i] = this.studentInClub[countMember - 1];
- }
- else {
- this.studentInClub[i] = null; //ให้ช่องว่างหลังสุดเป็นnullเพื่อรอรับค่าใหม่
- }
- countMember--; //ลบนักศึกษาออกไป 1 คน
- return true;
- }
- }
- return false;
- }
- public int searchStudent(Student student){ //ค้นหานักศึกษาในชมรม
- for (int i = 0; i < countMember; i++){ //หาวนจนกว่าจะครบทุกคนในชมรม
- if (studentInClub[i].equals(student)){ //ถ้าหานักศึกษที่รับเข้ามามีความเท่ากับตำแหน่งนักศึกษาในชมรม
- return i; //return ตำแหน่งที่พบ
- }
- }
- return -1; //ไม่พบ return -1
- }
- //CHECK♥
- public boolean clubIsFull() {
- return countMember == this.studentInClub.length ;
- }
- //CHECK ♥
- public boolean clubStatus(){
- if(this.clubIsFull()){ //กรณีที่มีสมาชิกครบแล้ว ให้มีสถานะเป็น CLOSED
- System.out.println("Club status is CLOSED");
- }
- if(!this.clubIsFull()){ //ถ้ายังรับสมาชิกใหม่ได้ ให้มีสถานะ OPEN
- System.out.println("Club status is OPEN");
- }
- return true;
- }
- //UPDATE STUDENT DATA >> edit firstname & lastname
- //CHECK♥
- public boolean editStudentFirstname(String newFirstname, Student s){ //ชื่อใหม่ กับ นักศึกษาคนที่จะเปลี่ยนชื่อ
- int index = searchStudent(s);
- if(index == -1){
- return false;
- }
- studentInClub[index].setFirstname(newFirstname);
- return true;
- }
- //CHECK♥
- public boolean editStudentLastname(String newLastname, Student s){ //นามสกุลใหม่ กับ นักศึกษาคนที่จะเปลี่ยนนามสกุล
- int index = searchStudent(s);
- if(index == -1){
- return false;
- }
- studentInClub[index].setLastname(newLastname);
- return true;
- }
- //CHECK♥
- public void listStudentInClub(){
- for(int i=0 ; i< countMember ; i++){
- System.out.println(studentInClub[i]);
- }
- }
- //CHECK♥
- @Override
- public String toString() {
- StringBuilder s1 = new StringBuilder();
- for(int i=0 ;i<this.countMember; i++ ){
- s1.append(this.studentInClub[i]);
- s1.append("\n");
- }
- return s1.toString();
- // return "Club >> clubName: " + clubName + ", countMember:" + countMember;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement