新建一个学生实体类

  1. package Test11_Demo.Demo04;/*
  2. @create 2020--11--26--9:31
  3. */
  4. /**
  5. * 学生的实体类
  6. */
  7. public class Student {
  8. //属性
  9. private String id;
  10. private String name;
  11. private String age;
  12. private String address;
  13. public Student(){
  14. }
  15. public Student(String id, String name , String age, String address){
  16. this.id = id;
  17. this.name = name;
  18. this.age = age;
  19. this.address = address;
  20. }
  21. public String getId() {
  22. return id;
  23. }
  24. public void setId(String id) {
  25. this.id = id;
  26. }
  27. public String getName() {
  28. return name;
  29. }
  30. public void setName(String name) {
  31. this.name = name;
  32. }
  33. public String getAge() {
  34. return age;
  35. }
  36. public void setAge(String age) {
  37. this.age = age;
  38. }
  39. public String getAddress() {
  40. return address;
  41. }
  42. public void setAddress(String address) {
  43. this.address = address;
  44. }
  45. @Override
  46. public String toString() {
  47. return "Student{" +
  48. "id='" + id + '\'' +
  49. ", name='" + name + '\'' +
  50. ", age='" + age + '\'' +
  51. ", address='" + address + '\'' +
  52. '}';
  53. }
  54. }

