根据以下这段话写出代码文件夹中有3个类文件(.java文件),Student.java StudentUtils.java StudentManager.java 学生管理系统 1.学生实体用Student类,包含学生学号,姓名,家庭住址,电话号码 2.学生操作用StudentUtils类,包含学生信息的新增,删除,修改,查询,保存 -每次新增学生时,要将学生信息保存到文件中 -每次删除学生时,要将相应学生信息从文件中删除 -每次修改学生时,要将相应学生信息在文件中修改 -学生信息保存在StudentInfo.txt中 3.学生管理用StudentManager类 当程序运行后,出现菜单供使用者选择 新增学生信息, 删除学生信息, 修改学生信息, 查询学生信息。
时间: 2024-02-24 11:55:07 浏览: 77
student_java.rar_java 学生管理系统_student_java_学生管理系统_学生管理系统 Java
5星 · 资源好评率100%
以下是实现上述功能的三个类文件:
Student.java
```java
public class Student {
private String id;
private String name;
private String address;
private String phone;
public Student(String id, String name, String address, String phone) {
this.id = id;
this.name = name;
this.address = address;
this.phone = phone;
}
// getters and setters
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 getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
```
StudentUtils.java
```java
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class StudentUtils {
private static final String FILE_NAME = "StudentInfo.txt";
public static void addStudent(Student student) {
List<Student> students = readFromFile();
students.add(student);
writeToFile(students);
}
public static void deleteStudent(String id) {
List<Student> students = readFromFile();
for (int i = 0; i < students.size(); i++) {
if (students.get(i).getId().equals(id)) {
students.remove(i);
break;
}
}
writeToFile(students);
}
public static void updateStudent(Student student) {
List<Student> students = readFromFile();
for (int i = 0; i < students.size(); i++) {
if (students.get(i).getId().equals(student.getId())) {
students.set(i, student);
break;
}
}
writeToFile(students);
}
public static Student getStudent(String id) {
List<Student> students = readFromFile();
for (Student student : students) {
if (student.getId().equals(id)) {
return student;
}
}
return null;
}
public static List<Student> getAllStudents() {
return readFromFile();
}
private static List<Student> readFromFile() {
List<Student> students = new ArrayList<>();
try (BufferedReader reader = new BufferedReader(new FileReader(FILE_NAME))) {
String line;
while ((line = reader.readLine()) != null) {
String[] parts = line.split(",");
Student student = new Student(parts[0], parts[1], parts[2], parts[3]);
students.add(student);
}
} catch (IOException e) {
e.printStackTrace();
}
return students;
}
private static void writeToFile(List<Student> students) {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(FILE_NAME))) {
for (Student student : students) {
writer.write(student.getId() + "," + student.getName() + "," + student.getAddress() + "," + student.getPhone());
writer.newLine();
}
} catch (IOException 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();
switch (choice) {
case 1:
addStudent(scanner);
break;
case 2:
deleteStudent(scanner);
break;
case 3:
updateStudent(scanner);
break;
case 4:
getStudent(scanner);
break;
case 5:
System.exit(0);
break;
default:
System.out.println("无效操作,请重新选择!");
break;
}
}
}
private static void addStudent(Scanner scanner) {
System.out.println("请输入学生学号:");
String id = scanner.next();
System.out.println("请输入学生姓名:");
String name = scanner.next();
System.out.println("请输入学生家庭住址:");
String address = scanner.next();
System.out.println("请输入学生电话号码:");
String phone = scanner.next();
Student student = new Student(id, name, address, phone);
StudentUtils.addStudent(student);
System.out.println("新增学生信息成功!");
}
private static void deleteStudent(Scanner scanner) {
System.out.println("请输入要删除的学生学号:");
String id = scanner.next();
StudentUtils.deleteStudent(id);
System.out.println("删除学生信息成功!");
}
private static void updateStudent(Scanner scanner) {
System.out.println("请输入要修改的学生学号:");
String id = scanner.next();
Student student = StudentUtils.getStudent(id);
if (student == null) {
System.out.println("未找到学生信息!");
return;
}
System.out.println("请输入学生姓名:");
String name = scanner.next();
System.out.println("请输入学生家庭住址:");
String address = scanner.next();
System.out.println("请输入学生电话号码:");
String phone = scanner.next();
student.setName(name);
student.setAddress(address);
student.setPhone(phone);
StudentUtils.updateStudent(student);
System.out.println("修改学生信息成功!");
}
private static void getStudent(Scanner scanner) {
System.out.println("请输入要查询的学生学号:");
String id = scanner.next();
Student student = StudentUtils.getStudent(id);
if (student == null) {
System.out.println("未找到学生信息!");
return;
}
System.out.println("学生学号:" + student.getId());
System.out.println("学生姓名:" + student.getName());
System.out.println("学生家庭住址:" + student.getAddress());
System.out.println("学生电话号码:" + student.getPhone());
}
}
```
阅读全文