新建一个学生实体类
package Test11_Demo.Demo04;/*
@create 2020--11--26--9:31
*/
/**
* 学生的实体类
*/
public class Student {
//属性
private String id;
private String name;
private String age;
private String address;
public Student(){
}
public Student(String id, String name , String age, String address){
this.id = id;
this.name = name;
this.age = age;
this.address = address;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Student{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", age='" + age + '\'' +
", address='" + address + '\'' +
'}';
}
}
实现在文档中增删改查
package Test11_Demo.Demo04;/*
@create 2020--11--26--9:28
*/
import Test10_Demo.StudentManager06.Student;
import com.sun.org.apache.xerces.internal.impl.xs.SchemaNamespaceSupport;
import javax.annotation.processing.Filer;
import java.io.*;
import java.sql.ClientInfoStatus;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
/**
* 这是一个学生管理系统
*
* 分析步骤:
* 1.定义一个学生类
* 2.学生管理系统主界面的编写
* 3.学生管理系统的查看所有学生的信息
* 4.学生管理系统的添加学生信息
* 5.学生管理系统的删除学生信息
* 6.学生管理系统的修改学生信息
*/
public class StudentManagerTest {
public static void main(String[] args) throws IOException {
//定义一个学生类型的集合
ArrayList<Student> array = new ArrayList<>();
//写出到文件
//为了用户体验度,设计一个界面
//利用循环的方法可以重复的展示界面,直到推出程序就结束循环
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.退出学生管理系统");
//键盘录入获取
Scanner sc = new Scanner(System.in);
String choiceString = sc.nextLine();
//用开关键 - swtich...case
switch (choiceString) {
case "1":
//查看所有学生
findAllStudent(array);
break;
case "2":
//添加学生信息
addStudent();
break;
case "3":
deleteStudent(array);
//删除学生信息
break;
case "4":
updateStudent(array);
//修改学生信息
break;
default:
System.out.println("谢谢你的使用");
System.exit(0); //jvm中提供的退出程序
break;
}
}
}
//封装一个方法用于查询学生的所有信息
public static void findAllStudent(ArrayList<Student> array) throws IOException{
//首先判断集合中有没有数据,如果是空的,那就要给出提示,并且该方法就不需要执行了 - 非空判断
BufferedReader br = new BufferedReader(new FileReader("Student.txt"));
String s;
boolean flag = true;
while((s=br.readLine())!=null){
String[] strings = s.split(",");
flag = false;
System.out.println(Arrays.toString(strings));
}
if(flag){
System.out.println("暂无学生");
}
br.close();
}
//封装一个方法用于添加学生信息
public static void addStudent()throws IOException {
//键盘对象
Scanner sc = new Scanner(System.in);
System.out.println("请输入ID");
String id = sc.next();
BufferedReader br = new BufferedReader(new FileReader("Student.txt"));
String s;
while((s=br.readLine())!=null){
String[] str = s.split(",");
if (id.equals(str[0])) {
System.out.println("您输入的学号已经存在");
break;
}
}
//通过上面的验证之后就可以添加学生了
System.out.println("请输入学生姓名");
String name = sc.next();
System.out.println("请输入学生年龄");
String age = sc.next();
System.out.println("请输入学生居住地");
String address = sc.next();
//写出到文件
BufferedWriter bw = new BufferedWriter(new FileWriter("Student.txt",true));
bw.write(id + "," + name + "," +age +"," + address);
bw.newLine();
bw.close();
System.out.println("添加学生成功!");
}
//封装以个方法用于删除学生
public static void deleteStudent(ArrayList<Student> array) throws IOException {
//思路:键盘录入一个id(唯一),到集合中查找,看看是否有学生,如果有,则删除
//键盘录入
ArrayList<String> arr = new ArrayList<>();
Scanner sc = new Scanner(System.in);
System.out.println("请输入你要删除的学号");
String id = sc.next();
String s;
BufferedReader bufferedReader = new BufferedReader(new FileReader("Student.txt"));
while((s = bufferedReader.readLine())!= null){
String[] strings = s.split(",");
if (id.equals(strings[0])) {
System.out.println("找到id为"+id+"的学生");
}
}
for (String s1 : arr) {
if (s1.startsWith(id)) {
arr.remove(s1);
}
}
BufferedWriter bw = new BufferedWriter(new FileWriter("Student.txt"));
for (String s1 : arr) {
bw.write(s1);
bw.newLine();
bw.flush();
}
bufferedReader.close();
bw.close();
System.out.println("删除学生成功");
}
//封装一个方法用于修改学生
public static void updateStudent(ArrayList<Student> array) throws IOException {
//思路:键盘录入一个id(唯一),到集合中查找,看看是否有学生,如果有,则删除
//键盘录入
Scanner sc = new Scanner(System.in);
System.out.println("请输入你要删除的学生的学号:");
String id = sc.nextLine();
ArrayList<String> arr =new ArrayList<>();
String s;
BufferedReader br = new BufferedReader(new FileReader("Student.txt"));
while((s = br.readLine())!=null) {
arr.add(s);
String[] strings = s.split(",");
if (id.equals(strings[0])) {
System.out.println("找到id为" + id + "的学生");
}
}
System.out.println(arr);
br.close();
//修改的逻辑
System.out.println("请输入学生的新的姓名:");
String name = sc.next();
System.out.println("请输入学生的新的年龄:");
String age = sc.next();
System.out.println("请输入学生的新的居住地:");
String address = sc.next();
for (int i = 0; i < arr.size(); i++) {
if (arr.get(i).startsWith(id)) {
arr.set(i, id + "," + name + "," + age + "," + address + ",");
break;
}
}
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("Student.txt"));
for (String s1 : arr) {
System.out.println(s1);
bufferedWriter.write(s1);
bufferedWriter.newLine();
bufferedWriter.flush();
}
bufferedWriter.close();
//给出一个提示
System.out.println("修改学生成功!");
}
}