Advertisement
panaewboi

club 23Oct2019

Oct 23rd, 2019
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.90 KB | None | 0 0
  1. public class Club extends Student{
  2.  
  3.     private String clubName; //แต่ละชมรมต้องมีชื่อชมรม
  4.     private Student[] studentInClub; //นักศึกษาที่เป็นสมาชิกชมรม(แต่ละคน)
  5.     private int countMember; //จำนวนนักศึกษาของแต่ละชมรม มีนักศึกษาเข้าร่วมได้มากกว่า1คน
  6.  
  7.     public Club(String clubName, int size) {
  8.         this.clubName = clubName;
  9.         this.studentInClub = new Student[size]; //การเปิดชมรมใหม่ จะต้องระบุจำนวนสมาชิกที่รับได้สูงสุด
  10.     }
  11.  
  12.     //CHECK♥
  13.     public boolean addStudent(Student addS) { //เพิ่มนักศึกษาเข้าชมรม
  14.         if (addS == null || clubIsFull() || searchStudent(addS) != -1) {
  15.             return false;
  16.         }
  17.         this.studentInClub[countMember] = addS; //นักศึกษาลงทะเบียนเข้าชมรม1คน
  18.         countMember++; //เมื่อลงทะเบียนเสร็จแล้ว จำนวนนักศึกษาก็จะเพิ่มเข้าชมรม1คน
  19.         return true;
  20.     }
  21.    
  22.     //CHECK♥
  23.     public boolean removeStudent(Student s) { //ลบนักศึกษาออกจากชมรม
  24.     if (s == null || studentInClub.length == 0) {
  25.             return false;
  26.         }
  27.         for (int i = 0; i < countMember; i++) { //วนหานักศึกษาในชมรม
  28.             if (studentInClub[i].equals(s)) {
  29.                 if (i != (countMember - 1)) {
  30.                     this.studentInClub[i] = this.studentInClub[countMember - 1];
  31.                 }
  32.                 else {
  33.                     this.studentInClub[i] = null; //ให้ช่องว่างหลังสุดเป็นnullเพื่อรอรับค่าใหม่
  34.                 }
  35.                 countMember--; //ลบนักศึกษาออกไป 1 คน
  36.                 return true;
  37.             }
  38.         }
  39.         return false;  
  40.  
  41.     }
  42.    
  43.     public int searchStudent(Student student){ //ค้นหานักศึกษาในชมรม
  44.         for (int i = 0; i < countMember; i++){ //หาวนจนกว่าจะครบทุกคนในชมรม
  45.             if (studentInClub[i].equals(student)){ //ถ้าหานักศึกษที่รับเข้ามามีความเท่ากับตำแหน่งนักศึกษาในชมรม
  46.                 return i; //return ตำแหน่งที่พบ
  47.             }
  48.         }
  49.         return -1; //ไม่พบ return -1
  50.     }
  51.    
  52.     //CHECK♥
  53.     public boolean clubIsFull() {
  54.         return  countMember == this.studentInClub.length ;          
  55.     }
  56.    
  57.    
  58.     //CHECK ♥
  59.     public boolean clubStatus(){
  60.         if(this.clubIsFull()){ //กรณีที่มีสมาชิกครบแล้ว ให้มีสถานะเป็น CLOSED
  61.             System.out.println("Club status is CLOSED");
  62.         }
  63.         if(!this.clubIsFull()){ //ถ้ายังรับสมาชิกใหม่ได้ ให้มีสถานะ OPEN
  64.             System.out.println("Club status is OPEN");
  65.         }
  66.         return true;
  67.     }
  68.    
  69.    
  70.     //UPDATE STUDENT DATA >> edit firstname & lastname
  71.     //CHECK♥
  72.     public boolean editStudentFirstname(String newFirstname, Student s){ //ชื่อใหม่ กับ นักศึกษาคนที่จะเปลี่ยนชื่อ
  73.         int index = searchStudent(s);
  74.         if(index == -1){
  75.             return false;
  76.         }
  77.         studentInClub[index].setFirstname(newFirstname);
  78.         return true;
  79.     }
  80.    
  81.     //CHECK♥
  82.     public boolean editStudentLastname(String newLastname, Student s){ //นามสกุลใหม่ กับ นักศึกษาคนที่จะเปลี่ยนนามสกุล
  83.         int index = searchStudent(s);
  84.         if(index == -1){
  85.             return false;
  86.         }
  87.         studentInClub[index].setLastname(newLastname);
  88.         return true;
  89.     }
  90.    
  91.     //CHECK♥
  92.     public void listStudentInClub(){
  93.         for(int i=0 ; i< countMember ; i++){
  94.                     System.out.println(studentInClub[i]);
  95.         }
  96.     }
  97.    
  98.     //CHECK♥
  99.     @Override
  100.     public String toString() {
  101.         StringBuilder s1 = new StringBuilder();
  102.         for(int i=0 ;i<this.countMember; i++ ){
  103.             s1.append(this.studentInClub[i]);
  104.             s1.append("\n");
  105.         }
  106.         return s1.toString();
  107. //        return "Club >> clubName: " + clubName + ", countMember:" + countMember;
  108.     }
  109.  
  110.    
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement