美文网首页
学生信息管理系统Plus

学生信息管理系统Plus

作者: March_Cullen | 来源:发表于2018-11-07 23:35 被阅读0次

    StudentManagerPlus.java

    package ly.com;
    
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.Scanner;
    
    public class StudentManagerPlus {
        public static void main(String[] args) throws IOException {
            addMainPage("array.txt");
            return;
        }
        
        public static void addMainPage(String fileName) throws IOException {
            while (true) {
                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("请输入序号选择");
                Scanner sc = new Scanner(System.in);
                String inputStr = sc.nextLine();
                switch (inputStr) {
                case "1": // 1.查看所有学生信息
                    getAllStudent(fileName);
                    break;
                case "2": // 2.添加学生
                    addStudent(fileName);
                    break;
                case "3": // 3.删除学生
                    removeStudent(fileName);
                    break;
                case "4": // 4.修改学生信息
                    modifyStudent(fileName);
                    break;
                case "5": // 5.退出
                default:
                    System.out.println("谢谢使用!");
                    System.exit(0);
                    break;
                }
            }
        }
        
        public static void readDataFromFile(ArrayList<Student> array, String fileName) throws IOException {
            BufferedReader br = new BufferedReader(new FileReader(fileName));
            String line;
            while (null != (line = br.readLine())) {
                String[] strArr = line.split("\t");
                Student stu = new Student(strArr[0], strArr[1], strArr[2], strArr[3]);
                array.add(stu);
    //          System.out.println(line);
            }
            br.close();
            return;
        }
        
        public static void writeDataToFile(ArrayList<Student> array, String fileName) throws IOException {
            BufferedWriter bw = new BufferedWriter(new FileWriter(fileName));
            for (int i = 0; i < array.size(); i++) {
                Student stu = array.get(i);
                StringBuffer bs = new StringBuffer();
                bs.append(stu.getId()).append("\t");
                bs.append(stu.getName()).append("\t");
                bs.append(stu.getAge()).append("\t");
                bs.append(stu.getAddress()).append("\t");
                bw.write(bs.toString());
                bw.newLine();
                bw.flush();
            }
            bw.close();
            return;
        }
        
        public static void getAllStudent(String fileName) throws IOException {
            ArrayList<Student> array = new ArrayList<Student>();
            readDataFromFile(array, fileName);
            
            if (0 == array.size()) {
                System.out.println(fileName + "文件中没有学生信息");
                return;
            }
            
            System.out.println("学号\t姓名\t年龄\t住址");
            for (int i = 0; i < array.size(); i++) {
                Student stu = array.get(i);
                System.out.println(stu.getId() + "\t" + stu.getName() + "\t" + stu.getAge() + "\t" + stu.getAddress());
            }
            return;
        }
        
        public static void addStudent(String fileName) throws IOException {
            ArrayList<Student> array = new ArrayList<Student>();
            readDataFromFile(array, fileName);
            
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入需要添加的学生学号");
            String strId = sc.nextLine();
            
            if (0 != array.size()) {
                while (true) {
                    int i = 0;
                    for (; i < array.size(); i++) {
                        Student stu = array.get(i);
                        if (strId.equals(stu.getId())) {
                            System.out.println("该学号已被占用,请重新输入添加的学生学号");
                            strId = sc.nextLine();
                            break;
                        }
                    }
                    if (i == array.size()) {
                        break;
                    }
                }
            }
            
            System.out.println("请输入学生姓名"); String strName = sc.nextLine();
            System.out.println("请输入学生年龄"); String strAge = sc.nextLine();
            System.out.println("请输入学生住址"); String strAddress = sc.nextLine();
            //sc.close();
            Student newStu = new Student(strId, strName, strAge, strAddress);
            array.add(newStu);
            
            writeDataToFile(array, fileName);
            System.out.println("添加学生成功");
            return;
        }
        
        public static void removeStudent(String fileName) throws IOException {
            ArrayList<Student> array = new ArrayList<Student>();
            readDataFromFile(array, fileName);
            
            if (0 == array.size()) {
                System.out.println("学生信息管理系统中没有学生信息,请重新输入");
                return;
            }
            
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入要删除的学生学号");
            String strId = sc.nextLine();
            
            int i = 0;
            while (true) {
                i = 0;
                for (; i < array.size(); i++) {
                    if (true == strId.equals(array.get(i).getId())) {
                        break;
                    }
                }
                if (i == array.size()) {
                    System.out.println("未查找到该学生学号,请重新输入需要删除的学生学号");
                    strId = sc.nextLine();
                } else {
                    break;
                }
            }
            //sc.close();
            array.remove(i);
            
            writeDataToFile(array, fileName);
            System.out.println("删除学生成功");
            return;
        }
        
        public static void modifyStudent(String fileName) throws IOException {
            ArrayList<Student> array = new ArrayList<Student>();
            readDataFromFile(array, fileName);
            
            if (0 == array.size()) {
                System.out.println("学生信息系统中没有学生信息,请重新选择");
                return;
            }
            
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入需要修改的学生学号");
            String strId = sc.nextLine();
            int i = 0;
            while (true) {
                i = 0;
                for (; i < array.size(); i++) {
                    if (true == strId.equals(array.get(i).getId())) {
                        break;
                    }
                }
                if (i == array.size()) {
                    System.out.println("未查找到该学生学号,请重新输入需要修改的学生学号");
                    strId = sc.nextLine();
                } else {
                    break;
                }
            }
            
            System.out.println("请输入新学生学号"); String newId = sc.nextLine();
            System.out.println("请输入新学生姓名"); String newName = sc.nextLine();
            System.out.println("请输入新学生年龄"); String newAge = sc.nextLine();
            System.out.println("请输入新学生住址"); String newAddress = sc.nextLine();
            //sc.close();
            Student stu = new Student(newId, newName, newAge, newAddress);
            array.set(i, stu);
            
            writeDataToFile(array, fileName);
            System.out.println("修改学生成功");
            return;
        }
        
    }
    

    小记:
    昨晚为了消除Eclipse中的警告,添加了sc.close();,今晚运行的时候出问题了。实践证明,不能加这一句。

    相关文章

      网友评论

          本文标题:学生信息管理系统Plus

          本文链接:https://www.haomeiwen.com/subject/qtxixqtx.html