如何将多名学生添加到学生班?

时间:2017-02-21 20:38:13

标签: java class arraylist

我试图创建一个学生班,一个课程班和主班。我正在尝试将学生添加到课程中,当学生加入课程时,课程中的学生人数应该增加,当代码运行时,应该打印课程详细信息,然后是课程中的学生。

我有以下代码: 主要课程:

public class JavaLecture3 {
    public static final int DEBUG = 0;

    public static void main(String [] args){

        //Student student = new Student(); // Calling default constructor here.
        Course course = new Course();

        student = new Student(21, "Joe", "CSE", "07447832342");

        course = new Course("CSE", "Tom", 5);

        System.out.println("Course Information: ");
        System.out.println("-------------------");
        System.out.println(course);
        System.out.println();
        System.out.println("Student contains: "); // calls student.toString());        
        System.out.println("-------------------");
        System.out.println(student);
    }

}

课程类:

public class Course {

    ArrayList<Student> studentList;
    private String courseName;
    private String teacher;
    private int noOfStudents;

    //Getters
    public String getCourseName(){
        return this.courseName;
    }
    public int getNoOfStudents(){
        return this.noOfStudents;
    }
    public String getTeacher(){
        return this.teacher;
    }

    //Setters
    public void setCourseName(String courseName){
        this.courseName = courseName;
    }
    public void setNoOfStudents(int noOfStudents){
        this.noOfStudents = noOfStudents;
    }
    public void setTeacher(String teacher){
        this.teacher = teacher;
    }

    /**
     * Default constructor. Populates course name, number of students with defaults
     * 
     */
    public Course(){
        this.noOfStudents = 0;
        this.courseName = "Not Set";
        this.teacher = "Not Set";
        studentList = new ArrayList<Student>();
    }

    /** 
     * Constructor with parameters 
     * @param noOfStudents integer
     * @param courseName String with the Course name
     * @param teacher String with the teacher
    */
    public Course(String courseName, String teacher, int noOfStudents){
        this.courseName = courseName;
        this.teacher = teacher;
        noOfStudents = noOfStudents;
        studentList = new ArrayList<Student>();
    } 

   public static void addStudent(Student newStudent){
        if(studentList.size()==noOfStudents){
            System.out.println("The class is full, you cannot enrol.");
        }
        else {
            studentList.add(newStudent);
        }
    }

    public String toString() {
        return "Course Name: " + this.courseName + " Teacher: " + this.teacher 
                + " Number of Students: " + this.noOfStudents;
    }
}

学生班:

public class Student {
    private String name;
    private int age;
    public String gender = "na";
    private String course;
    private String phoneNo;
    public static int instances = 0;

    // Getters
    public int getAge(){
        return this.age;
    }
    public  String getName(){
        return this.name;
    }
    public String getCourse(){
        return this.course;
    }
    public String getPhoneNo(){
        return this.phoneNo;
    }

    // Setters
    public void setAge(int age){
        this.age = age;
    }
    public void setName(String name){
        if (JavaLecture3.DEBUG > 3) System.out.println("In Student.setName. Name = "+ name);

        this.name = name;
    }
    public void setCourse(String course){
        this.course = course;
    }
    public void setPhoneNo(String phoneNo){
        this.phoneNo = phoneNo;
    }


    /**
     * Default constructor. Populates name,age,gender,course and phone Number 
     * with defaults
     */
    public Student(){
        instances++;
        this.age = 18;
        this.name = "Not Set";
        this.gender = "Not Set";
        this.course = "Not Set";
        this.phoneNo = "Not Set";
    }

    /** 
     * Constructor with parameters 
     * @param age integer
     * @param name String with the name
     * @param course String with course name
     * @param phoneNo String with phone number
    */
    public Student(int age, String name, String course, String phoneNo){
        this.age = age;
        this.name = name;
        this.course = course;
        this.phoneNo = phoneNo;
    }

    /** 
     * Gender constructor
     * @param gender 
     */
    public Student(String gender){
        this(); // Must be the first line!
        this.gender = gender;

    }

    protected void finalize() throws Throwable{
        //do finalization here
        instances--;
        super.finalize(); //not necessary if extending Object.
    } 

    public String toString (){
        return "Name: " + this.name + " Age: " + this.age + " Gender: " 
                + this.gender + " Course: " + this.course + " Phone number: " 
                + this.phoneNo;
    }
}

4 个答案:

答案 0 :(得分:0)

由于您在课程类中初始化了数组列表,因此您应该将学生添加到其中。您应该在Course中创建一个方法,将对象添加到数组列表中,例如:

public void addStudent(Student s){
    studentList.add(s);
    noOfStudents++;
}

添加多名学生:

public void addStudents(Student[] students){
    for(int i = 0; i < students.length; i++){
        studentList.add(students[i]);
    }
}

你几乎就在那里,只需在数组列表中使用为你制作的.add方法。

答案 1 :(得分:0)

参考: http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html?is-external=true

Course.getNoOfStudents应该只返回studentList.size()。无需维护单独的变量。

打印学生列表(来自课程):

for(int i=0;i<studentList.size();i++) System.out.println(studentList.get(i));

答案 2 :(得分:0)

