Java之基于集合的学生管理系统

文章目录

      • 实验目的
      • 实验展示
      • 实验源码




实验目的

练习集合中的增、删、改、查的基本操作。




实验展示

添加

Java之基于集合的学生管理系统_第1张图片


查询

Java之基于集合的学生管理系统_第2张图片

其他功能就一一展示啦,直接看下面源码。




实验源码

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

/**
 * 学生管理系统 v1.0
 * @author xiaomingxing
 * @create 2020/3/23 16:27
 */

// 实体类
class Student {
    private String studentName;
    private int studentAge;
    private String studentSex;

    public Student() {
    }

    public Student(String studentName, int studentAge, String studentSex) {
        this.studentName = studentName;
        this.studentAge = studentAge;
        this.studentSex = studentSex;
    }

    public String getStudentName() {
        return studentName;
    }

    public void setStudentName(String studentName) {
        this.studentName = studentName;
    }

    public int getStudentAge() {
        return studentAge;
    }

    public void setStudentAge(int studentAge) {
        this.studentAge = studentAge;
    }

    public String getStudentSex() {
        return studentSex;
    }

    public void setStudentSex(String studentSex) {
        this.studentSex = studentSex;
    }

    @Override
    public String toString() {
        return "名字:"+studentName+" 年龄:"+studentAge+" 性别:"+studentSex;
    }
}


public class StudentManagementSystem {

    private static Scanner sc = new Scanner(System.in);
    private static List<Student> studentList = new ArrayList<>();

    public static void main(String[] args) {

        while (true) {
            logo();
            System.out.println("请输入你的选择:");
            int choose = sc.nextInt();
            if (choose == 0)
                return;

            switch (choose) {
                case 1:
                    add();
                    break;

                case 2:
                    query();
                    break;

                case 3:
                    update();
                    break;

                case 4:
                    delete();
                    break;

                    default:
                        break;
            }

            choose = 0;
        }

    }

    // logo
    public static void logo() {
        System.out.println("*************");
        System.out.println("*\t1.添加\t*");
        System.out.println("*\t2.查询\t*");
        System.out.println("*\t3.修改\t*");
        System.out.println("*\t4.删除\t*");
        System.out.println("*\t0.退出\t*");
        System.out.println("*************");
    }


    public static void add() {

        System.out.println("请输入姓名:");
        String studentName = sc.next();

        System.out.println("请输入年龄:");
        int studentAge = sc.nextInt();

        System.out.println("请输入性别:");
        String studentSex = sc.next();

        Student student = new Student(studentName,studentAge,studentSex);

        if (studentList.add(student))
            System.out.println("添加成功");
        else
            System.out.println("添加失败");
    }

    public static void query() {
        studentList.forEach(System.out::println);
    }

    // 更新
    public static void update() {
        System.out.println("请输入要修改的学生姓名:");
        String studentName = sc.next();

        Iterator<Student> iterator = studentList.iterator();
        while (iterator.hasNext()) {
            Student student = iterator.next();
            if (studentName.equals(student.getStudentName())) {
                System.out.println("请输入新的姓名:");
                String studentNewName = sc.next();
                student.setStudentName(studentNewName);

                System.out.println("请输入年龄:");
                int studentNewAge = sc.nextInt();
                student.setStudentAge(studentNewAge);

                System.out.println("请输入性别:");
                String studentNewSex = sc.next();
                student.setStudentSex(studentNewSex);

                System.out.println("修改完成");
            }
        }
    }

    public static void delete() {
        System.out.println("请输入要删除的学生姓名:");
        String delName = sc.next();

        Iterator<Student> iterator = studentList.iterator();
        while (iterator.hasNext()) {
            Student student = iterator.next();
            if (delName.equals(student.getStudentName())) {
                iterator.remove();
                System.out.println("删除完成");
            }
        }
    }

}


注:上面源码中,并没有考虑到性能、美观等相关问题,还请各位大佬,不喜勿喷。

你可能感兴趣的:(Java学习笔记)