Home > Java > javaTutorial > body text

How to use Java to write a simple query system for student course enrollment statistics results?

WBOY
Release: 2023-11-02 11:06:53
Original
769 people have browsed it

How to use Java to write a simple query system for student course enrollment statistics results?

How to use Java to write a simple query system for student course enrollment statistics results?

With the popularization of education and the development of information technology, the school's course selection system has become an essential component. In order to facilitate school administrators to query the statistical results of course enrollment, we can use Java to write a simple query system for course enrollment statistics. This article will introduce you how to implement this system using Java language.

First, we need to define a student class to save student information and selected courses. A simple student class includes the student's name, student number, and selected course.

public class Student {
    private String name;
    private String id;
    private List<String> courses;

    public Student(String name, String id, List<String> courses) {
        this.name = name;
        this.id = id;
        this.courses = courses;
    }

    // 省略getters和setters方法
}
Copy after login

Next, we need to define a course selection system class to manage students' course selection information and implement query functions. The course selection system class includes the student list and two important methods: adding students and querying the number of course selections.

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CourseSelectionSystem {
    private List<Student> students;

    public CourseSelectionSystem(){
        students = new ArrayList<>();
    }

    // 添加学生
    public void addStudent(Student student){
        students.add(student);
    }

    // 查询选课人数
    public int getCourseSelectionCount(String courseName){
        int count = 0;
        for(Student student : students){
            if(student.getCourses().contains(courseName)){
                count++;
            }
        }
        return count;
    }

    public static void main(String[] args){
        CourseSelectionSystem system = new CourseSelectionSystem();

        // 添加学生数据
        List<String> courses1 = new ArrayList<>();
        courses1.add("Math");
        courses1.add("English");
        Student student1 = new Student("张三", "001", courses1);

        List<String> courses2 = new ArrayList<>();
        courses2.add("Computer Science");
        courses2.add("English");
        Student student2 = new Student("李四", "002", courses2);

        List<String> courses3 = new ArrayList<>();
        courses3.add("Math");
        courses3.add("Physics");
        Student student3 = new Student("王五", "003", courses3);

        system.addStudent(student1);
        system.addStudent(student2);
        system.addStudent(student3);

        // 查询选课人数
        int count = system.getCourseSelectionCount("English");
        System.out.println("选修英语课程的人数为:" + count);
    }
}
Copy after login

In the above code, we first created a course selection system object and added three student data. Then, we call the method of querying the number of course enrollees, pass in the course name "English" to query, and print out the results.

Through this simple example, we can see how to use Java to write a query system for student course enrollment statistics results. You can expand the system according to actual needs, such as adding more student information, increasing the flexibility of query functions, etc. I hope this article can help you understand and use Java to write a simple course enrollment statistics result query system.

The above is the detailed content of How to use Java to write a simple query system for student course enrollment statistics results?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template