public class JavaLecture3 {
public static final int DEBUG = 0;

public static void main(String [] args){

    //Create course object
    Course course  = new Course("CSE", "Tom", 5);
    Scanner scanner = new Scanner(System.in);

    String cmd = "Yes";

    while(cmd.equals("Yes")){
       Student student = new Student();

       System.out.print("Enter a new student? ");
       cmd = scanner.next();

       if (cmd.equals("Yes")){
           //Read student name
           System.out.print("Enter a student name: ");
           String name = scanner.next();
           student.setName(name);

           //Read student Age
           System.out.print("Enter a student age: ");
           int age = scanner.nextInt();
           student.setAge(age);

           //Read student Course
           System.out.print("Enter a student course: ");
           String stdent_course = scanner.next();
           student.setCourse(stdent_course);

           //register the student to the class
           course.addStudent(student);
       }
    }

    scanner.close();

    System.out.println("Course Information: ");
    System.out.println("-------------------");
    System.out.println(course.toString());
    System.out.println();
}

}

我在Course类中重写了一些toString()方法。这应该打印参加课程的学生名单。

public String toString (){
    String ret_value =  "Name: " + this.name + " Age: " + this.age + " Gender: " 
            + this.gender + " Course: " + this.course + " Phone number: " 
            + this.phoneNo + " Students attending this course:";

    for (Student student: studentList) {
        ret_value = ret_value + " " + Student.getName();
    }

    return ret_value;
}

答案 3 :(得分:0)

public class JavaLecture3 {
    public static final int DEBUG = 0;

    public static void main(String [] args){

        Student student = new Student(); // Calling default constructor here.
        Course course = new Course();

        student = new Student(21, "Joe", "CSE", "07447832342");

        course = new Course("CSE", "Tom", 5);
        course.addStudent(student);

        System.out.println("Course Information: ");
        System.out.println("-------------------");
        System.out.println(course);
        System.out.println();
        System.out.println("Student contains: "); // calls student.toString());        
        System.out.println("-------------------");
        System.out.println(student);
    }
}

public class Course {

    List<Student> studentList;
    private String courseName;
    private String teacher;
    private int noOfStudents;

    // Getters
    public String getCourseName() {
        return this.courseName;
    }

    public int getNoOfStudents() {
        return this.noOfStudents;
    }

    public String getTeacher() {
        return this.teacher;
    }

    // Setters
    public void setCourseName(String courseName) {
        this.courseName = courseName;
    }

    public void setNoOfStudents(int noOfStudents) {
        this.noOfStudents = noOfStudents;
    }

    public void setTeacher(String teacher) {
        this.teacher = teacher;
    }

    /**
     * Default constructor. Populates course name, number of students with
     * defaults
     * 
     */
    public Course() {
        this.noOfStudents = 0;
        this.courseName = "Not Set";
        this.teacher = "Not Set";
        studentList = new ArrayList<Student>();
    }

    /**
     * Constructor with parameters
     * 
     * @param noOfStudents
     *            integer
     * @param courseName
     *            String with the Course name
     * @param teacher
     *            String with the teacher
     */
    public Course(String courseName, String teacher, int noOfStudents) {
        this.courseName = courseName;
        this.teacher = teacher;
        this.noOfStudents = noOfStudents;
        studentList = new ArrayList<Student>();
    }

    public void addStudent(Student newStudent) {
        if (studentList.size() == noOfStudents) {
            System.out.println("The class is full, you cannot enrol.");
        } else {
            studentList.add(newStudent);
        }
    }

    @Override
    public String toString() {
        return "Course Name: " + this.courseName + " Teacher: " + this.teacher
                + " Number of Students: " + studentList.size();
    }
}

public class Student {
    private String name;
    private int age;
    public String gender = "na";
    private String course;
    private String phoneNo;
    public static int instances = 0;

    // Getters
    public int getAge() {
        return this.age;
    }

    public String getName() {
        return this.name;
    }

    public String getCourse() {
        return this.course;
    }

    public String getPhoneNo() {
        return this.phoneNo;
    }

    // Setters
    public void setAge(int age) {
        this.age = age;
    }

    public void setName(String name) {
        if (JavaLecture3.DEBUG > 3)
            System.out.println("In Student.setName. Name = " + name);

        this.name = name;
    }

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

    public void setPhoneNo(String phoneNo) {
        this.phoneNo = phoneNo;
    }

    /**
     * Default constructor. Populates name,age,gender,course and phone Number
     * with defaults
     */
    public Student() {
        instances++;
        this.age = 18;
        this.name = "Not Set";
        this.gender = "Not Set";
        this.course = "Not Set";
        this.phoneNo = "Not Set";
    }

    /**
     * Constructor with parameters
     * 
     * @param age
     *            integer
     * @param name
     *            String with the name
     * @param course
     *            String with course name
     * @param phoneNo
     *            String with phone number
     */
    public Student(int age, String name, String course, String phoneNo) {
        this.age = age;
        this.name = name;
        this.course = course;
        this.phoneNo = phoneNo;
    }

    /**
     * Gender constructor
     * 
     * @param gender
     */
    public Student(String gender) {
        this(); // Must be the first line!
        this.gender = gender;

    }

    protected void finalize() throws Throwable {
        // do finalization here
        instances--;
        super.finalize(); // not necessary if extending Object.
    }

    public String toString() {
        return "Name: " + this.name + " Age: " + this.age + " Gender: "
                + this.gender + " Course: " + this.course + " Phone number: "
                + this.phoneNo;
    }
}