实现在文档中增删改查

  1. package Test11_Demo.Demo04;/*
  2. @create 2020--11--26--9:28
  3. */
  4. import Test10_Demo.StudentManager06.Student;
  5. import com.sun.org.apache.xerces.internal.impl.xs.SchemaNamespaceSupport;
  6. import javax.annotation.processing.Filer;
  7. import java.io.*;
  8. import java.sql.ClientInfoStatus;
  9. import java.util.ArrayList;
  10. import java.util.Arrays;
  11. import java.util.Scanner;
  12. /**
  13. * 这是一个学生管理系统
  14. *
  15. * 分析步骤:
  16. * 1.定义一个学生类
  17. * 2.学生管理系统主界面的编写
  18. * 3.学生管理系统的查看所有学生的信息
  19. * 4.学生管理系统的添加学生信息
  20. * 5.学生管理系统的删除学生信息
  21. * 6.学生管理系统的修改学生信息
  22. */
  23. public class StudentManagerTest {
  24. public static void main(String[] args) throws IOException {
  25. //定义一个学生类型的集合
  26. ArrayList<Student> array = new ArrayList<>();
  27. //写出到文件
  28. //为了用户体验度,设计一个界面
  29. //利用循环的方法可以重复的展示界面,直到推出程序就结束循环
  30. while (true) {
  31. //主界面的欢迎词
  32. System.out.println("-------------------------欢迎来到学生管理系统-------------------------------");
  33. System.out.println("1.查看所有学生信息");
  34. System.out.println("2.添加学生信息");
  35. System.out.println("3.删除学生信息");
  36. System.out.println("4.修改学生信息");
  37. System.out.println("5.退出学生管理系统");
  38. //键盘录入获取
  39. Scanner sc = new Scanner(System.in);
  40. String choiceString = sc.nextLine();
  41. //用开关键 - swtich...case
  42. switch (choiceString) {
  43. case "1":
  44. //查看所有学生
  45. findAllStudent(array);
  46. break;
  47. case "2":
  48. //添加学生信息
  49. addStudent();
  50. break;
  51. case "3":
  52. deleteStudent(array);
  53. //删除学生信息
  54. break;
  55. case "4":
  56. updateStudent(array);
  57. //修改学生信息
  58. break;
  59. default:
  60. System.out.println("谢谢你的使用");
  61. System.exit(0); //jvm中提供的退出程序
  62. break;
  63. }
  64. }
  65. }
  66. //封装一个方法用于查询学生的所有信息
  67. public static void findAllStudent(ArrayList<Student> array) throws IOException{
  68. //首先判断集合中有没有数据,如果是空的,那就要给出提示,并且该方法就不需要执行了 - 非空判断
  69. BufferedReader br = new BufferedReader(new FileReader("Student.txt"));
  70. String s;
  71. boolean flag = true;
  72. while((s=br.readLine())!=null){
  73. String[] strings = s.split(",");
  74. flag = false;
  75. System.out.println(Arrays.toString(strings));
  76. }
  77. if(flag){
  78. System.out.println("暂无学生");
  79. }
  80. br.close();
  81. }
  82. //封装一个方法用于添加学生信息
  83. public static void addStudent()throws IOException {
  84. //键盘对象
  85. Scanner sc = new Scanner(System.in);
  86. System.out.println("请输入ID");
  87. String id = sc.next();
  88. BufferedReader br = new BufferedReader(new FileReader("Student.txt"));
  89. String s;
  90. while((s=br.readLine())!=null){
  91. String[] str = s.split(",");
  92. if (id.equals(str[0])) {
  93. System.out.println("您输入的学号已经存在");
  94. break;
  95. }
  96. }
  97. //通过上面的验证之后就可以添加学生了
  98. System.out.println("请输入学生姓名");
  99. String name = sc.next();
  100. System.out.println("请输入学生年龄");
  101. String age = sc.next();
  102. System.out.println("请输入学生居住地");
  103. String address = sc.next();
  104. //写出到文件
  105. BufferedWriter bw = new BufferedWriter(new FileWriter("Student.txt",true));
  106. bw.write(id + "," + name + "," +age +"," + address);
  107. bw.newLine();
  108. bw.close();
  109. System.out.println("添加学生成功!");
  110. }
  111. //封装以个方法用于删除学生
  112. public static void deleteStudent(ArrayList<Student> array) throws IOException {
  113. //思路:键盘录入一个id(唯一),到集合中查找,看看是否有学生,如果有,则删除
  114. //键盘录入
  115. ArrayList<String> arr = new ArrayList<>();
  116. Scanner sc = new Scanner(System.in);
  117. System.out.println("请输入你要删除的学号");
  118. String id = sc.next();
  119. String s;
  120. BufferedReader bufferedReader = new BufferedReader(new FileReader("Student.txt"));
  121. while((s = bufferedReader.readLine())!= null){
  122. String[] strings = s.split(",");
  123. if (id.equals(strings[0])) {
  124. System.out.println("找到id为"+id+"的学生");
  125. }
  126. }
  127. for (String s1 : arr) {
  128. if (s1.startsWith(id)) {
  129. arr.remove(s1);
  130. }
  131. }
  132. BufferedWriter bw = new BufferedWriter(new FileWriter("Student.txt"));
  133. for (String s1 : arr) {
  134. bw.write(s1);
  135. bw.newLine();
  136. bw.flush();
  137. }
  138. bufferedReader.close();
  139. bw.close();
  140. System.out.println("删除学生成功");
  141. }
  142. //封装一个方法用于修改学生
  143. public static void updateStudent(ArrayList<Student> array) throws IOException {
  144. //思路:键盘录入一个id(唯一),到集合中查找,看看是否有学生,如果有,则删除
  145. //键盘录入
  146. Scanner sc = new Scanner(System.in);
  147. System.out.println("请输入你要删除的学生的学号:");
  148. String id = sc.nextLine();
  149. ArrayList<String> arr =new ArrayList<>();
  150. String s;
  151. BufferedReader br = new BufferedReader(new FileReader("Student.txt"));
  152. while((s = br.readLine())!=null) {
  153. arr.add(s);
  154. String[] strings = s.split(",");
  155. if (id.equals(strings[0])) {
  156. System.out.println("找到id为" + id + "的学生");
  157. }
  158. }
  159. System.out.println(arr);
  160. br.close();
  161. //修改的逻辑
  162. System.out.println("请输入学生的新的姓名:");
  163. String name = sc.next();
  164. System.out.println("请输入学生的新的年龄:");
  165. String age = sc.next();
  166. System.out.println("请输入学生的新的居住地:");
  167. String address = sc.next();
  168. for (int i = 0; i < arr.size(); i++) {
  169. if (arr.get(i).startsWith(id)) {
  170. arr.set(i, id + "," + name + "," + age + "," + address + ",");
  171. break;
  172. }
  173. }
  174. BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("Student.txt"));
  175. for (String s1 : arr) {
  176. System.out.println(s1);
  177. bufferedWriter.write(s1);
  178. bufferedWriter.newLine();
  179. bufferedWriter.flush();
  180. }
  181. bufferedWriter.close();
  182. //给出一个提示
  183. System.out.println("修改学生成功!");
  184. }
  185. }