欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java实现简单学生信息管理系统

程序员文章站 2022-03-23 23:23:51
最近在学习java,所以写了个学生信息管理系统,话不多说,上代码。student.java:package com.mumu;public class student { //定义学生类 pr...

最近在学习java,所以写了个学生信息管理系统,话不多说,上代码。

student.java:

package com.mumu;

public class student {  //定义学生类
    private string name;
    private string age;
    private string id;
    private string room_num;
    private int math;
    private int english;
    private int physic;

    public student() {//无参构造方法
    }

    public student(string name, string age, string id, string room_num, int math, int english, int physic) {
        this.name = name;
        this.age = age;
        this.id = id;
        this.room_num = room_num;
        this.math = math;
        this.english = english;
        this.physic = physic;
    }

//alt+ insert键,可自动生成构造方法
    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 getid() {
        return id;
    }

    public void setid(string id) {
        this.id = id;
    }

    public string getroom_num() {
        return room_num;
    }

    public void setroom_num(string room_num) {
        this.room_num = room_num;
    }

    public int getmath() {
        return math;
    }

    public int getenglish() {
        return english;
    }

    public int getphysic() {
        return physic;
    }

    public void setmath(int math) {
        this.math = math;
    }

    public void setenglish(int english) {
        this.english = english;
    }

    public void setphysic(int physic) {
        this.physic = physic;
    }
}

studentmanager .java:

package com.mumu;

import java.util.arraylist;
import java.util.scanner;

public class studentmanager {
    public static void main(string[] args) {

    arraylist<student> array=new arraylist<>();
    menu(array);
    }
    public static void menu(arraylist<student> array)//菜单
    {
        while(true)
        {
            system.out.println("^^^^^^^^welcom to my system^^^^^^^^");
            system.out.println("please input your choice");
            system.out.println("1.add students' information");
            system.out.println("2.remove students' information");
            system.out.println("3.revise students' information");
            system.out.println("4.look over students' information");
            system.out.println("5.find  students' information");
            system.out.println("6.quit the system");
            scanner sc=new scanner(system.in);
            string choice =sc.nextline();
            switch(choice)
            {
                case "1":
                    adding(array);
                    break;
                case "2":
                    removing(array);
                    break;
                case "3":
                    revising(array);
                    break;
                case "4":
                    look_over(array);
                    break;
                case "5":
                    serching(array);
                case "6":
                    quiting();
                    break;
                default:
                    system.out.println("error!");
                    system.exit(0);
            }
        }

    }

    public static void adding(arraylist<student> array)//添加学生信息
    {//录入的学生数据录入给成员变量
        system.out.println("please input student's id");
        scanner sc=new scanner(system.in);
        string stu_num=sc.nextline();
        if(is_used(array,stu_num)==false)
        {
            system.out.println("please input student's name");
            string stu_name=sc.nextline();
            system.out.println("please input student's age");
            string stu_age=sc.nextline();
            system.out.println("please input student's room number");
            string stu_addr=sc.nextline();
            system.out.println("do you want to add student's grade?yes/no");
            //创建学生对象
            student st=new student();
            st.setage(stu_age);
            st.setid(stu_num);
            st.setname(stu_name);
            st.setroom_num(stu_addr);
            //添加学生成绩
            string cho=sc.nextline();
            if(cho=="yes")
            {
                system.out.println("please input student's math grade");
                int stu_math=sc.nextint();
                system.out.println("please input student's english grade");
                int stu_english= sc.nextint();
                system.out.println("please input student's physic grade");
                int stu_physic= sc.nextint();
                st.setmath(stu_math);
                st.setenglish(stu_english);
                st.setphysic(stu_physic);
            }
            //将学生对象添加到集合中
            array.add(st);
            system.out.println("add successfully");
        }
        else
        {
            system.out.println("you are already input information of this student");
        }

    }
    public static void removing(arraylist<student> array)//删除学生信息
    {
    scanner sc=new scanner(system.in);
        system.out.println("please input student's number");
        string stu_num=sc.nextline();
        for(int i=0;i<array.size();i++)
        {
            student st=array.get(i);
            if(st.getid().equals(stu_num))
            {
                array.remove(i);
                break;
            }
            else
            {
                system.out.println("there is no information of that student");
            }
        }
        system.out.println("remove successfully");
    }
    public static void revising(arraylist<student> array)//修改学生信息
    {
        scanner sc=new scanner(system.in);
        system.out.println("please input stubent's id");
        string stu_num=sc.nextline();
        system.out.println("please input student's new name");
        string stu_name=sc.nextline();
        system.out.println("please input student's new  id");
        string stu_id=sc.nextline();
        system.out.println("please input student's new age");
        string stu_age=sc.nextline();
        system.out.println("please input student's new room_number");
        string stu_add=sc.nextline();
        system.out.println("do you want to revise student's grade?yes/no");
        //创建学生对象
        student st1=new student();
        st1.setroom_num(stu_add);
        st1.setname(stu_name);
        st1.setid(stu_id);
        st1.setage(stu_age);
        string cho= sc.nextline();
        if(cho=="yes")
        {
            system.out.println("please input student's new math grade");
            int stu_math=sc.nextint();
            system.out.println("please input student's new english grade");
            int stu_english=sc.nextint();
            system.out.println("please input student's new physic grade");
            int stu_physic=sc.nextint();
            st1.setenglish(stu_english);
            st1.setmath(stu_math);
            st1.setphysic(stu_physic);
        }
        for(int i=0;i< array.size();i++)
        {
            student st2=array.get(i);
            if(st2.getid().equals(stu_num))//判断输入的学号是否在array里面
            {
                array.set(i,st1);
                break;
            }
            else
            {
                system.out.println("there is no information of that student");
            }
        }
        system.out.println("revise successfully");
    }
    public static void look_over(arraylist<student> array)//查看所有学生信息
    {
        if(array.size()==0)//先判断集合是否为空
        {
            system.out.println("there is no information,please input information firstly");
        }
        else
        {
            system.out.println("number\tname\tage\troom_number\tmath_grade\tenglish_grade\tphysic_grade");
            for(int i=0;i<array.size();i++)
            {
                student st=array.get(i);
                system.out.println(st.getid()+"\t"+st.getname()+"\t"+st.getage()+"\t"+st.getroom_num()+"\t"
                        +st.getmath()+"\t"+st.getenglish()+"\t"+st.getphysic());
            }

        }

    }
    public static void quiting() //退出系统
    {
        system.exit(0);
    }

    public static boolean is_used(arraylist<student> array,string sid)//判断学号是否重复
    {
        boolean temp=false;
        for(int i=0;i< array.size();i++)
        {
            student st=array.get(i);
            if(st.getid().equals(sid))
            {
                temp=true;
                break;
            }
        }
        return temp;
    }
    public static void serching(arraylist<student> array)//通过学号查找
  {
      system.out.println("please input id of the student you want to find");
      scanner sc=new scanner(system.in);
      string stu_num=sc.nextline();
      for(int i=0;i< array.size();i++)
      {
          student st= array.get(i);
          if(st.getid().equals(stu_num))
          {
              system.out.println(st.getid()+"\t"+st.getname()+"\t"+st.getage()+"\t"+st.getroom_num()+"\t"
                      +st.getmath()+"\t"+st.getenglish()+"\t"+st.getphysic());
          }
          else
          {
              system.out.println("there is no information of that student");
          }
      }
  }
}

代码是用idea写的,因为是初学者,功能相对简单,如有问题欢迎指正哦。

更多学习资料请关注专题《管理系统开发》。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。