Java使用封装实现以及ArrayList实现简单的学生管理系统!

完成界面如下:
在这里插入图片描述
首先创建学生类:

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

    public Student(){};

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

    public void setAge(String a){
        age=a;
    }

    public String getAge(){
        return age;
    }

    public void setName(String a){
        name=a;
    }

    public String getName(){
        return name;
    }
}

然后实现main

import com.sun.source.doctree.SystemPropertyTree;

import javax.swing.*;
import javax.swing.plaf.synth.SynthToolTipUI;
import java.util.ArrayList;
import java.util.Scanner;

public class my01 {
    public static void main(String[] args) {
        ArrayList<Student> arr=new ArrayList<>();
        while (true) {
            switch (showMenu()) {
                case 1:
                    addStudent(arr);
                    break;
                case 2:
                    findAllStudent(arr);
                    break;
                case 3:
                    delectStudent(arr);
                    break;
                case 4:
                    updateStudent(arr);
                    break;
                case 5:
                    System.out.println("感谢使用!");
                    System.exit(0);  //新知识:跳出java虚拟机方法。
            }
        }
    }


    public static void addStudent(ArrayList<Student> arr){
        System.out.println("请输入学生年龄和名字:");
        Scanner sr=new Scanner(System.in);
        String age=sr.nextLine();
        String name=sr.nextLine();
        Student stu=new Student(age,name);
        arr.add(stu);
        System.out.println("添加成功!");
    }

    public static void findAllStudent(ArrayList<Student> arr){
        if(arr.size()==0)
            System.out.println("违法操作!无数据!");
        else
            for(Student i:arr){
                System.out.println("姓名"+i.getName()+" "+"年龄"+i.getAge());
            }
    }

    public static void delectStudent(ArrayList<Student> arr){
        if(arr.size()==0)
            System.out.println("违法操作!无数据!");
        else{
            System.out.println("请输入要删除的学生姓名:");
            Scanner sr=new Scanner(System.in);
            String name=sr.nextLine();
            for(int i=0;i<arr.size();i++){
                if(name.equals(arr.get(i).getName())){
                    if(arr.remove(arr.get(i))) {
                        System.out.println("删除成功!");
                        i--;
                    }
                }
            }
        }
    }

    public static  void updateStudent(ArrayList<Student> arr){
        if(arr.size()==0)
            System.out.println("违法操作!无数据!");
        else{
            System.out.println("请输入要跟新的位置:");
            Scanner sr=new Scanner(System.in);
            int n=sr.nextInt();
            System.out.println("请输入学生姓名和年龄:");
            sr.nextLine();  //特别关注的一点:in.nextLine();会读入"\n"字符,但"\n"并不会成为返回的字符。
            String age=sr.nextLine();
            String name=sr.nextLine();
            Student stu=new Student(age,name);
            System.out.println("你已经成功更新学生"+arr.set(n,stu).getName()+"的数据");
        }
    }

    public static int showMenu(){
        System.out.println("------------------");
        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("------------------");
        System.out.println("请输入操作序号:");
        Scanner sr=new Scanner(System.in);
        int s=sr.nextInt();
        return s;

    }
}

后面有时间我会发一个与mysql链接存学生信息的方法。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值