KsProgrammer82
KsProgrammer82

Reputation: 23

Writing Class Hierarchy objects list onto a text file?

I'm doing a class assignment where I have to take a program I already made consisting of multiple classes in a hierarchy and a 'demo' class that prints based on what each object's information consists of. Now I need to change it so that instead of printing out on console, it has to be printed to a text file with user input for the text file name. Here's the coding for the 'demo' main class:

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();
            }
        }
    }
}

I know it's not finished, but the programming is as far as I've gotten. The multiple class hierarchy are all linked to the person object and each object has a specific piece of information pertaining to it. Anyone have some pointers on how to get the information copied to a text file keeping an output format like this?

Name: Cotty, Manny
Student Number: 4910

Name: Kick, Anita
Student Number: 9931

Name: DeBanque, Robin
Student Number: 8812

Name: Bugg, June

Name: DeBanque, Sylvia
Family Number: 1849

Name: McCombs, Daniel
Family Number: 1542
Alumni Fraternity/Sorority: Pi Kappa Alpha

Name: Steele, Ashley
Student Number: 7025
Student Government Title: Class President

Name: Jones, Jason
Employee ID: 150
Employee Department: English

Name: Greene, Erin
Employee ID: 124
Employee Department: Math
Faculty Member Title: Professor of Calculus

Name: McKinley, Aaron
Employee ID: 86
Employee Department: Maintenance
Staff Paygrade: 14

Name: Honda, Edward

Name: Huginkiss, Amanda
Student Number: 8230
StudentLevel: 1

I should mention that each of these classes linked together have within their classes a way to print out the information each person/student/undergraduate/etc. has, maybe there's a certain way to print out the info based on what's in the class printout?

Upvotes: 1

Views: 339

Answers (1)

Nowhere Man
Nowhere Man

Reputation: 19565

Implement a method in class Person to provide a string representation of the class, for example it can be a standard method toString inherited from Object class that facilitates printing of Person and its subclasses:

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

Then in subclasses override this implementation and append related details calling parent's implementation with super.toString():

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

Undergraduate may extend Student but it does not need to override implementation of toString and may reuse the Student's implementation.

StudentGovernment may also extend Student but it has to override its toString to add information about title:

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

And so on for all subclasses of Person.


Printing to file should be implemented using try-with-resources to guarantee that the writer is closed properly.

Also, the logic needs to be modiied to read the filename and create the writer once (not for each person in the input array):

// 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
    }
}

Upvotes: 1

Related Questions