Java教程

Java学习笔记-学生管理系统

本文主要是介绍Java学习笔记-学生管理系统,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

Java学习笔记
一个Student类

public class Student {
    private String sid;
    private String name;
    private String age;
    private String address;

    public Student() {
    }

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

    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;
    }
}

一个Student的测试类

import java.util.ArrayList;
import java.util.Scanner;

public class Student_Demo {
    public static void main(String[] args) {
        ArrayList<Student> array = new ArrayList<>();
        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 sc = new Scanner(System.in);
            String line = sc.nextLine();
            switch (line) {
                case "1":
//                    System.out.println("添加学生成功");
                    addStudent(array);
                    break;
                case "2":
//                    System.out.println("修改学生成功");
                    modifyStudent(array);
                    break;
                case "3":
//                    System.out.println("删除学生成功");
                    delStudent(array);
                    break;
                case "4":
//                    System.out.println("查看学生成功");
                    showStudent(array);
                    break;
                case "5":
                    System.out.println("谢谢使用");
//                    break;
                    return;
//                    System.exit(0);
            }
        }
    }
    // 添加学生
    // 传入参数是学生数组,添加学生对象,不返回
    public static void addStudent(ArrayList<Student> array) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入学号:");
        String sid = sc.nextLine();
        boolean flag = false;
        for (int i = 0; i < array.size(); i++) {
            if (sid.equals(array.get(i).getSid())) {
                flag = true;
            }
        }
        if (flag) {
            System.out.println("有内鬼,停止交易!");
        } else {
            System.out.println("请输入姓名:");
            String name = sc.nextLine();
            System.out.println("请输入年龄:");
            String age = sc.nextLine();
            System.out.println("请输入地址:");
            String address = sc.nextLine();
            Student student = new Student();
            student.setSid(sid);
            student.setName(name);
            student.setAge(age);
            student.setAddress(address);
            array.add(student);
            System.out.println("添加学生成功!");
        }
    }
    // 删除学生
    // 传入的参数是学生数组,删除学生对象,不返回
    public static void delStudent(ArrayList<Student> array){
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入您要删除学生的学号:");
        String sid = sc.nextLine();
        boolean flag = true;
        // j用来获取要删除对象的索引
        int j = 0;
        for (int i = 0; i < array.size(); i++) {
            if (sid.equals(array.get(i).getSid())) {
                flag = false;
                j = i;
            }
        }
        if(flag){
            System.out.println("有内鬼,停止交易!");
        }else{
            System.out.println("正在删除.........");
            array.remove(array.get(j));
        }
    }
    // 修改学生
    // 选择修改学生的学号,然后可以修改的对象
    public static void modifyStudent(ArrayList<Student> array){
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入您要修改学生的学号:");
        String sid = sc.nextLine();
        boolean flag = false;
        int j = 0;
        for (int i = 0; i < array.size(); i++) {
            if (sid.equals(array.get(i).getSid())) {
                flag = true;
                j = i;
            }
        }
        if(flag){
            System.out.println("修改的学号为:");
            sid = sc.nextLine();
            array.get(j).setSid(sid);
            System.out.println("修改的姓名为:");
            sid = sc.nextLine();
            array.get(j).setName(sid);
            System.out.println("修改的年龄为:");
            sid = sc.nextLine();
            array.get(j).setAge(sid);
            System.out.println("修改的地址为:");
            sid = sc.nextLine();
            array.get(j).setAddress(sid);
            System.out.println("修改成功,请进行查看");
        }else{
            System.out.println("有内鬼,终止交易!");
        }
    }
    // 查看所有学生
    // 传入参数是学生数组,进行遍历,不返回
    public static void showStudent(ArrayList<Student> array) {
        if (array.size() != 0) {
            System.out.println("学号\t\t"+"姓名\t\t"+"年龄\t\t"+"地址");
            for (int i = 0; i < array.size(); i++) {
                System.out.printf("%-8s",array.get(i).getSid());
                System.out.printf("%-8s",array.get(i).getName());
                System.out.printf("%-8s",array.get(i).getAge());
                System.out.printf("%-8s",array.get(i).getAddress());
                System.out.println();
            }

            System.out.println("查看完毕");
        }
        else{
            System.out.println("请先添加学生,再进行查看!");
        }
    }
}
这篇关于Java学习笔记-学生管理系统的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!