美文网首页
java037IO学生管理系统正确

java037IO学生管理系统正确

作者: 牛倩贱 | 来源:发表于2018-11-27 22:29 被阅读0次

    package secondRountine;

    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 StudentSystem2 {

    public static void main(String[] args) throws IOException {

    //ArrayList<Student> array = new ArrayList<Student>();

    String fileName = "student.txt";

    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  choice = sc.nextLine();

    //选择自己的要输入的选项

    switch (choice){

    case "1":

    //查看所有学生信息

    //findAllStudent(array);

    findAllStudent(fileName);

    break;

    case "2":

    //添加学生

    addStudent(fileName);

    break;

    case "3":

    //删除学生

    deleteStudent(fileName);

    break;

    case "4":

    //修改学生

    selectStudent(fileName);

    break;

    case "5":

    //退出

    System.out.println("谢谢你的使用");

    System.exit(0);

    break;

    default:

    System.out.println("谢谢你的使用");

    System.exit(0);    //jvm虚拟机退出

    break;

    }

    }

    }

    //写方法写入集合数据到文本文件

    public static void write(String fileName,ArrayList<Student> array) throws IOException{

    //创建输出缓冲流对象

    BufferedWriter bw = new BufferedWriter(new FileWriter(fileName));

    for(int x=0;x<array.size();x++){

    Student s =array.get(x);

    StringBuilder sb = new StringBuilder();

    sb.append(s.getId()).append(",").append(s.getName()).append(",").append(s.getAge()).append(",").append(s.getHome());

    bw.write(sb.toString());

    bw.newLine();

    bw.flush();

    }

    bw.close();

    }

    //写方法读取文本文件

    public static void read(String fileName,ArrayList<Student> array) throws IOException{

    //创建输入缓冲流对象

    BufferedReader br = new BufferedReader(new FileReader(fileName));

    String line;

    while((line=br.readLine())!=null){

    String[] str = line.split(",");//把文本文件中的每行元素按逗号分割

    Student s = new Student();

    s.setId(str[0]);

    s.setName(str[1]);

    s.setAge(str[2]);

    s.setHome(str[3]);

    array.add(s);

    }

    br.close();

    }

    //查看所有学生信息,先得判断集合里有没有学生,然后遍历集合输出学生数据

    public static void findAllStudent(String fileName) throws IOException{

    ArrayList<Student> array = new ArrayList<Student>();

    //把文本中的数据读取到集合内

    read(fileName, array);

    if(array.size()==0){

    System.out.println("目前还没有可供查询的学生信息,请返回重新选择");

    return;

    }

    //为了方便看到对应信息,加补一行提示语句

    System.out.println("学号 姓名 年龄 居住地");

    //查看学生就是遍历整个学生集合,遍历集合

    for(int x =0;x<array.size();x++){

    //创建学生对象用于接收数据

    Student s = array.get(x);

    System.out.println(s.getId()+" "+s.getName()+" "+s.getAge()+" "+s.getHome());

    }

    }

    //添加学生

    public static void addStudent(String fileName) throws IOException{

    ArrayList<Student> array = new ArrayList<Student>();

    //System.out.println(array.size());

    //把文本中的数据读取到集合内

    read(fileName, array);

    //键盘录入学生信息,1.学号

    //判断学号是否有重复的

    Scanner sc = new Scanner(System.in);

    String id;

    boolean flag=false;

    while(true){

    System.out.println("请输入一个学号");

    id=sc.nextLine();

    //遍历集合

    for(int x=0;x<array.size();x++){

    Student s = array.get(x);

    if(s.getId().equals(id)){  //将现在键盘录入的学号与集合里的学号进行比对看是否相同

    flag=true;    // 如果相同了那么就将true赋给flag

    break;

    }

    }

    if(flag){

    System.out.println("你输入的学号已经被占用,请重新输入");

    }else{

    break;

    }

    }

    System.out.println("请输入学生姓名:");

    String name = sc.nextLine();

    System.out.println("请输入学生年龄:");

    String age = sc.nextLine();

    System.out.println("请输入学生住址");

    String home = sc.nextLine();

    //存到集合

    Student s  =new Student();

    s.setId(id);

    s.setName(name);

    s.setAge(age);

    s.setHome(home);

    array.add(s);

    //把集合中的数据写到文本文件

    write(fileName, array);

    System.out.println("添加学生成功!");

    }

    //删除学生

    public static void deleteStudent(String fileName) throws IOException{

    ArrayList<Student> array = new ArrayList<Student>();

    //把文本中的数据读取到集合内

    read(fileName, array);

    Scanner sc = new Scanner(System.in);

    System.out.println("请输入你要删除的学生的学号");

    String id =sc.nextLine();

    int a=-1;

    for(int x=0;x<array.size();x++){

    Student s =array.get(x);

    if(s.getId().equals(id)){

    a=x;

    //break;

    }

    break;

    }

    if(a==-1){

    System.out.println("你输入的学号,系统里不存在,请重新输入");

    }else{

    array.remove(a);

    System.out.println("删除成功");

    }

    write(fileName, array);

    }

    //修改学生

    public static void selectStudent(String fileName) throws IOException{

    ArrayList<Student> array = new ArrayList<Student>();

    //把文本中的数据读取到集合内

    read(fileName, array);

    Scanner sc = new Scanner(System.in);

    System.out.println("请输入要修改学生的学号");

    String id = sc.nextLine();

    int a=-1;

    for(int x=0;x<array.size();x++){

    Student s = array.get(x);

    if(s.getId().equals(id)){

    a=x;

    break;

    }

    }

    if(a==-1){

    System.out.println("你输入的学号有误不存在,请重新输入");

    }else{

    System.out.println("请输入学生姓名:");

    String name = sc.nextLine();

    System.out.println("请输入学生年龄:");

    String age = sc.nextLine();

    System.out.println("请输入学生住址");

    String home = sc.nextLine();

    //存到集合

    Student s  =new Student();

    s.setId(id);

    s.setName(name);

    s.setAge(age);

    s.setHome(home);

    array.add(s);

    }

    write(fileName, array);

    }

    }

    相关文章

      网友评论

          本文标题:java037IO学生管理系统正确

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