当前位置:   article > 正文

使用Java制作一个学生数据管理系统,初学Java入门!_数据管理系统java

数据管理系统java

一,这里是基于录入学生信息来制作的一个学生管理系统,只是一个面对对象的小练习。仅供初学Java熟悉Java使用。

下面是代码格式:

代码部分:
 

//创建一个学生类

package wang.com;

public class students {

    // 学号

    private String sid;

    // 姓名

    private String name;

    // 年龄

    private String age;

    // 居住地

    private String address;

    // 创建getset方法

    public String getSid() {

        return sid;

    }

    public void setSid(String sid) {

        this.sid = sid;

    }

    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 wang.com;

import java.util.ArrayList;

import javax.swing.*;

import java.awt.*;

import java.util.Scanner;

public class studentsManager {

       public static void main(String[] agrs) {

              // 创建集合来存储学生数据

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

              // new Student().setVisible(true);

              // 使用死循环

              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("输入你的选择(1~5):");

                     // Scanner录入数据

                     Scanner sc = new Scanner(System.in);

                     String line = sc.nextLine();

                     // 采用switch语句完成操作内容

                     switch (line) {

                     case "1":

                            // System.out.println("添加学生");

                            addStudent(array);

                            break;

                     case "2":

                            // System.out.println("删除学生");

                            deleteStudent(array);

                            break;

                     case "3":

                            // System.out.println("修改学生");

                            updataStudent(array);

                            break;

                     case "4":

                            // System.out.println("查看所有学生");

                            displayStudent(array);

                            break;

                     case "5":

                            System.out.println("谢谢使用");

                            System.exit(0);      // JVM退出

                     default:

                            System.out.println("暂无额外功能,请根据提示选择功能");

                     }

              }

       }

       // UI界面(暂未添加)

       public static void StudentUI() {

              JButton addButton, deleteButton, modifyButton, viewAllButton, exitButton;

              JTextArea studentListArea;

              // 初始化组件

              addButton = new JButton("添加学生");

              deleteButton = new JButton("删除学生");

              modifyButton = new JButton("修改学生");

              viewAllButton = new JButton("打印所有学生");

              exitButton = new JButton("退出系统");

              studentListArea = new JTextArea();

              // 添加组件到面板

              JPanel buttonPanel = new JPanel();

              buttonPanel.add(addButton);

              buttonPanel.add(deleteButton);

              buttonPanel.add(modifyButton);

              buttonPanel.add(viewAllButton);

              buttonPanel.add(exitButton);

              // 设置窗口属性

              JFrame frame = new JFrame("学生管理系统");

              // 设置布局管理器

              frame.setLayout(new BorderLayout());

              frame.add(buttonPanel, BorderLayout.NORTH);

              frame.add(new JScrollPane(studentListArea), BorderLayout.CENTER);

              frame.setSize(400, 300);

              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

              frame.setLocationRelativeTo(null);

       }

       // “输入内容且不能为空”方法

       public static String noEmpty() {

              String input;

              Scanner scanner = new Scanner(System.in);

              input = scanner.nextLine();

              while (true) {

                     if (!input.isEmpty()) {

                            return input;

                     } else {

                            System.out.println("输入内容不能为空,请重新输入!");

                     }

                     input = scanner.nextLine();

              }

              // System.out.println("您输入的内容是:" + input);

       }

       // “学号是否重复”方法

       public static boolean isUsed(ArrayList<students> array, String sid) {

              boolean flag = false;

              for (int i = 0; i < array.size(); i++) {

                     students s = array.get(i);

                     if (s.getSid().equals(sid)) {

                            flag = true;

                            break;

                     }

              }

              return flag;

       }

       // “添加学生”方法

