zoukankan      html  css  js  c++  java
  • java学习笔记(10-学生管理系统练习)

    1.简单学生管理系统练习

    说明:键盘录入学生信息,实现学生信息的增删改

    package com.daigua10;
    
    import java.util.Scanner;
    import java.io.BufferedWriter;
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.ArrayList;
    
    /*
     * 学生管理系统
     * */
    public class StudentManagerSystem {
        public static void main(String[] args) throws IOException {
            while(true) {
                ArrayList<Student> array = new ArrayList<Student>();
                int choice = printer();
                switch (choice) {
                case 1:
                    // 展示所有学生
                    showStudentList(array);
                    break;
                case 2:
                    // 添加学生
                    addStudent(array);
                    break;
                case 3:
                    // 删除学生
                    deleteStudent(array);
                    break;
                case 4:
                    // 修改学生
                    // 待续
                    break;
                case 5:
                    System.out.println("已退出学生管理系统!");
                    break;
                default:
                    break;
                }
                if(choice==5) {
                    break;
                }
            }
    
        }
    
        public static void showStudentList(ArrayList<Student> array) throws IOException {
            // 从文件读取所有学生信息并显示出来
            readData(array);
    
            System.out.println("学号	" + "姓名	" + "年龄	" + "住址");
    //      System.out.println(array);
            for (int i = 0; i < array.size(); i++) {
                Student temp_stu = array.get(i);
                System.out.println(temp_stu.getId() + '	' + temp_stu.getName() + '	' + temp_stu.getAge() + '	'
                        + temp_stu.getAddress());
            }
    
        }
    
        public static void addStudent(ArrayList<Student> array) throws IOException {
            readData(array);
            // 添加学生
            Scanner sc = new Scanner(System.in);
    
            System.out.println("请输入学号:");
            String id = sc.nextLine();
            System.out.println("请输入姓名:");
            String name = sc.nextLine();
            System.out.println("请输入年龄:");
            String age = sc.nextLine();
            System.out.println("请输入住址:");
            String address = sc.nextLine();
    
            Student stu = new Student(age, name, id, address);
            array.add(stu);
            // 写入文件
            writeData(array);
            System.out.println("添加成功!");
    
        }
        
        public static void deleteStudent(ArrayList<Student> arrayListStudent) throws IOException{
            // 更新下这个集合信息
            Scanner sc = new Scanner(System.in);
            Student temp_u = null;
            readData(arrayListStudent);
            System.out.println("请输入要删除的学生id:");
            String id = sc.nextLine();
    
            for (int i = 0; i < arrayListStudent.size(); i++) {
                String cur_id = arrayListStudent.get(i).getId();
                System.out.println("cur_id:"+cur_id);
                System.out.println("id:"+id);
                if(cur_id.equals(id)==true) {
                    temp_u = arrayListStudent.get(i);
                    System.out.println(temp_u);
                    break;
                }
            }
            arrayListStudent.remove(temp_u);
            writeData(arrayListStudent);
        }
    
        public static void readData(ArrayList<Student> arrayListStudent) throws IOException {
            // 读取文件信息的方法
            BufferedReader br = new BufferedReader(new FileReader("./src/com/daigua10/student.txt"));
            String line;
            while ((line = br.readLine()) != null) {
                String[] cur_stu = line.split(",");
                Student temp_stu = new Student(cur_stu[2], cur_stu[1], cur_stu[0], cur_stu[3]);
                arrayListStudent.add(temp_stu);
            }
            br.close();
        }
    
        public static void writeData(ArrayList<Student> arrayListStudent) throws IOException {
            // 写入文件的方法
            BufferedWriter bw = new BufferedWriter(new FileWriter("./src/com/daigua10/student.txt"));
            for (int i = 0; i < arrayListStudent.size(); i++) {
                Student cur_stu = arrayListStudent.get(i);
                String temp_data = cur_stu.getId() + "," + cur_stu.getName() + "," + cur_stu.getAge() + ","
                        + cur_stu.getAddress();
                bw.write(temp_data);
                bw.newLine();
                bw.flush();
            }
            bw.close();
        }
    
        // 打印器
        public static int printer() {
            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.print("请输入你的选择:");
            Scanner sc = new Scanner(System.in);
            int choice = sc.nextInt();
    //      System.out.println(choice);
            return choice;
        }
    
    }
    
  • 相关阅读:
    SQLServer之ISO游标使用
    Eclipse Android 代码自动提示功能
    Java中利用MessageFormat对象实现类似C# string.Format方法格式化
    Eclipse的常用快捷键、旁门左道、系统错误小贴士
    Eclipse配置详解(包括智能提示设置、智能提示插件修改,修改空格自动上屏、JDK配置、各种快捷键列表……)
    Unity3D DF根据名称获取多个子控件代码
    unity3d asset store下载的代码所在位置
    Unity3D Update() 和 FixedUpdate()区别
    Unity3D的断点调试功能
    Error building Player: UnityException: Bundle Identifier has not been set up correctly
  • 原文地址:https://www.cnblogs.com/daigua/p/java-xue-xi-bi-ji-10xue-sheng-guan-li-xi-tong-lian.html
Copyright © 2011-2022 走看看