Home > Java > javaTutorial > body text

How to write a simple student information management system in Java?

WBOY
Release: 2023-11-04 17:05:04
Original
1034 people have browsed it

How to write a simple student information management system in Java?

Java is a popular programming language suitable for a variety of different application development. A common Java application is a student information management system. Through this article, we will introduce how to use Java to write a simple student information management system so that students, teachers, and administrators can access and manage student information.

  1. Create Student Class

First, we need to create a Java class to represent students. This class should include information such as the student's name, student number, course, and grades.

Creating a class in Java is very simple, just use the class keyword followed by the class name. The following is a sample code for a simple student class:

public class Student {
   private String name;
   private int id;
   private String course;
   private int score;
  
   // 构造函数
   public Student(String name, int id, String course, int score) {
       this.name = name;
       this.id = id;
       this.course = course;
       this.score = score;
   }
  
   // 获取学生姓名
   public String getName() {
       return this.name;
   }
  
   // 获取学生学号
   public int getId() {
       return this.id;
   }
  
   // 获取学生课程
   public String getCourse() {
       return this.course;
   }
  
   // 获取学生成绩
   public int getScore() {
       return this.score;
   }
}
Copy after login

In this example, we define variables such as name, ID, course, and score in the class, and write the constructor and method to obtain the values ​​​​of these variables. function.

  1. Create a student information management system

Once we have the student class, we can create a management system to store and manage the information of these students.

We can use the ArrayList class in the Java collection framework to store student information. The following is a sample code for a simple student information management system:

import java.util.ArrayList;

public class StudentManagementSystem {
   private ArrayList<Student> students;
  
   // 构造函数
   public StudentManagementSystem() {
       students = new ArrayList<>();
   }
  
   // 添加学生
   public void addStudent(Student student) {
       students.add(student);
   }
  
   // 根据学号查找学生
   public Student findStudentById(int id) {
       for (Student student : students) {
           if (student.getId() == id) {
               return student;
           }
       }
       return null;
   }
  
   // 根据姓名查找学生
   public ArrayList<Student> findStudentByName(String name) {
       ArrayList<Student> result = new ArrayList<>();
       for (Student student : students) {
           if (student.getName().equals(name)) {
               result.add(student);
           }
       }
       return result;
   }
  
   // 根据学号删除学生
   public void deleteStudent(int id) {
       students.removeIf(student -> student.getId() == id);
   }
  
   // 获取所有学生
   public ArrayList<Student> getAllStudents() {
       return students;
   }
}
Copy after login

In the above code, we create a student information management system class, which includes a list of students. We also defined some functions, such as adding and finding students, for management purposes.

  1. Using the Student Information Management System

Once we have the student information management system, we can use it to manage student information. The following is a simple Java application that demonstrates how to use the student information management system:

public class Main {
   public static void main(String[] args) {
       // 创建一个学生信息管理系统
       StudentManagementSystem system = new StudentManagementSystem();
      
       // 添加一些学生
       system.addStudent(new Student("张三", 1001, "Java编程", 80));
       system.addStudent(new Student("李四", 1002, "Python编程", 90));
       system.addStudent(new Student("王五", 1003, "C++编程", 85));
      
       // 查找学生
       Student student1 = system.findStudentById(1002);
       System.out.println(student1.getName() + " " + student1.getCourse() + " " + student1.getScore());
      
       ArrayList<Student> students = system.findStudentByName("张三");
       for (Student student : students) {
           System.out.println(student.getName() + " " + student.getCourse() + " " + student.getScore());
       }
      
       // 删除学生
       system.deleteStudent(1001);
      
       // 获取所有学生
       ArrayList<Student> allStudents = system.getAllStudents();
       for (Student student : allStudents) {
           System.out.println(student.getName() + " " + student.getCourse() + " " + student.getScore());
       }
   }
}
Copy after login

In this simple example, we first create a student information management system and then add some students. Then we use the find function to find the student, the delete function to delete the student, and finally the get function to get all student information.

Summary

Here we just demonstrate how to use Java to write a simple student information management system. In actual applications, we may need to add some other functions, such as input and output processing, interface design, etc. Regardless, this article has provided a good starting point for beginners to better understand and use Java for application development.

The above is the detailed content of How to write a simple student information management system in Java?. For more information, please follow other related articles on the PHP Chinese website!

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