package com.qfedu.b_zuoye01;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Scanner;
class Student {
private String Id;
private String name;
public Student() {
}
public String getId() {
return Id;
}
public void setId(String id) {
Id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Student{" +
"Id=" + Id +
", name='" + name + '\'' +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return Id == student.Id && Objects.equals(name, student.name);
}
@Override
public int hashCode() {
return Objects.hash(Id, name);
}
public Student(String id, String name) {
Id = id;
this.name = name;
}
}
public class Demo_02 {
public static void main(String[] args) throws IOException {
//输入流:
BufferedReader br = new BufferedReader(new FileReader(new File("D:\\aaa\\User\\student.txt")));
//输出流:
BufferedWriter bw = new BufferedWriter(new FileWriter(new File("D:\\aaa\\User\\student.txt"),true));
/**
* List数组,存学生对象
*/
List<Student> list = new ArrayList<>();
while (true){
Scanner scanner = new Scanner(System.in);
//读取文件中的学生信息,存入list学生集合中
String stu;
while ((stu = br.readLine())!=null ){
String[] split = stu.split(":");//用 : 把读取出来的信息如 01:张三 切割成一个字符串数组[01,张三]
list.add(new Student(split[0],split[1]));//创建学生对象,并赋值,存入list集合中
}
System.out.println(list);
System.out.println("-----欢迎来到学生管理系统--------");
System.out.println("菜单:1、查询所有学生 2、添加学生 3、删除学生 4、修改学生 5、查询学生 6、退出");
int a = scanner.nextInt();
switch (a){
case 1:
System.out.println("查询所有学生");
for (Student student : list) {
System.out.println("Id:"+student.getId()+" "+"姓名:"+student.getName());
}
break;
case 2:
System.out.println("添加学生");
System.out.println("输入学生id:");
String a1 = scanner.next();
System.out.println("输入学生姓名:");
String a2 = scanner.next();
bw.write(a1+":"+a2);
bw.newLine();
bw.flush();
System.out.println("添加成功!!!");
break;
case 3:
System.out.println("删除学生");
System.out.println("请输入你要删除的学生的Id或者名字:");
String name = scanner.next();
/**
* 遍历list集合,删除指定的对象
*/
for (int i = 0; i < list.size(); i++) {
Student student1 = new Student();
student1 = list.get(i);
if (list.get(i).getName().equals(name)||list.get(i).getId().equals(name)){
list.remove(student1);
}
}
System.out.println("删除成功!!");
/**
* 遍历删除后的list集合,然后把它重新存入txt文档中
*/
//输出流:(没加true,所以会覆盖之前的文档中的内容);
BufferedWriter bw2 = new BufferedWriter(new FileWriter(new File("D:\\aaa\\User\\student.txt")));
for (Student student1 : list) {
bw2.write(student1.getId()+":"+student1.getName());
bw2.newLine();
bw2.flush();
}
bw2.close();
break;
case 4:
System.out.println("修改学生");
System.out.println("请输入你要修改的学生id:");
String num = scanner.next();
int k = 0;//控制最后是输出修改成功,还是修改失败!!
for (Student student1 : list) {
if (student1.getId().equals(num)){
System.out.println("开始修改");
System.out.println("请输入你要修改的id:");
String num1 = scanner.next();
System.out.println("请输入你要修改的姓名:");
String num2 = scanner.next();
student1.setId(num1);
student1.setName(num2);
/**
* 开始遍历list集合,把它写入磁盘中
*/
//输出流:(没加true,所以会覆盖之前的文档中的内容);
BufferedWriter bw3 = new BufferedWriter(new FileWriter(new File("D:\\aaa\\User\\student.txt")));
for (Student student2 : list) {
bw3.write(student2.getId()+":"+student2.getName());
bw3.newLine();
}
bw3.close();//关闭,输出流
k++;
}
}
switch (k){
case 0:
System.out.println("该学生不存在,请重新输入!!");
break;
default:
System.out.println("修改成功!!");
break;
}
case 5:
System.out.println("查询学生");
System.out.println("请输入需要查询的学生的id或者姓名:");
String n1 = scanner.next();
for (Student student1 : list) {
if (student1.getId().equals(n1)||student1.getName().equals(n1)){
System.out.println("查询到的信息如下:");
System.out.println("Id:"+student1.getId()+" "+"姓名:"+student1.getName());
break;
}
}
System.out.println("该学生不存在!!");
break;
default:
System.out.println("退出");
bw.close();
br.close();
System.exit(0);
}
}
}
}
运行效果图:
存储学生信息的tex文档