简单的学生管理系统示例

package com.jsu.man;
//今天来一个简单的学生管理系统
//下面是一个简单的javabeen
public class Student {
  private String id;
  private String name;
  private int age;
  private String address;

    public Student() {
    }

    public Student(String id, String name, int 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 int getAge() {
        return age;
    }

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

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}
package com.jsu.man;

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

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

        ArrayList<Student>list=new ArrayList<>();
        //创建一个动态数组
        XunHuan:while (true) {
            System.out.println("---------------------欢迎来到学生管理系统------------------");
            System.out.println("1:添加学生");
            System.out.println("2:删除学生");
            System.out.println("3:修改学生");
            System.out.println("4:查询学生");
            System.out.println("请输入你的选择:");
            Scanner scan = new Scanner(System.in);
            String choose = scan.next();
            switch(choose){
                case "1"-> addStudent(list);
                case "2"-> deleteStudent(list);
                case "3"-> updateStudent(list);
                case "4"-> queryStudent(list);
                case "5"-> {
                    System.out.println("退出");
                    break XunHuan;
                }
                default -> System.out.println("没有这个选项");
            }
            //用switch方法可以循环输入
        }
    }

    //添加学生
    public static void addStudent(ArrayList<Student>list){
        Student s=new Student();
        Scanner scan = new Scanner(System.in);

           String id;
        while (true) {
            System.out.println("请输入学生的id");
            id = scan.next();
            s.setId(id);
            boolean flag= contains(list,id);
            if(flag) {
                System.out.println("id已经存在,请重新录入");
            }

            else{
           s.setId(id);
           break;
            }
        }

        System.out.println("请输入学生的姓名");
        String name=scan.next();
        s.setName(name);
        System.out.println("请输入学生的年龄");
        int age = scan.nextInt();
        s.setAge(age);
        System.out.println("请输入学生的家庭住址");
        String address = scan.next();
        s.setAddress(address);

       list.add(s);

        System.out.println("学生信息添加成功");
    }
//删除学生
    public static void deleteStudent(ArrayList<Student>list){

        Scanner scan = new Scanner(System.in);
        System.out.println("请输入要删除的id");
        String id= scan.next();
        int index=getIndex(list,id);
        if(index>=0){
            list.remove(index);
            System.out.println("id为:"+id+"的学生删除成功");
        }
        else{
            System.out.println("id不存在,删除失败");
        }
    }
//修改学生
    public static void updateStudent(ArrayList<Student>list){

        Scanner scan = new Scanner(System.in);
        System.out.println("请输入要修改学生的id");
        String id=scan.next();
        int index=getIndex(list,id);
         if(index==-1){
             System.out.println("要修改的id"+id+"不存在,请重新输入");
             return;
         }
         Student stu=list.get(index);
        System.out.println("请输入要修改的学生姓名");
        String newName= scan.next();
        stu.setName(newName);
        System.out.println("要修改的学生年龄");
        int newAge= scan.nextInt();
        stu.setAge(newAge);
        System.out.println("要修改的学生的学生地址");
        String newAddress= scan.next();
        stu.setAddress(newAddress);
        System.out.println("学生信息修改成功");
    }
//查询学生
    public static void queryStudent(ArrayList<Student>list){
        System.out.println("查询学生");
        if(list.size()==0){
            System.out.println("当前无学生信息,请添加后再查询");
            return;
        }
        System.out.println("id\t\t姓名\t年龄\t家庭住址");
        for (int i = 0; i < list.size(); i++) {
            Student stu=list.get(i);
            System.out.println(stu.getId()+"\t"+stu.getName()+"\t"+stu.getAge()+"\t"+stu.getAddress());
        }
    }
   public static boolean contains(ArrayList<Student>list,String id){
       for (int i = 0; i < list.size(); i++) {
           Student stu=list.get(i);
           String sid=stu.getId();
           if(sid.equals(id)){
               return true;
           }
       }
       return false;
    }
    public static int getIndex(ArrayList<Student>list,String id){
        for (int i = 0; i < list.size(); i++) {
            Student stu=list.get(i);
                String sid=stu.getId();
            if(sid.equals(id)){
                return i;
            }
        }
        return -1;
    }

    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值