• 学生管理系统代码


      1 package com.itheima;
      2 import java.io.BufferedReader;
      3 import java.io.BufferedWriter;
      4 import java.io.FileReader;
      5 import java.io.FileWriter;
      6 import java.io.IOException;
      7 import java.util.ArrayList;
      8 import java.util.Scanner;
      9 
     10 /*
     11  * 这是我的学生管理系统的主类
     12  * 
     13  * 步骤如下:
     14  * A:定义学生类
     15  * B:学生管理系统的主界面的代码编写
     16  * C:学生管理系统的查看所有学生的代码编写
     17  * D:学生管理系统的添加学生的代码编写
     18  * E:学生管理系统的删除学生的代码编写
     19  * F:学生管理系统的修改学生的代码编写
     20  */
     21 public class StudentManagerTest {
     22     public static void main(String[] args) throws IOException{
     23         //定义文件路径
     24         String fileName = "students.txt";
     25         
     26         //为了让程序能够回到这里来,我们使用循环
     27         while(true) {
     28             //这是学生管理系统的主界面
     29             System.out.println("--------欢迎来到学生管理系统--------");
     30             System.out.println("1 查看所有学生");
     31             System.out.println("2 添加学生");
     32             System.out.println("3 删除学生");
     33             System.out.println("4 修改学生");
     34             System.out.println("5 退出");
     35             System.out.println("请输入你的选择:");
     36             //创建键盘录入对象
     37             Scanner sc = new Scanner(System.in);
     38             String choiceString = sc.nextLine();
     39             //用switch语句实现选择
     40             switch(choiceString) {
     41             case "1":
     42                 //查看所有学生
     43                 findAllStudent(fileName);
     44                 break;
     45             case "2":
     46                 //添加学生
     47                 addStudent(fileName);
     48                 break;
     49             case "3":
     50                 //删除学生
     51                 deleteStudent(fileName);
     52                 break;
     53             case "4":
     54                 //修改学生
     55                 updateStudent(fileName);
     56                 break;
     57             case "5":
     58             default:
     59                 System.out.println("谢谢你的使用");
     60                 System.exit(0); //JVM退出
     61                 break;
     62             }
     63         }
     64     }
     65     
     66     //从文件中读数据到集合
     67     public static void readData(String fileName,ArrayList<Student> array) throws IOException {
     68         //创建输入缓冲流对象
     69         BufferedReader br = new BufferedReader(new FileReader(fileName));
     70         
     71         String line;
     72         while((line=br.readLine())!=null) {
     73             String[] datas = line.split(",");
     74             Student s = new Student();
     75             s.setId(datas[0]);
     76             s.setName(datas[1]);
     77             s.setAge(datas[2]);
     78             s.setAddress(datas[3]);
     79             array.add(s);
     80         }
     81         
     82         br.close();
     83     }
     84     
     85     //把集合中的数据写入文件
     86     public static void writeData(String fileName,ArrayList<Student> array) throws IOException {
     87         //创建输出缓冲流对象
     88         BufferedWriter bw = new BufferedWriter(new FileWriter(fileName));
     89         
     90         for(int x=0; x<array.size(); x++) {
     91             Student s = array.get(x);
     92             StringBuilder sb = new StringBuilder();
     93             sb.append(s.getId()).append(",").append(s.getName()).append(",").append(s.getAge()).append(",").append(s.getAddress());
     94             
     95             bw.write(sb.toString());
     96             bw.newLine();
     97             bw.flush();
     98         }
     99         
    100         bw.close();
    101     }
    102     
    103     //修改学生
    104     public static void updateStudent(String fileName) throws IOException {
    105         //创建集合对象
    106         ArrayList<Student> array = new ArrayList<Student>();
    107         //从文件中把数据读取到集合中
    108         readData(fileName, array);
    109         
    110         //修改学生的思路:键盘录入一个学号,到集合中去查找,看是否有学生使用的是该学号,如果有就修改该学生
    111         //创建键盘录入对象
    112         Scanner sc = new Scanner(System.in);
    113         System.out.println("请输入你要修改的学生的学号:");
    114         String id = sc.nextLine();
    115         
    116         //定义一个索引
    117         int index = -1;
    118         
    119         //遍历集合
    120         for(int x=0; x<array.size(); x++) {
    121             //获取每一个学生对象
    122             Student s = array.get(x);
    123             //拿学生对象的学号和键盘录入的学号进行比较
    124             if(s.getId().equals(id)) {
    125                 index = x;
    126                 break;
    127             }
    128         }
    129         
    130         if(index == -1) {
    131             System.out.println("不好意思,你要修改的学号对应的学生信息不存在,请回去重新你的选择");
    132         }else {
    133             System.out.println("请输入学生新姓名:");
    134             String name = sc.nextLine();
    135             System.out.println("请输入学生新年龄:");
    136             String age = sc.nextLine();
    137             System.out.println("请输入学生新居住地:");
    138             String address = sc.nextLine();
    139             
    140             //创建学生对象
    141             Student s = new Student();
    142             s.setId(id);
    143             s.setName(name);
    144             s.setAge(age);
    145             s.setAddress(address);
    146             
    147             //修改集合中的学生对象
    148             array.set(index, s);
    149             //把集合中的数据重新写回到文件
    150             writeData(fileName, array);
    151             //给出提示
    152             System.out.println("修改学生成功");
    153         }
    154     }
    155     
    156     //删除学生
    157     public static void deleteStudent(String fileName) throws IOException {
    158         //创建集合对象
    159         ArrayList<Student> array = new ArrayList<Student>();
    160         //从文件中把数据读取到集合中
    161         readData(fileName, array);
    162         
    163         //删除学生的思路:键盘录入一个学号,到集合中去查找,看是否有学生使用的是该学号,如果有就删除该学生
    164         //创建键盘录入对象
    165         Scanner sc = new Scanner(System.in);
    166         System.out.println("请输入你要删除的学生的学号:");
    167         String id = sc.nextLine();
    168         
    169         //我们必须给出学号不存在的时候的提示
    170         
    171         //定义一个索引
    172         int index = -1;
    173         
    174         //遍历集合
    175         for(int x=0; x<array.size(); x++) {
    176             //获取到每一个学生对象
    177             Student s = array.get(x);
    178             //拿这个学生对象的学号和键盘录入的学号进行比较
    179             if(s.getId().equals(id)) {
    180                 index = x;
    181                 break;
    182             }
    183         }
    184         
    185         if(index == -1) {
    186             System.out.println("不好意思,你要删除的学号对应的学生信息不存在,请回去重新你的选择");
    187         }else {
    188             array.remove(index);
    189             //把集合中的数据重新写回到文件
    190             writeData(fileName, array);
    191             System.out.println("删除学生成功");
    192         }
    193         
    194     }
    195     
    196     //添加学生
    197     public static void addStudent(String fileName) throws IOException {
    198         //创建集合对象
    199         ArrayList<Student> array = new ArrayList<Student>();
    200         //从文件中把数据读取到集合中
    201         readData(fileName, array);
    202                 
    203         //创建键盘录入对象
    204         Scanner sc = new Scanner(System.in);
    205         
    206         //为了让id能够被访问到,我们就把id定义在了循环的外面
    207         String id;
    208         
    209         //为了让代码能够回到这里,用循环
    210         while(true) {
    211             System.out.println("请输入学生学号:");
    212             //String id = sc.nextLine();
    213             id = sc.nextLine();
    214             
    215             //判断学号有没有被人占用
    216             //定义标记
    217             boolean flag = false;
    218             //遍历集合,得到每一个学生
    219             for(int x=0; x<array.size(); x++) {
    220                 Student s = array.get(x);
    221                 //获取该学生的学号,和键盘录入的学号进行比较
    222                 if(s.getId().equals(id)) {
    223                     flag = true; //说明学号被占用了
    224                     break;
    225                 }
    226             }
    227             
    228             if(flag) {
    229                 System.out.println("你输入的学号已经被占用,请重新输入");
    230             }else {
    231                 break; //结束循环
    232             }
    233         }
    234         
    235         
    236         System.out.println("请输入学生姓名:");
    237         String name = sc.nextLine();
    238         System.out.println("请输入学生年龄:");
    239         String age = sc.nextLine();
    240         System.out.println("请输入学生居住地:");
    241         String address = sc.nextLine();
    242         
    243         //创建学生对象
    244         Student s = new Student();
    245         s.setId(id);
    246         s.setName(name);
    247         s.setAge(age);
    248         s.setAddress(address);
    249         
    250         //把学生对象作为元素添加到集合
    251         array.add(s);
    252         //把集合中的数据重新写回到文件
    253         writeData(fileName, array);
    254         
    255         //给出提示
    256         System.out.println("添加学生成功");
    257     }
    258     
    259     //查看所有学生
    260     public static void findAllStudent(String fileName) throws IOException {
    261         //创建集合对象
    262         ArrayList<Student> array = new ArrayList<Student>();
    263         //从文件中把数据读取到集合中
    264         readData(fileName, array);
    265         
    266         //首先来判断集合中是否有数据,如果没有数据,就给出提示,并让该方法不继续往下执行
    267         if(array.size() == 0) {
    268             System.out.println("不好意思,目前没有学生信息可供查询,请回去重新选择你的操作");
    269             return;
    270         }
    271         
    272         //	 其实就是一个tab键的位置
    273         System.out.println("学号		姓名	年龄	居住地");
    274         for(int x=0; x<array.size(); x++) {
    275             Student s = array.get(x);
    276             System.out.println(s.getId()+"	"+s.getName()+"	"+s.getAge()+"	"+s.getAddress());
    277         }
    278     }
    279 }
  • 相关阅读:
    C#往SQLServer中插入大数据
    (5.1.2)引擎管理——启动参数与单用户模式启动
    (5.1.1)引擎管理——包含数据库解决迁移造成孤立用户问题
    【转】mysql的group_concat函数,默认最大长度是1024
    【转】【mysql面试】https://blog.csdn.net/hanfazy/article/details/14520437
    【基本优化实践】【1.2】索引优化——重建索引、查看堆表、查看索引使用情况、查看索引碎片率
    【基本优化实践】【1.1】IO优化——把文件迁移到不同物理磁盘
    sql server优化方向?
    mysql中的事务隔离级别与锁
    Stanford大学机器学习公开课(三):局部加权回归、最小二乘的概率解释、逻辑回归、感知器算法
  • 原文地址:https://www.cnblogs.com/samuraihuang/p/9759102.html
Copyright © 2020-2023  润新知