Java 简单版学生管理系统

家住魔仙堡 提交于 2020-07-27 15:01:05

学生管理系统

功能:1 添加学生

2 删除学生

3 修改学生信息

4 查看所有学生

 

 

学生类

import javax.print.DocFlavor;

public class Student {

    private String ID;
    private String name;
    private String age;
    private String address;

    public Student() {
    }

    public Student(String ID, String name, String age, String address) {
        this.ID = ID;
        this.name = name;
        this.age = age;
        this.address = address;
    }

    public String getID() {
        return ID;
    }

    public void setID(String ID) {
        this.ID = ID;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}

 

学生管理

package Student_Manage;

import com.sun.xml.internal.bind.v2.model.core.ID;
import com.sun.xml.internal.ws.wsdl.writer.document.http.Address;

import javax.naming.PartialResultException;
import javax.print.DocFlavor;
import javax.swing.plaf.synth.SynthTextAreaUI;
import javax.swing.text.StyledDocument;
import java.awt.dnd.DragGestureEvent;
import java.sql.SQLOutput;
import java.util.ArrayList;
import java.util.Scanner;

public class StudentManager {
    public static void main(String[] args) {

        ArrayList<Student> array = new ArrayList<Student>();

        while (true) {
            System.out.println("--------欢迎来到学生管理系统--------");
            System.out.println("1 添加学生");
            System.out.println("2 删除学生");
            System.out.println("3 修改学生");
            System.out.println("4 查看所有学生");
            System.out.println("5 退出");
            System.out.println("清输入你的选择:");
            Scanner in = new Scanner(System.in);
            String line = in.nextLine();
            switch (line) {
                case "1":
//                    System.out.println("添加学生");
                    addStudent(array);
                    break;
                case "2":
//                    System.out.println("删除学生");
                    deleteStudent(array);
                    break;
                case "3":
//                    System.out.println("修改学生");
                    updateStudent(array);
                    break;
                case "4":
//                    System.out.println("查看所有学生");
                    findAllStudent(array);
                    break;
                case "5":
                    System.out.println("谢谢使用");
                    System.exit(0);
            }

        }
    }

    public static void addStudent(ArrayList<Student> array) {
        Scanner in = new Scanner(System.in);

        String ID;
        while (true) {
            System.out.println("请输入学生学号");
            ID = in.nextLine();

            boolean flag = isUsed(array, ID);
            if (flag) {
                System.out.println("你输入的学号已经被使用,清重新输入");
            } else {
                break;
            }
        }
        System.out.println("请输入学生姓名");
        String name = in.nextLine();
        System.out.println("请输入学生年龄");
        String age = in.nextLine();
        System.out.println("请输入学生居住地");
        String address = in.nextLine();

        Student s = new Student();
        s.setID(ID);
        s.setAddress(address);
        s.setAge(age);
        s.setName(name);

        array.add(s);
        System.out.println("添加学生成功");
    }

    public static boolean isUsed(ArrayList<Student> array, String ID) {
        boolean flag = false;
        for (int i = 0; i < array.size(); i++) {
            Student s = array.get(i);
            if (s.getID().equals(ID)) {
                flag = true;
                break;
            }
        }
        return flag;
    }

    public static void findAllStudent(ArrayList<Student> array) {
        if (array.size() == 0) {
            System.out.println("无信息,请县添加信息再查询");
            return;
        }
        System.out.println("学号\t\t\t姓名\t\t年龄\t\t居住地");

        for (int i = 0; i < array.size(); i++) {
            Student s = array.get(i);
            System.out.println(s.getID() + "\t" + s.getName() + "\t" + s.getAge() + "岁\t" + s.getAddress());
        }
    }

    public static void deleteStudent(ArrayList<Student> array) {
        Scanner in = new Scanner(System.in);

        System.out.println("请输入要删除的学生学号");
        String ID = in.nextLine();

        int index = -1;
        for (int i = 0; i < array.size(); i++) {
            Student s = array.get(i);
            if (s.getID().equals(ID)) {
                index = i;
                break;
            }
        }
        if (index == -1) {
            System.out.println("该信息不存在,请重新输入");
        } else {
            array.remove(index);
            System.out.println("删除学生成功");
        }

    }

    public static void updateStudent(ArrayList<Student> array) {
        Scanner in = new Scanner(System.in);

        System.out.println("请输入要修改学生学号:");
        String ID = in.nextLine();

        System.out.println("请输入学生新姓名");
        String name = in.nextLine();
        System.out.println("请输入学生新年龄");
        String age = in.nextLine();
        System.out.println("请输入学生新居住地");
        String address = in.nextLine();

        Student s = new Student();
        s.setName(name);
        s.setAge(age);
        s.setAddress(address);
        s.setID(ID);

        for (int i = 0; i < array.size(); i++) {
            Student student = array.get(i);
            if (student.getID().equals(ID)) {
                array.set(i, s);
                break;
            }
        }
        System.out.println("修改成功");
    }
}

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!