将类层次结构对象列表写入文本文件?

问题描述

我正在做一个班级分配,我必须参加一个我已经制作的程序,该程序由层次结构中的多个类和一个“演示”类组成,该类根据每个对象的信息组成进行打印。现在我需要更改它,而不是在控制台上打印出来,它必须打印到一个文本文件用户输入文本文件名。这是“演示”主类的编码:

import java.lang.*;
import java.io.*;
import java.util.*;
/**
 * polymorphDemo
 *
 * @author (Sean Hall)
 * @version (4/12/21)
 */
public class polymorphDemo
{
    public static void main(String[] args) throws Exception
    {
        Person[] people = new Person[12];
        people[0] = new Student("Cotty,Manny",4910);
        people[1] = new Undergraduate("Kick,Anita",9931,1);
        people[2] = new Undergraduate("DeBanque,Robin",8812,4);
        people[3] = new Person("Bugg,June");
        people[4] = new Parent("DeBanque,Sylvia",1849);
        people[5] = new Alumni("McCombs,Daniel",1542,"Pi Kappa Alpha");
        people[6] = new StudentGovernment("Steele,Ashley",7025,"Class President");
        people[7] = new Employee("Jones,Jason",150,"English");
        people[8] = new Faculty("Greene,Erin",124,"Math","Professor of Calculus");
        people[9] = new Staff("McKinley,Aaron",86,"Maintenance",14);
        people[10] = new Person("Honda,Edward");
        people[11] = new Undergraduate("Huginkiss,Amanda",8230,2);
           
        //Scanner for user input
        Scanner keyboard = new Scanner(system.in);
            
        //prepare the output file
        System.out.print("Enter the name of the new File: ");
        String outputFileName = keyboard.nextLine();
            
        try (PrintWriter output = new PrintWriter(new File(outputFileName)))
        {
            for (Person p : people)
            {
                output.println(p);
                output.println();
            }
        }
    }
}

我知道它还没有完成,但编程就我已经掌握了。多类层次结构都链接到 person 对象,并且每个对象都有一条与其相关的特定信息。有人对如何将信息复制到保持这样的输出格式的文本文件有一些指示吗?

姓名:科蒂,曼尼
学号:4910

姓名:踢,安妮塔
学号:9931

姓名:德班克、罗宾
学号:8812

姓名:巴格,六月

姓名:德班克,西尔维亚
家庭编号:1849

姓名:麦库姆斯,丹尼尔
家庭编号:1542
校友会/姐妹会:Pi Kappa Alpha

姓名:斯蒂尔,阿什利
学号:7025
学生会头衔:班长

姓名:琼斯、杰森
员工编号:150
员工部:英文

姓名:格林、艾琳
员工编号:124
员工部门:数学
教员职称:微积分教授

姓名:麦金莱、亚伦
员工编号:86
员工部门:维修
员工薪级:14

姓名:本田,爱德华

姓名:Huginkiss,阿曼达
学号:8230
学生等级:1

我应该提到,这些链接在一起的每个班级在他们的班级中都有一种打印每个人/学生/本科生/等信息的方法。有,也许有某种方法可以根据课堂打印输出中的内容打印出信息?

解决方法

在类 Person 中实现一个方法来提供类的字符串表示,例如它可以是一个标准方法 toString 继承自 Object 类,便于打印 Person 及其子类:

// class Person
@Override
public String toString() {
    return "Name: " + this.name;
}

然后在子类中覆盖此实现并附加相关详细信息,使用 super.toString() 调用父类的实现:

// class Student extends Person
@Override
public String toString() {
    return String.format("%s%nStudent Number: %d",super.toString(),this.studentNumber);
}

Undergraduate 可以扩展 Student 但它不需要覆盖 toString 的实现并且可以重用 Student 的实现。

StudentGovernment 也可以扩展 Student 但它必须覆盖它的 toString 以添加有关标题的信息:

// class StudentGovernment extends Student
@Override
public String toString() {
    return String.format("%s%nStudent Government Title: %s",this.title);
}

Person 的所有子类依此类推。


应使用 try-with-resources 实现打印到文件,以确保正确关闭写入器。

此外,需要修改逻辑以读取文件名并创建一次编写器(不是为输入数组中的每个人):

// Scanner for user input
Scanner keyboard = new Scanner(System.in);

// prepare the output file
System.out.print("Enter the name of the new File: ");
String outputFileName = keyboard.nextLine();

try (PrintWriter output = new PrintWriter(new File(outputFileName))) {
    for (Person p : people) {
       output.println(p);
       output.println(); // empty line separating the data of persons
    }
}