       public static void addStudent(ArrayList<students> array) {

              Scanner sc = new Scanner(System.in);

              String sid;

              // 输入并核实学号是否重复

              while (true) {

                     System.out.println("请输入学号:");

                     sid = noEmpty();

                     boolean flag = isUsed(array, sid);

                     if (flag) {

                            System.out.println("你输入的学号已经被使用,请重新输入:");

                     } else {

                            break;

                     }

              }

              System.out.println("请输入学生姓名:");

              String name = noEmpty();

              // age应该合理

              String age;

              while (true) {

                     System.out.println("请输入学生年龄:");

                     age = noEmpty();

                     int num = Integer.parseInt(age);

                     if (num < 0 || num > 100) {

                            System.out.println("你输入的年龄有误,请重新输入:");

                     } else {

                            break;

                     }

              }

              System.out.println("请输入学生居住地:");

              String address = noEmpty();

              // 创建学生对象并将键盘输入赋值

              students s = new students();

              s.setSid(sid);

              s.setName(name);

              s.setAge(age);

              s.setAddress(address);

              // 往array里加入s

              array.add(s);

              System.out.println("已添加学生:" + s.getName());

       }

       // “查看学生”方法

       public static void displayStudent(ArrayList<students> array) {

              if (array.size() == 0) {

                     System.out.println("无任何学生信息,请补充后查询");

              } else {

                     // 显示表头

                     System.out.println("学号\t\t姓名\t年龄\t居住地");

                     // 将集合中的数据对应显示

                     for (int i = 0; i < array.size(); i++) {

                            students s = array.get(i);

                            System.out.println(s.getSid() + "\t" + s.getName() + "\t" + s.getAge() + "岁\t" + s.getAddress());

                     }

              }

              // System.out.println(array);

       }

       // “删除学生”方法

       public static void deleteStudent(ArrayList<students> array) {

              Scanner sc = new Scanner(System.in);

              if (array.size() == 0) {

                     System.out.println("无任何学生信息,请补充后操作");

              } else {

                     System.out.println("请输入你要删除的学生的学号");

                     String sid = noEmpty();

                     // 遍历集合核实并删除对应对象

                     int index = -1;

                     for (int i = 0; i < array.size(); i++) {

                            students s = array.get(i);

                            if (s.getSid().equals(sid)) {

                                   // array.remove(i);

                                   index = i;

                                   break;

                            }

                     }

                     // 给出反馈

                     if (index == -1) {

                            System.out.println("该学生不存在,请核实学号输入");

                     } else {

                            array.remove(index);

                            System.out.println("删除成功");

                     }

              }

       }

       // “修改学生”方法

       public static void updataStudent(ArrayList<students> array) {

              Scanner sc = new Scanner(System.in);

              if (array.size() == 0) {

                     System.out.println("无任何学生信息,请补充后操作");

              } else {

                     System.out.println("请输入你要修改的学生的学号");

                     String sid = noEmpty();

                     // 核实学号

                     int index = -1;

                     for (int j = 0; j < array.size(); j++) {

                            students std = array.get(j);

                            if (std.getSid().equals(sid)) {

                                   index = 1;

                                   break;

                            }

                     }

                     if (index == -1) {

                            System.out.println("该学生不存在,请核实学号输入");

                            return;

                     }

                     // 键盘输入修改后的学生

                     System.out.println("请输入学生的新姓名:");

                     String name = noEmpty();

                     System.out.println("请输入学生的新年龄:");

                     String age = noEmpty();

                     System.out.println("请输入学生的新居住地:");

                     String address = noEmpty();

                     // 创建学生对象

                     students s = new students();

                     s.setSid(sid);

                     s.setName(name);

                     s.setAge(age);

                     s.setAddress(address);

                     // 遍历集合修改对应信息

                     for (int i = 0; i < array.size(); i++) {

                            students student = array.get(i);

                            if (student.getSid().equals(sid)) {

                                   array.set(i, s); // 用s替换i

                                   break;

                            }

                     }

                     // 给出反馈提示

                     System.out.println("学生信息修改成功");

              }

       }

}

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/运维做开发/article/detail/790174
推荐阅读
相关标签
  

闽ICP备14008679号