API:学生管理系统(思路+写法)

API:学生管理系统(思路+写法)

学生管理系统实现思路:

1、定义学生类

2、主界面的代码编写

3、添加学生的代码编写

4、查看学生的代码编写

5、删除学生的代码编写

6、修改学生的代码编写

1.定义一个学生类,设置学生的属性
学生管理系统;

public class Student {
        private String id;
        private String name;
        private int age;
        private String birthday;

    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 int getAge() {
        return age;
    }

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

    public String getBirthday() {
        return birthday;
    }

    public void setBirthday(String birthday) {
        this.birthday = birthday;
    }

    public Student() {
    }

    public Student(String id, String name, int age, String birthday) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.birthday = birthday;
    }
}
2、主界面的代码编写思路:
1、用输出语句完成主界面的编写
2、用Scanner实现键盘录入数据
3、用switch语句完成操作的选择
4、用循环完成再次回到主界面
import java.util.ArrayList;
import java.util.Scanner;

public class StudentManager {
    //1.主界面
    public static void main(String[] args) {
        //1.1创建ArrayList集合,存储Student对象
        ArrayList<Student> list = new ArrayList<>();
        Scanner sc = new Scanner(System.in);
        //1.2主界面可以一直重复使用,while死循环
        while (true) {
            System.out.println("========欢迎使用学生管理系统========");
            System.out.println("请选择以下功能:\r\n1.添加学生信息\t2.删除学生信息\t3.修改学生信息\t4.查询学生信息\t5.退出学生管理系统");
            String choice = sc.next();

            //1.3根据choice执行不同的分支功能,使用switch
            switch (choice) {
                case "1":
                    addStudent(list);
                    break;
                case "2":
                    deleteStudent(list);
                    break;
                case "3":
                    updateStudent(list);
                    break;
                case "4":
                    findStudent(list);
                    break;
                case "5":
                    System.out.println("感谢使用本系统,欢迎下次使用!");
                    return;
                default:
                    System.out.println("您的选择有误,请检查后重新输入!");
            }
        }
    }
3、添加学生的代码编写思路:
1、用键盘录入选择添加学生
2、定义一个方法,用于添加学生:
1、显示提示信息,提示要输入何种信息
2、键盘录入学生对象所需的数据
3、创建学生对象,把键盘录入的数据赋值给学生对象的成员变量
4、将学生对象添加到集合中
5、给出添加成功的提示
public static void addStudent(ArrayList<Student> list) {
    System.out.println("=========添加学生信息=========");
    Scanner sc = new Scanner(System.in);
    String id;
    //2.开始录入学生信息,解决学号重复的问题,利用死循环不断让用户输入学号
    while (true) {
        System.out.println("请输入学生学号:");
        id = sc.next();
        //查询输入的id的索引
        int index = getIndex(list, id);
        //判断index是否是-1
        if (index == -1) {
            //说明这个id不存在,可以添加!结束循环
            break;
        } else {
            //说明这个id已经存在,得重新输入
            System.out.println("抱歉,您输入的id已被占用,请重新输入!");
        }
    }
    System.out.println("请输入学生的姓名:");
    String name = sc.next();
    System.out.println("请输入学生的年龄:");
    int age = sc.nextInt();
    System.out.println("请输入学生的生日:");
    String birthday = sc.next();
    //创建学生对象,封装录入学生信息
    Student stu = new Student(id, name, age, birthday);
    //存入集合
    list.add(stu);
    System.out.println("添加成功");
}
3.13.查询指定学号在集合中的索引:将此方法的返回值调用于其他方法
public static int getIndex(ArrayList<Student> list, String id) {
    //3.查询指定学号在集合中的索引
/*
明确参数:ArrayList<stdunt>list,String id
明确返回值:int
 */
    for (int i = 0; i < list.size(); i++) {
        //取出i索引位置的学生对象
        Student stu = list.get(i);
        //获取stu对象的id和方法传入的id比较内容是否相同
        if (id.equals(stu.getId())) {
            //成立说明找到了,返回索引i
            return i;
        }
    }
    //如果循环结束还没找到,方法还没结束,没有找到返回-1
    return -1;
}
4、查看看学生的代码思路:
1、用键盘录入选择查看所有学生信息
2、定义一个方法,用于查看学生信息:1、显示表格内容格式 2、降级和数据取出按照对应格式显示学生信息。
3、调用方法
public static void findStudent(ArrayList<Student> list) {
    //4.查询学生添加的信息
    /**
     * 明确参数:ArrayList<Student>list
     * 无返回值
     */
    System.out.println("=========查询所有学生信息========");
    //首先判断集合是否为空
    if (list.size() == 0) {
        System.out.println("该系统还未录入学生信息,请添加后重试!");
        return;
    }//打印表头
    System.out.println("学号\t\t姓名\t\t年龄\t\t生日");
    //遍历数组,获取集合索引i位置的学生对象
    for (int i = 0; i < list.size(); i++) {
        Student stu = list.get(i);
        //获取学生对象的各项信息
        String id = stu.getId();
        String name = stu.getName();
        int age = stu.getAge();
        String birthday = stu.getBirthday();

        //安装表头的格式打印数据
        System.out.println(id + "\t\t" + name + "\t" + age + "\t\t" + birthday);
    }
}
5、删除学生信息的代码思路:
1、键盘录入想要删除的学生信息 2、调用方法集合索引返回值,判断删除的学号是否存在3、进行学生信息删除
public static void deleteStudent(ArrayList<Student>list){
    System.out.println("=========删除学生系统=========");
    Scanner sc = new Scanner(System.in);
    System.out.println("请输入学生的学号:");
    String id = sc.next();
    int index = findStudent(list, id);
    if (index==-1){
        System.out.println("该学号还未录入,请添加后再来操作!");
        return;
    }list.remove(index);
    System.out.println("删除成功!");
}
6、修改学生信息的代码思路:
1、键盘录入想要修改的学生信息2、调用方法集合索引返回值,判断该学号是否存在3,进行学生信息修改
public static void updateStudent(ArrayList<Student> list) {
        System.out.println("==========修改学生信息===========");
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入学生的学号:");
        String id = sc.next();
        int index = findStudent(list, id);
        if (index == -1) {
            System.out.println("该学号还未录入,请添加后再来操作!");
            return;
        }
        System.out.println("请输入学生的姓名:");
        String name = sc.next();
        System.out.println("请输入学生的年龄:");
        int age = sc.nextInt();
        System.out.println("请输入学生的生日:");
        String birthday = sc.next();
        Student stu = new Student(id, name, age, birthday);
        list.set(index, stu);
        System.out.println("修改成功!");
    }
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值