文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

java如何实现学生宿舍系统

2023-06-29 12:50

关注

这篇文章主要介绍了java如何实现学生宿舍系统的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇java如何实现学生宿舍系统文章都会有所收获,下面我们一起来看看吧。

java如何实现学生宿舍系统

学生类代码

Student.java

package dormitory;public class Student {    private String  id;    private String name;    private String sex;    private String dormid;    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 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;    }}

主操作代码

IntailStudent.java

package dormitory;import java.awt.List;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.util.ArrayList;import java.util.Iterator;import java.util.Scanner;import javax.print.DocFlavor.INPUT_STREAM;import javax.swing.event.ListSelectionEvent;import org.omg.PortableInterceptor.IORInterceptor;public class InitailStudent {    private static int n=0;    private static Student[] stu=new Student[100];    //主函数    public static void main(String[] args) throws IOException {        boolean a=false;        boolean b=false;        InitailStudent student=new InitailStudent();        student.judge(a, b);    }    //登入函数    private void judge(boolean a, boolean b) throws IOException    {        do {            System.out.println("欢迎进入登入页面!");            Scanner input=new Scanner(System.in);            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 void Menu() throws IOException{        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 void Show() throws IOException{        System.out.println("您总录入的信息如下:");        System.out.println("*****************************");        BufferedReader br=new BufferedReader(new FileReader("student.txt"));        String line;        while((line=br.readLine())!=null){            System.out.println(line);        }        br.close();        System.out.println("\n\r");        System.out.println("此次录入的信息为");        System.out.println("*****************************");        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("返回主菜单");        Menu();    }     //查询学生信息    private void Find() throws IOException{        ArrayList<ArrayList<String>> lists = new ArrayList<>();        BufferedReader br=new BufferedReader(new FileReader("student.txt"));        String line;        ArrayList<String> list = new ArrayList<>();        ArrayList<String> validlist = new ArrayList<>();        while((line=br.readLine())!=null){            list.add(line.toString());        }        br.close();        for(int i = 0;i<list.size();i++)            if(i!=0&&list.get(i-1).startsWith("学号")){                validlist.add(list.get(i));        }        for (String string : validlist) {            String[] split = string.split("     ");            ArrayList<String> tempString = new ArrayList<>();            for (String string2 : split) {                tempString.add(string2);            }            lists.add(tempString);        }           System.out.println("共有"+lists.size()+"个学生信息");        String[][] stu1=new String[lists.size()][4];        for(int i=0;i<lists.size();i++)            for(int j=0;j<4;j++){                stu1[i][j]=lists.get(i).get(j);            }        System.out.println("请输入学生的学号:");        Scanner input=new Scanner(System.in);        String  d=input.next();        for(int i=0;i<stu1.length;i++)        {            if(d.equals(stu1[i][0]))            {                System.out.println("查询成功,以下为该学生的信息");                System.out.println("学号:"+stu1[i][0]+"\t姓名:"+stu1[i][1]+"\t性别:"+stu1[i][2]+"\t宿舍号:"+stu1[i][3]);                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();                }            }        }        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 void Add() throws IOException{        String  id;        String dormid;        String name;        String sex;        String cho;        char ch;        stu[n]=new Student();        Scanner input=new Scanner(System.in);        if(n==0)        {            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'){                break;            }            while(ch=='N'||ch=='n'){                Menu();            }        }        FileWriter fw=new FileWriter("student.txt",true);        fw.write("\r\n");        fw.write("学号        姓名      性别      宿舍号 \r\n");        System.out.println("请输入学生的学号:");        id=input.next();        stu[n].setId(id);        fw.write(stu[n].getId()+"       ");        System.out.println("请输入学生的姓名:");        name=input.next();        stu[n].setName(name);        fw.write(stu[n].getName()+"     ");        System.out.println("请输入学生的性别:");        sex=input.next();        stu[n].setSex(sex);        fw.write(stu[n].getSex()+"      ");        System.out.println("请输入学生的宿舍号:");        dormid=input.next();        stu[n].setDormid(dormid);        fw.write(stu[n].getDormid()+"       ");        n++;        fw.close();        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 void Renew() throws IOException{        ArrayList<ArrayList<String>> lists = new ArrayList<>();        BufferedReader br=new BufferedReader(new FileReader("student.txt"));        String line;        ArrayList<String> list = new ArrayList<>();        ArrayList<String> validlist = new ArrayList<>();        while((line=br.readLine())!=null){            list.add(line.toString());        }        br.close();        for(int i = 0;i<list.size();i++)            if(i!=0&&list.get(i-1).startsWith("学号")){                validlist.add(list.get(i));        }        for (String string : validlist) {            String[] split = string.split("     ");            ArrayList<String> tempString = new ArrayList<>();            for (String string2 : split) {                tempString.add(string2);            }            lists.add(tempString);        }           String[][] stu1=new String[lists.size()][4];        for(int i=0;i<lists.size();i++)            for(int j=0;j<4;j++){                stu1[i][j]=lists.get(i).get(j);            }        int temp=0;        boolean flag=false;        System.out.println("请输入要修改学生的学号:");        Scanner input=new Scanner(System.in);        String  d=input.next();        for(int i=0;i<stu1.length;i++)        {            while(d.equals(stu1[i][0]))            {                temp=i;                flag=true;                break;            }        }        if(!flag)        {            System.out.println("输入的学号有误,未找到该学生,是否再次进入修改,是Y,否N");            String cho1=input.next();            char ch2=cho1.charAt(0);            while (ch2!='N'&&ch2!='n'&&ch2!='Y'&&ch2!='y')            {                System.out.println("输入无效,请重新输入:");                cho1=input.next();                ch2=cho1.charAt(0);            }            if (ch2=='y'||ch2=='Y'){                Renew();            }            if (ch2=='N'||ch2=='n'){                System.out.println("返回主菜单");                Menu();            }         }        else        {            System.out.println("您要修改的学生的信息如下:");            System.out.println("学号:"+stu1[temp][0]+"\t姓名:"+stu1[temp][1]+"\t性别:"+stu1[temp][2]+"\t宿舍号:"+stu1[temp][3]);            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();                stu1[temp][1]=name;                FileWriter fw1=new FileWriter("student.txt");                fw1.write("      ");                fw1.close();                FileWriter fw=new FileWriter("student.txt",true);                fw.write("\r\n"+"           "+"学生信息表\r\n");                for(int i=0;i<stu1.length;i++)                {                    fw.write("\r\n学号        姓名      性别      宿舍号 \r\n");                    fw.write(stu1[i][0]+"       ");                    fw.write(stu1[i][1]+"       ");                    fw.write(stu1[i][2]+"       ");                    fw.write(stu1[i][3]+"       ");                }                fw.close();                System.out.println("修改成功!");                System.out.println("还要继续修改吗?是继续修改,否返回主菜单,是Y否N");                String cho1=input1.next();                char ch2=cho1.charAt(0);                while (ch2!='N'&&ch2!='n'&&ch2!='Y'&&ch2!='y')                {                    System.out.println("输入无效,请重新输入:");                    cho1=input.next();                    ch2=cho1.charAt(0);                }                if (ch2=='y'||ch2=='Y'){                    Renew();                }                if (ch2=='N'||ch2=='n'){                    System.out.println("返回主菜单");                    Menu();                }             }            else if(a==2)            {                System.out.println("请输入新的性别:");                String sex=input1.next();                stu1[temp][2]=sex;                FileWriter fw1=new FileWriter("student.txt");                fw1.write("      ");                fw1.close();                FileWriter fw=new FileWriter("student.txt",true);                fw.write("\r\n"+"           "+"学生信息表\r\n");                for(int i=0;i<stu1.length;i++)                {                    fw.write("\r\n学号        姓名      性别      宿舍号 \r\n");                    fw.write(stu1[i][0]+"       ");                    fw.write(stu1[i][1]+"       ");                    fw.write(stu1[i][2]+"       ");                    fw.write(stu1[i][3]+"       ");                }                fw.close();                System.out.println("修改成功!");                System.out.println("还要继续修改吗?是继续修改,否返回主菜单,是Y否N");                String cho1=input1.next();                char ch2=cho1.charAt(0);                while (ch2!='N'&&ch2!='n'&&ch2!='Y'&&ch2!='y')                {                    System.out.println("输入无效,请重新输入:");                    cho1=input.next();                    ch2=cho1.charAt(0);                }                if (ch2=='y'||ch2=='Y'){                    Renew();                }                if (ch2=='N'||ch2=='n'){                    System.out.println("返回主菜单");                    Menu();                }             }            else if(a==3)            {                System.out.println("请输入新的宿舍号:");                String  dormid=input1.next();                stu1[temp][3]=dormid;                FileWriter fw1=new FileWriter("student.txt");                fw1.write("      ");                fw1.close();                FileWriter fw=new FileWriter("student.txt",true);                fw.write("\r\n"+"           "+"学生信息表\r\n");                for(int i=0;i<stu1.length;i++)                {                    fw.write("\r\n学号        姓名      性别      宿舍号 \r\n");                    fw.write(stu1[i][0]+"       ");                    fw.write(stu1[i][1]+"       ");                    fw.write(stu1[i][2]+"       ");                    fw.write(stu1[i][3]+"       ");                }                fw.close();                System.out.println("修改成功!");                System.out.println("还要继续修改吗?是继续修改,否返回主菜单,是Y否N");                String cho1=input1.next();                char ch2=cho1.charAt(0);                while (ch2!='N'&&ch2!='n'&&ch2!='Y'&&ch2!='y')                {                    System.out.println("输入无效,请重新输入:");                    cho1=input.next();                    ch2=cho1.charAt(0);                }                if (ch2=='y'||ch2=='Y'){                    Renew();                }                if (ch2=='N'||ch2=='n'){                    System.out.println("返回主菜单");                    Menu();                }             }            else {                System.out.println("输入有误,请重新输入:");                Renew();            }        }    }    //删除学生信息    private void Delete() throws IOException{        ArrayList<ArrayList<String>> lists = new ArrayList<>();        BufferedReader br=new BufferedReader(new FileReader("student.txt"));        String line;        ArrayList<String> list = new ArrayList<>();        ArrayList<String> validlist = new ArrayList<>();        while((line=br.readLine())!=null){            list.add(line.toString());        }        br.close();        for(int i = 0;i<list.size();i++)            if(i!=0&&list.get(i-1).startsWith("学号")){                validlist.add(list.get(i));        }        for (String string : validlist) {            String[] split = string.split("     ");            ArrayList<String> tempString = new ArrayList<>();            for (String string2 : split) {                tempString.add(string2);            }            lists.add(tempString);        }        String[][] stu1=new String[lists.size()][4];        for(int i=0;i<lists.size();i++)            for(int j=0;j<4;j++){                stu1[i][j]=lists.get(i).get(j);            }        int temp=0;        boolean flag=true;        System.out.println("请输入你想要删除该学生的学号:");        Scanner input2=new Scanner(System.in);        String  d=input2.next();        for(int i=0;i<stu1.length;i++)        {            while(d.equals(stu1[i][0]))            {                temp=i;                flag=true;                break;            }        }        if(!flag)        {            System.out.println("输入的学号有误,未找到该学生,再次进入删除,请重新输入:");            String cho1=input2.next();            char ch2=cho1.charAt(0);            while (ch2!='N'&&ch2!='n'&&ch2!='Y'&&ch2!='y')            {                System.out.println("输入无效,请重新输入:");                cho1=input2.next();                ch2=cho1.charAt(0);            }            if (ch2=='y'||ch2=='Y'){                Delete();            }            if (ch2=='N'||ch2=='n'){                System.out.println("返回主菜单");                Menu();            }         }        else{            System.out.println("您要删除的学生的信息如下:");            System.out.println("学号:"+stu1[temp][0]+"\t姓名:"+stu1[temp][1]+"\t性别:"+stu1[temp][2]+"\t宿舍号:"+stu1[temp][3]);            for (int i=temp;i<stu1.length-1;i++)            {                stu1[i]=stu1[i+1];            }            FileWriter fw1=new FileWriter("student.txt");            fw1.write("      ");            fw1.close();            FileWriter fw=new FileWriter("student.txt",true);            fw.write("\r\n"+"           "+"学生信息表\r\n");            for(int i=0;i<stu1.length-1;i++)            {                fw.write("\r\n学号        姓名      性别      宿舍号 \r\n");                fw.write(stu1[i][0]+"       ");                fw.write(stu1[i][1]+"       ");                fw.write(stu1[i][2]+"       ");                fw.write(stu1[i][3]+"       ");            }            fw.close();               System.out.println("删除该学生信息成功!");            System.out.println("---------------------");        }        System.out.println("还要继续删除吗?是继续删除,否返回主菜单,是Y否N");        String cho2=input2.next();        char ch3=cho2.charAt(0);        while (ch3!='N'&&ch3!='n'&&ch3!='Y'&&ch3!='y')        {            System.out.println("输入无效,请重新输入:");            cho2=input2.next();            ch3=cho2.charAt(0);        }        if (ch3=='y'||ch3=='Y'){            Delete();        }        if (ch3=='N'||ch3=='n'){            System.out.println("返回主菜单");            Menu();        }     } }

关于“java如何实现学生宿舍系统”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“java如何实现学生宿舍系统”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注编程网行业资讯频道。

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-后端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