1. 程式人生 > >Java —— 有關儲存學生資訊陣列的操作

Java —— 有關儲存學生資訊陣列的操作

//class Test

public class Test {  
  
    public static void main(String[] args) {  
        // TODO Auto-generated method stub  
        Student s;  
        s = new Student("Beyond",2000272823);  
        System.out.println(s.toString());  
        System.out.println();  
        StudentsManagement p = new StudentsManagement();  
        System.out.println("The none List is: ");  
        p.printStudentsInfo();  
        p.addAnewStudent("Beyond",2001232625);  
        p.addAnewStudent("TopC",2001232625);  
        p.addAnewStudent("Sam",2000272823);
        System.out.println("The "+p.getNbStudents()+" objects of List is:");  
        p.printStudentsInfo();  
        System.out.println("After remove the List is :");  
        p.removeAStudent(2001232625);  
        p.printStudentsInfo();
        
    }  
}  



// class StudentsManagement

public class StudentsManagement {  
  
    private Student[] students;  
    private int nbStudents;  
      
    public StudentsManagement(){  
        nbStudents = 0;  
        students = new Student[100];  
    }  
    public void addAnewStudent(String name,int number){  
        Student s = new Student(name,number); 
        int index = searchAstudent(number);
        if(index >= 0){
        	
        	System.out.println("A student already has this student number: "
        			+number);
        }
        else{
        	students[nbStudents] = s;  
        	++ nbStudents; 
        }
    }  
    
    public void removeAStudent(int number){  
        
        // The operator of remove
    	int index = searchAstudent(number);
        if(index == nbStudents-1){  
            nbStudents --;
        }
        else if(index < nbStudents - 1){  
            for(int i = index; i<nbStudents-1; i++){  
                students[i] = students[i+1];  
            }  
            nbStudents --;  
        }  
    }     
    
    // Search the Student whose number is number
    public int searchAstudent(int number){
    	
    	int index = -1;  
        for(int i = 0; i<nbStudents; i++){  
            if(students[i].getNumber() == number){  
//              Student d = students[i];  
//              students.remove(d);  
//              students[i] = null;  
                index = i;  
                break;  
            }  
        }  
        return index;
    }
    
    
      
    public void printStudentsInfo(){  
        if(nbStudents == 0) return ;  
        for(int i = 0; i<nbStudents; i++){  
            System.out.println(students[i]);  
        }  
        System.out.println();  
    }  
      
    public int getNbStudents() {  
        return nbStudents;  
    }  
    public void setNbStudents(int nbStudents) {  
        this.nbStudents = nbStudents;  
    }  
      
}  


//class Student

public class Student extends Person{  
      
    private int number;  
      
    //Default constructor  
    public Student(){  
        System.out.println("I am subclass constructor.");  
    }  
      
    public Student(String name,int number){  
//      super.setName(name);  
        super(name);  
        this.number = number;  
          
    }  
    public String toString(){  
        return "The name of this student is: "+super.getName()+".       And the number is: "+number;  
    }  
    //Accessor methods (get/set)  
    public int getNumber() {  
        return number;  
    }  
  
    public void setNumber(int number) {  
        this.number = number;  
    }  
}  


//class Person

public class Person {  
  
    private String name;  
      
    //Default constructor  
    public Person(){  
        System.out.println("I am the superclass constructor.");  
    }  
      
    public Person(String name){  
        this.name = name;  
    }  
    //Accessor methods (get/set)  
    public String getName() {  
        return name;  
    }  
  
    public void setName(String name) {  
        this.name = name;  
    }  
}