学生宿舍系统

来源:互联网 发布:2017网络暴力的新闻 编辑:程序博客网 时间:2024/06/08 19:19
  • 学生宿舍的管理系统,由于博主是小萌新,代码难免有冗余和错误的地方,如果您发现有什么不足之处或者错误,请留言,谢谢。博主会尽量回复!

功能需求分析

这里写图片描述

代码

学生类

Student.java

package Dorm;public class Student {    private int  id;    private String name;    private String sex;    private String dormid;    public int  getId() {        return id;    }    public void setId(int  id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getSex() {        return sex;    }    public void setSex(String sex) {        this.sex = sex;    }    public String getDormid() {        return dormid;    }    public void setDormid(String dormid) {        this.dormid = dormid;    }}

操作类

Main.java

package Dorm;import java.util.Scanner;public class Main {    private static int n=0;    private static Student[] stu=new Student[100];    //主函数    public static void main(String[] args) {        boolean a=false;        boolean b=false;        judge(a, b);}    //登入函数    private static void judge(boolean a, boolean b)    {        do {            Scanner input=new Scanner(System.in);            System.out.println("欢迎进入登入页面!");            System.out.println("请输入账号:");            String account=input.nextLine();            System.out.println("请输入密码:");            String code=input.nextLine();            a=account.equals("admin");            b=code.equals("admin");        } while(!(a==true&&b==true));        Menu();     }    //系统菜单页面    private static void Menu(){        Scanner input=new Scanner(System.in);        System.out.println("------  欢迎进入宿舍管理系统  ------");        System.out.println("------  请选择下列操作             ------");        System.out.println("---     1.显示所有学生信息          ---");      //Show()        System.out.println("---     2.查询学生信息                 ---");      //Find()        System.out.println("---     3.增加学生信息                 ---");      //Add()        System.out.println("---     4.修改学生信息                 ---");      //Renew()        System.out.println("---     5.删除学生信息                 ---");      //Delete()        System.out.println("---     0.退出系统                         ---");        System.out.println("请输入1~5:");        int a=input.nextInt();        while(a<0||a>5)        {            System.out.println("输入有误,请重新输入:");            a=input.nextInt();        }        switch (a) {        case 1:            Show();            break;        case 2:            Find();            break;        case 3:            Add();            break;        case 4:            Renew();            break;        case 5:            Delete();            break;        case 0:            System.out.println("成功退出系统!");            System.exit(0);            break;        }    }    //显示学生的全部信息    private static void Show(){        int i;        for(i=0;i<n;i++)        {            System.out.println("学号:"+stu[i].getId()+"\t姓名:"+stu[i].getName()+"\t性别:"+stu[i].getSex()+"\t宿舍号:"+stu[i].getDormid());        }        System.out.println("请输入任意值返回菜单页面:");        Scanner input=new Scanner(System.in);        input.next();        Menu();    }     //查询学生信息    private static void Find(){        int temp=0;        boolean flag=false;        System.out.println("请输入学生的学号:");        Scanner input=new Scanner(System.in);        long d=input.nextInt();        for(int i=0;i<n;i++)        {            while(d==stu[i].getId())            {                temp=i;                flag=true;                break;            }        }        if(flag)        {            System.out.println("查询成功,以下为该学生的信息");            System.out.println("学号:"+stu[temp].getId()+"\t姓名:"+stu[temp].getName()+"\t性别:"+stu[temp].getSex()+"\t宿舍号:"+stu[temp].getDormid());            System.out.println("是否继续查询,选择是是重新查询,否返回主菜单,是Y否N");            String cho=input.next();            char ch=cho.charAt(0);            while(ch!='Y'&&ch!='y'&&ch!='N'&&ch!='n')            {                System.out.println("输入有误!请重新输入:");                cho=input.next();                ch=cho.charAt(0);            }            while(ch=='Y'||ch=='y'){                Find();            }            while(ch=='N'||ch=='n'){                Menu();            }        }else {            System.out.println("没有找到该学生,选择是是重新查询,否返回主菜单,是Y否N");            String cho=input.next();            char ch=cho.charAt(0);            while(ch!='Y'&&ch!='y'&&ch!='N'&&ch!='n')            {                System.out.println("输入有误!请重新输入:");                cho=input.next();                ch=cho.charAt(0);            }            while(ch=='Y'||ch=='y'){                Find();            }            while(ch=='N'||ch=='n'){                Menu();            }        }/*      for(int i=0;i<n;i++){            if(d==stu[i].getId())            {                System.out.println("查询成功,以下为该学生的信息");                System.out.println("学号:"+stu[i].getId()+"\t姓名:"+stu[i].getName()+"\t性别:"+stu[i].getSex()+"\t宿舍号:"+stu[i].getDormid());                break;            }            else{                System.out.println("没有找到该学生,选择是是重新查询,否返回主菜单,是Y否N");                String cho=input.next();                char ch=cho.charAt(0);                while(ch!='Y'&&ch!='y'&&ch!='N'&&ch!='n')                {                    System.out.println("输入有误!请重新输入:");                    cho=input.next();                    ch=cho.charAt(0);                }                while(ch=='Y'||ch=='y'){                    Find();                }                while(ch=='N'||ch=='n'){                    Menu();                }            }        }*/    }       //增加一个学生    private static void Add(){        int id=0;        String dormid;        String name;        String sex;        String cho;        char ch;        stu[n]=new Student();        System.out.println("请输入学生的学号:");        Scanner input=new Scanner(System.in);        id=input.nextInt();        stu[n].setId(id);        System.out.println("请输入学生的姓名:");        name=input.next();        stu[n].setName(name);        System.out.println("请输入学生的性别:");        sex=input.next();        stu[n].setSex(sex);        System.out.println("请输入学生的宿舍号:");        dormid=input.next();        stu[n].setDormid(dormid);        n++;        System.out.println("是否继续添加学生?否返回主菜单,是Y否N");        cho=input.next();        ch=cho.charAt(0);        while(ch!='Y'&&ch!='y'&&ch!='N'&&ch!='n')        {            System.out.println("输入有误!请重新输入:");            cho=input.next();            ch=cho.charAt(0);        }        while(ch=='Y'||ch=='y'){            Add();        }        while(ch=='N'||ch=='n'){            Menu();        }    }    //修改学生信息    private static void Renew(){        int temp=0;        boolean flag=false;        System.out.println("请输入要修改学生的学号:");        Scanner input=new Scanner(System.in);        int d=input.nextInt();        for(int i=0;i<n;i++)        {            while(d==stu[i].getId())            {                temp=i;                flag=true;                break;            }        }        if(!flag)        {            System.out.println("输入的学号有误,未找到该学生,再次进入修改,请重新输入:");            Renew();        }        else        {            System.out.println("您要修改的学生的信息如下:");            System.out.println("学号:"+stu[temp].getId()+"\t姓名:"+stu[temp].getName()+"\t性别:"+stu[temp].getSex()+"\t宿舍号:"+stu[temp].getDormid());            System.out.println("请以下选择要修改的内容:");            System.out.println("------   1.姓名       ------");            System.out.println("------   2.性别       ------");            System.out.println("------   3.宿舍号      ------");            Scanner input1=new Scanner(System.in);            int a=input1.nextInt();            if(a==1)            {                System.out.println("请输入新的姓名:");                String name=input1.next();                stu[temp].setName(name);                System.out.println("修改成功!");                System.out.println("还要继续修改吗?是继续修改,否返回主菜单,是Y否N");                String cho1=input1.next();                char ch1=cho1.charAt(0);                while (ch1!='N'&&ch1!='n'&&ch1!='Y'&&ch1!='y')                {                    System.out.println("输入无效,请重新输入:");                    cho1=input.next();                    ch1=cho1.charAt(0);                }                if (ch1=='y'||ch1=='Y'){                    Renew();                }                if (ch1=='N'||ch1=='n'){                    System.out.println("返回主菜单");                    Menu();                }             }            else if(a==2)            {                System.out.println("请输入新的性别:");                String sex=input1.next();                stu[temp].setSex(sex);                System.out.println("修改成功!");                System.out.println("还要继续修改吗?是继续修改,否返回主菜单,是Y否N");                String cho1=input1.next();                char ch1=cho1.charAt(0);                while (ch1!='N'&&ch1!='n'&&ch1!='Y'&&ch1!='y')                {                    System.out.println("输入无效,请重新输入:");                    cho1=input.next();                    ch1=cho1.charAt(0);                }                if (ch1=='y'||ch1=='Y'){                    Renew();                }                if (ch1=='N'||ch1=='n'){                    System.out.println("返回主菜单");                    Menu();                }             }            else if(a==3)            {                System.out.println("请输入新的宿舍号:");                String  dormid=input1.next();                stu[temp].setDormid(dormid);                System.out.println("修改成功!");                System.out.println("还要继续修改吗?是继续修改,否返回主菜单,是Y否N");                String cho1=input1.next();                char ch1=cho1.charAt(0);                while (ch1!='N'&&ch1!='n'&&ch1!='Y'&&ch1!='y')                {                    System.out.println("输入无效,请重新输入:");                    cho1=input.next();                    ch1=cho1.charAt(0);                }                if (ch1=='y'||ch1=='Y'){                    Renew();                }                if (ch1=='N'||ch1=='n'){                    System.out.println("返回主菜单");                    Menu();                }             }            else {                System.out.println("输入有误,请重新输入:");                Renew();            }        }    }    //删除学生信息    private static void Delete(){        int temp=0;        boolean flag=true;        System.out.println("请输入你想要删除该学生的学号:");        Scanner input2=new Scanner(System.in);        int d=input2.nextInt();        for(int i=0;i<n;i++)        {            while(d==stu[i].getId())            {                temp=i;                flag=true;                break;            }        }        if(!flag)        {            System.out.println("输入的学号有误,未找到该学生,再次进入删除,请重新输入:");            Delete();        }        else{            System.out.println("您要修改的学生的信息如下:");            System.out.println("学号:"+stu[temp].getId()+"\t姓名:"+stu[temp].getName()+"\t性别:"+stu[temp].getSex()+"\t宿舍号:"+stu[temp].getDormid());            for (int i=temp;i<n-1;i++)            {                stu[i]=stu[i+1];            }            n--;            System.out.println("删除该学生信息成功!");            System.out.println("---------------------");        }        System.out.println("还要继续修改吗?是继续修改,否返回主菜单,是Y否N");        String cho2=input2.next();        char ch2=cho2.charAt(0);        while (ch2!='N'&&ch2!='n'&&ch2!='Y'&&ch2!='y')        {            System.out.println("输入无效,请重新输入:");            cho2=input2.next();            ch2=cho2.charAt(0);        }        if (ch2=='y'||ch2=='Y'){            Delete();        }        if (ch2=='N'||ch2=='n'){            System.out.println("返回主菜单");            Menu();        }     }}
原创粉丝点击