首頁 > Java > java教程 > java如何實現學生宿舍系統

java如何實現學生宿舍系統

WBOY
發布: 2023-05-11 12:04:05
轉載
735 人瀏覽過

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!=&#39;Y&#39;&&ch!=&#39;y&#39;&&ch!=&#39;N&#39;&&ch!=&#39;n&#39;)
                {
                    System.out.println("输入有误!请重新输入:");
                    cho=input.next();
                    ch=cho.charAt(0);
                }
                while(ch==&#39;Y&#39;||ch==&#39;y&#39;){
                    Find();
                }
                while(ch==&#39;N&#39;||ch==&#39;n&#39;){
                    Menu();
                }

            }
        }
        System.out.println("没有找到该学生,是继续输入,否返回菜单,是Y否N");
        String cho=input.next();
        char ch=cho.charAt(0);
        while(ch!=&#39;Y&#39;&&ch!=&#39;y&#39;&&ch!=&#39;N&#39;&&ch!=&#39;n&#39;)
        {
            System.out.println("输入有误!请重新输入:");
            cho=input.next();
            ch=cho.charAt(0);
        }
        while(ch==&#39;Y&#39;||ch==&#39;y&#39;){
            Find();
        }
        while(ch==&#39;N&#39;||ch==&#39;n&#39;){
            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!=&#39;Y&#39;&&ch!=&#39;y&#39;&&ch!=&#39;N&#39;&&ch!=&#39;n&#39;)
            {
                System.out.println("输入有误!请重新输入:");
                cho=input.next();
                ch=cho.charAt(0);
            }

            while(ch==&#39;Y&#39;||ch==&#39;y&#39;){
                break;
            }
            while(ch==&#39;N&#39;||ch==&#39;n&#39;){
                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!=&#39;Y&#39;&&ch!=&#39;y&#39;&&ch!=&#39;N&#39;&&ch!=&#39;n&#39;)
        {
            System.out.println("输入有误!请重新输入:");
            cho=input.next();
            ch=cho.charAt(0);
        }
        while(ch==&#39;Y&#39;||ch==&#39;y&#39;){
            Add();
        }
        while(ch==&#39;N&#39;||ch==&#39;n&#39;){
            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!=&#39;N&#39;&&ch2!=&#39;n&#39;&&ch2!=&#39;Y&#39;&&ch2!=&#39;y&#39;)
            {
                System.out.println("输入无效,请重新输入:");
                cho1=input.next();
                ch2=cho1.charAt(0);
            }
            if (ch2==&#39;y&#39;||ch2==&#39;Y&#39;){
                Renew();
            }
            if (ch2==&#39;N&#39;||ch2==&#39;n&#39;){
                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!=&#39;N&#39;&&ch2!=&#39;n&#39;&&ch2!=&#39;Y&#39;&&ch2!=&#39;y&#39;)
                {
                    System.out.println("输入无效,请重新输入:");
                    cho1=input.next();
                    ch2=cho1.charAt(0);
                }
                if (ch2==&#39;y&#39;||ch2==&#39;Y&#39;){
                    Renew();
                }
                if (ch2==&#39;N&#39;||ch2==&#39;n&#39;){
                    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!=&#39;N&#39;&&ch2!=&#39;n&#39;&&ch2!=&#39;Y&#39;&&ch2!=&#39;y&#39;)
                {
                    System.out.println("输入无效,请重新输入:");
                    cho1=input.next();
                    ch2=cho1.charAt(0);
                }
                if (ch2==&#39;y&#39;||ch2==&#39;Y&#39;){
                    Renew();
                }
                if (ch2==&#39;N&#39;||ch2==&#39;n&#39;){
                    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!=&#39;N&#39;&&ch2!=&#39;n&#39;&&ch2!=&#39;Y&#39;&&ch2!=&#39;y&#39;)
                {
                    System.out.println("输入无效,请重新输入:");
                    cho1=input.next();
                    ch2=cho1.charAt(0);
                }
                if (ch2==&#39;y&#39;||ch2==&#39;Y&#39;){
                    Renew();
                }
                if (ch2==&#39;N&#39;||ch2==&#39;n&#39;){
                    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!=&#39;N&#39;&&ch2!=&#39;n&#39;&&ch2!=&#39;Y&#39;&&ch2!=&#39;y&#39;)
            {
                System.out.println("输入无效,请重新输入:");
                cho1=input2.next();
                ch2=cho1.charAt(0);
            }
            if (ch2==&#39;y&#39;||ch2==&#39;Y&#39;){
                Delete();
            }
            if (ch2==&#39;N&#39;||ch2==&#39;n&#39;){
                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!=&#39;N&#39;&&ch3!=&#39;n&#39;&&ch3!=&#39;Y&#39;&&ch3!=&#39;y&#39;)
        {
            System.out.println("输入无效,请重新输入:");
            cho2=input2.next();
            ch3=cho2.charAt(0);
        }
        if (ch3==&#39;y&#39;||ch3==&#39;Y&#39;){
            Delete();
        }
        if (ch3==&#39;N&#39;||ch3==&#39;n&#39;){
            System.out.println("返回主菜单");
            Menu();
        } 

    }
 }
登入後複製

以上是java如何實現學生宿舍系統的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:yisu.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板