根据以下这段话写出相应的代码文件夹中有3个类文件(.java文件),Student.java StudentUtils.java StudentManager.java 学生管理系统 1.学生实体用Student类,包含学生学号,姓名,家庭住址,电话号码 2.学生操作用StudentUtils类,包含学生信息的新增,删除,修改,查询,保存 -每次新增学生时,要将学生信息保存到文件中 -每次删除学生时,要将相应学生信息从文件中删除 -每次修改学生时,要将相应学生信息在文件中修改 -学生信息保存在StudentInfo.txt中 3.学生管理用StudentManager类 当程序运行后,出现菜单供使用者选择 新增学生信息, 删除学生信息, 修改学生信息, 查询学生信息。
时间: 2024-02-24 19:58:51 浏览: 80
以下是对应的代码:
Student.java
```java
public class Student {
private String id;
private String name;
private String address;
private String phoneNumber;
public Student(String id, String name, String address, String phoneNumber) {
this.id = id;
this.name = name;
this.address = address;
this.phoneNumber = phoneNumber;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public String getAddress() {
return address;
}
public String getPhoneNumber() {
return phoneNumber;
}
}
```
StudentUtils.java
```java
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class StudentUtils {
private static final String FILE_PATH = "StudentInfo.txt";
public static void addStudent(Student student) {
List<Student> students = getAllStudents();
students.add(student);
saveStudents(students);
}
public static void deleteStudent(String id) {
List<Student> students = getAllStudents();
students.removeIf(student -> student.getId().equals(id));
saveStudents(students);
}
public static void modifyStudent(Student student) {
List<Student> students = getAllStudents();
int index = -1;
for (int i = 0; i < students.size(); i++) {
if (students.get(i).getId().equals(student.getId())) {
index = i;
break;
}
}
if (index != -1) {
students.set(index, student);
saveStudents(students);
}
}
public static Student getStudent(String id) {
List<Student> students = getAllStudents();
for (Student student : students) {
if (student.getId().equals(id)) {
return student;
}
}
return null;
}
public static List<Student> getAllStudents() {
List<Student> students = new ArrayList<>();
File file = new File(FILE_PATH);
if (!file.exists()) {
return students;
}
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
String line;
while ((line = reader.readLine()) != null) {
String[] fields = line.split(",");
if (fields.length == 4) {
students.add(new Student(fields[0], fields[1], fields[2], fields[3]));
}
}
} catch (IOException e) {
e.printStackTrace();
}
return students;
}
private static void saveStudents(List<Student> students) {
File file = new File(FILE_PATH);
try (PrintWriter writer = new PrintWriter(file)) {
for (Student student : students) {
writer.println(String.format("%s,%s,%s,%s", student.getId(), student.getName(),
student.getAddress(), student.getPhoneNumber()));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
```
StudentManager.java
```java
import java.util.Scanner;
public class StudentManager {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
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.退出程序");
int choice = scanner.nextInt();
scanner.nextLine();
switch (choice) {
case 1:
System.out.println("请输入学生信息(学号,姓名,家庭住址,电话号码):");
String[] fields = scanner.nextLine().split(",");
if (fields.length == 4) {
StudentUtils.addStudent(new Student(fields[0], fields[1], fields[2], fields[3]));
}
break;
case 2:
System.out.println("请输入学生学号:");
String id = scanner.nextLine();
StudentUtils.deleteStudent(id);
break;
case 3:
System.out.println("请输入学生信息(学号,姓名,家庭住址,电话号码):");
fields = scanner.nextLine().split(",");
if (fields.length == 4) {
StudentUtils.modifyStudent(new Student(fields[0], fields[1], fields[2], fields[3]));
}
break;
case 4:
System.out.println("请输入学生学号:");
id = scanner.nextLine();
Student student = StudentUtils.getStudent(id);
if (student != null) {
System.out.println(String.format("学号:%s,姓名:%s,家庭住址:%s,电话号码:%s",
student.getId(), student.getName(), student.getAddress(), student.getPhoneNumber()));
} else {
System.out.println("未找到该学生信息!");
}
break;
case 5:
return;
default:
System.out.println("无效的操作,请重新选择!");
}
}
}
}
```
阅读全文