学生信息管理系统

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

public class StudentSystem {

    public static void main(String[] args) {
        ArrayList<student> list = new ArrayList<>();
        loop:
        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.退出");

            Scanner sc = new Scanner(System.in);
            System.out.println("请重新输入你的选择:");
            String choose = sc.next();

            switch (choose) {
                case "1": {
                    addStudent(list);
                    System.out.println("添加成功");
                    break;
                }
                case "2": {
                    deleteStudent(list);
                    break;
                }
                case "3": {
                    researchStudent(list);
                    break;
                }
                case "4": {
                    updateStudent(list);
                    break;
                }
                case "5": {
                    System.out.println("退出");
                    break loop;
                }
                default: {
                    System.out.println("输入有误,请重新输入:");
                    break;
                }


            }
        }

    }

    //    添加学上
    public static void addStudent(ArrayList<student> list) {
//        利用空参构造先加上学生对象
        Scanner sc = new Scanner(System.in);
        System.out.println("添加学生:");
        student s = new student();
        int id = 0;
        while (true) {
            System.out.println("请输入学生的id:");
            id = sc.nextInt();
            boolean falg = coantains(list, id);
            if (falg) {
                System.out.println("该id已存在,请重兴输入:");
            } else {
                s.setId(id);
                break;
            }
        }


        System.out.println("请输入学生姓名:");
        String name = sc.next();
        s.setName(name);
        System.out.println("请输入学生的年龄:");
        int age = sc.nextInt();
        s.setAge(age);
        System.out.println("请输入学生的家庭地址");
        String adress = sc.next();
        s.setAdress(adress);

//        把该学生对象添加到集合

        list.add(s);

    }


    //    删除学生
    public static void deleteStudent(ArrayList<student> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("删除学生:");
        int id = 0;
        System.out.println("请输入学生的id:");
        id = sc.nextInt();
        int index = getIndex(list, id);
        if (index >= 0) {
            student stu = list.get(index);
            list.remove(index);
            System.out.println("删除的是id为" + stu.id + "\t" + "的学生,删除成功");
        } else {
            System.out.println("该id不存在,删除失败");

        }

    }


    //    修改学生
    public static void updateStudent(ArrayList<student> list) {
        System.out.println("请输入修改学生的id:");
        Scanner sc = new Scanner(System.in);
        int id = sc.nextInt();
        int index = getIndex(list, id);
        if (index<0) {
           System.out.println("该学生不存在");
           return;
        } else {
            student stu = list.get(index);
            System.out.println("请输入需要修改的学生名字:");
            String newName = sc.next();
            stu.setName(newName);
            System.out.println("请输入需要修改的学生年龄");
            int newage = sc.nextInt();
            stu.setAge(newage);
            System.out.println("请输入许需要修改学生家庭地址:");
            String newadress = sc.next();
            stu.setAdress(newadress);
            System.out.println("修改成功");




        }
    }


    //    查询学生
    public static void researchStudent(ArrayList<student> list) {
        System.out.println("查询学生:");
        System.out.println("id" + "\t\t" + "姓名" + "\t" + "年龄" + "\t" + "家庭住址");
        if (list.size() == 0) {
            System.out.println("当前无学生信息");
//        结束方法
            return;
        } else {
            for (int i = 0; i < list.size(); i++) {
                System.out.println(list.get(i).getId() + "\t" + list.get(i).getName() + "\t" + list.get(i).getAge() + "\t" + list.get(i).getAdress());
            }
        }
    }

    //    比较id并若存在返回true,否则返回false
    public static boolean coantains(ArrayList<student> list, int id) {
        //循环遍历集合得到里面的每一个学生对象
        // 拿到学生对象后,获取id并进行判断不存在false
        // 存在,true
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getId() == id) {
                return true;
            }
        }
        return false;
    }

    //    比较id并返回索引
    public static int getIndex(ArrayList<student> list, int id) {
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getId() == id) {
                return i;
            }
        }
        return -1;
    }


}

类方法

public class student {
   // id name age adress
    int id;
    String name;
    int age;
    String adress;


    public student() {
    }

    public student(int id, String name, int age, String adress) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.adress = adress;
    }

    /**
     * 获取
     * @return id
     */
    public int getId() {
        return id;
    }

    /**
     * 设置
     * @param id
     */
    public void setId(int id) {
        this.id = id;
    }

    /**
     * 获取
     * @return name
     */
    public String getName() {
        return name;
    }

    /**
     * 设置
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * 获取
     * @return age
     */
    public int getAge() {
        return age;
    }

    /**
     * 设置
     * @param age
     */
    public void setAge(int age) {
        this.age = age;
    }

    /**
     * 获取
     * @return adress
     */
    public String getAdress() {
        return adress;
    }

    /**
     * 设置
     * @param adress
     */
    public void setAdress(String adress) {
        this.adress = adress;
    }

    public String toString() {
        return "student{id = " + id + ", name = " + name + ", age = " + age + ", adress = " + adress + "}";
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值