编写一个程序,模拟学校多个学生注册交学费流程。系统用户有学生、老师两类: (1)注册信息包括:学号、姓名、所属学院、专业、年级、班、宿舍。 缴费信息包括:学号、学期(例如:2022-2022-2)、学费、住宿费。 (2)功能需求:创建学生、老师两个子线程 1)学生线程实现注册、缴费; 2)老师线程统计不同学院的学费收缴金额、住宿费收缴金额。 (3)数据组织及存储要求 1)注册信息、缴费信息存储在.txt文件中; 2)注册、缴费信息要求使用泛型集合方式,具体实现类型自定; (4)测试数据要求: 1)存储注册信息的文件,至少有10条数据,至少涵盖2个学院、4个专业(不同专业对应的学费不同)。 2)存储缴费信息的文件,至少有10条数据,与注册信息关联。例如,某学生完成了注册,缴费,则在注册信息文件与缴费信息文件中新增两条信息,这两条信息有相同的学号。应Java写完整的代码,具体要求你定
时间: 2024-03-25 17:37:28 浏览: 44
好的,我根据你的要求编写了Java代码,如下所示:
Student.java
```java
public class Student {
private String id; // 学号
private String name; // 姓名
private String college; // 所属学院
private String major; // 专业
private String grade; // 年级
private String clazz; // 班级
private String dormitory; // 宿舍
public Student(String id, String name, String college, String major, String grade, String clazz, String dormitory) {
this.id = id;
this.name = name;
this.college = college;
this.major = major;
this.grade = grade;
this.clazz = clazz;
this.dormitory = dormitory;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public String getCollege() {
return college;
}
public String getMajor() {
return major;
}
public String getGrade() {
return grade;
}
public String getClazz() {
return clazz;
}
public String getDormitory() {
return dormitory;
}
@Override
public String toString() {
return id + "," + name + "," + college + "," + major + "," + grade + "," + clazz + "," + dormitory;
}
}
```
Teacher.java
```java
public class Teacher {
private String id; // 工号
private String name; // 姓名
private String college; // 所属学院
public Teacher(String id, String name, String college) {
this.id = id;
this.name = name;
this.college = college;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public String getCollege() {
return college;
}
@Override
public String toString() {
return id + "," + name + "," + college;
}
}
```
Payment.java
```java
public class Payment {
private String id; // 学号
private String term; // 学期
private double tuition; // 学费
private double accommodationFee; // 住宿费
public Payment(String id, String term, double tuition, double accommodationFee) {
this.id = id;
this.term = term;
this.tuition = tuition;
this.accommodationFee = accommodationFee;
}
public String getId() {
return id;
}
public String getTerm() {
return term;
}
public double getTuition() {
return tuition;
}
public double getAccommodationFee() {
return accommodationFee;
}
@Override
public String toString() {
return id + "," + term + "," + tuition + "," + accommodationFee;
}
}
```
RegistrationSystem.java
```java
import java.io.*;
import java.util.*;
public class RegistrationSystem {
private List<Student> students; // 学生信息
private List<Teacher> teachers; // 老师信息
private List<Payment> payments; // 缴费信息
public RegistrationSystem() {
students = new ArrayList<>();
teachers = new ArrayList<>();
payments = new ArrayList<>();
}
// 加载学生信息
public void loadStudents(String fileName) {
try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
String line;
while ((line = br.readLine()) != null) {
String[] data = line.split(",");
Student student = new Student(data[0], data[1], data[2], data[3], data[4], data[5], data[6]);
students.add(student);
}
} catch (IOException e) {
e.printStackTrace();
}
}
// 加载老师信息
public void loadTeachers(String fileName) {
try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
String line;
while ((line = br.readLine()) != null) {
String[] data = line.split(",");
Teacher teacher = new Teacher(data[0], data[1], data[2]);
teachers.add(teacher);
}
} catch (IOException e) {
e.printStackTrace();
}
}
// 加载缴费信息
public void loadPayments(String fileName) {
try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
String line;
while ((line = br.readLine()) != null) {
String[] data = line.split(",");
Payment payment = new Payment(data[0], data[1], Double.parseDouble(data[2]), Double.parseDouble(data[3]));
payments.add(payment);
}
} catch (IOException e) {
e.printStackTrace();
}
}
// 学生线程
public void studentThread() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入学生信息:");
System.out.print("学号:");
String id = scanner.nextLine();
System.out.print("姓名:");
String name = scanner.nextLine();
System.out.print("所属学院:");
String college = scanner.nextLine();
System.out.print("专业:");
String major = scanner.nextLine();
System.out.print("年级:");
String grade = scanner.nextLine();
System.out.print("班级:");
String clazz = scanner.nextLine();
System.out.print("宿舍:");
String dormitory = scanner.nextLine();
Student student = new Student(id, name, college, major, grade, clazz, dormitory);
students.add(student);
System.out.println("请输入缴费信息:");
System.out.print("学号:");
String paymentId = scanner.nextLine();
System.out.print("学期:");
String term = scanner.nextLine();
System.out.print("学费:");
double tuition = scanner.nextDouble();
System.out.print("住宿费:");
double accommodationFee = scanner.nextDouble();
Payment payment = new Payment(paymentId, term, tuition, accommodationFee);
payments.add(payment);
System.out.println("注册缴费成功!");
}
// 老师线程
public void teacherThread() {
Map<String, Double> collegeTuition = new HashMap<>();
Map<String, Double> collegeAccommodationFee = new HashMap<>();
for (Payment payment : payments) {
String college = findCollege(payment.getId());
if (!collegeTuition.containsKey(college)) {
collegeTuition.put(college, payment.getTuition());
collegeAccommodationFee.put(college, payment.getAccommodationFee());
} else {
collegeTuition.put(college, collegeTuition.get(college) + payment.getTuition());
collegeAccommodationFee.put(college, collegeAccommodationFee.get(college) + payment.getAccommodationFee());
}
}
System.out.println("学费收缴情况:");
for (String college : collegeTuition.keySet()) {
System.out.println(college + ":" + collegeTuition.get(college));
}
System.out.println("住宿费收缴情况:");
for (String college : collegeAccommodationFee.keySet()) {
System.out.println(college + ":" + collegeAccommodationFee.get(college));
}
}
// 查找学生所属学院
private String findCollege(String id) {
for (Student student : students) {
if (student.getId().equals(id)) {
return student.getCollege();
}
}
return null;
}
// 保存学生信息
public void saveStudents(String fileName) {
try (PrintWriter pw = new PrintWriter(new FileWriter(fileName))) {
for (Student student : students) {
pw.println(student);
}
} catch (IOException e) {
e.printStackTrace();
}
}
// 保存老师信息
public void saveTeachers(String fileName) {
try (PrintWriter pw = new PrintWriter(new FileWriter(fileName))) {
for (Teacher teacher : teachers) {
pw.println(teacher);
}
} catch (IOException e) {
e.printStackTrace();
}
}
// 保存缴费信息
public void savePayments(String fileName) {
try (PrintWriter pw = new PrintWriter(new FileWriter(fileName))) {
for (Payment payment : payments) {
pw.println(payment);
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
RegistrationSystem system = new RegistrationSystem();
// 加载测试数据
system.loadStudents("students.txt");
system.loadPayments("payments.txt");
// 创建学生、老师两个子线程
Thread studentThread = new Thread(() -> {
system.studentThread();
});
Thread teacherThread = new Thread(() -> {
system.teacherThread();
});
// 启动子线程
studentThread.start();
teacherThread.start();
try {
// 等待子线程结束
studentThread.join();
teacherThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
// 保存数据
system.saveStudents("students.txt");
system.savePayments("payments.txt");
}
}
```
其中,students.txt和payments.txt是存储测试数据的文件。其他的代码注释中已经说明了。你可以根据自己的需求进行修改和扩展。
阅读全文