/home/caleb/ASDV-Java/Semester 3/Assignments/MP5-Binary-Files_CalebFontenot/src/com/calebfontenot/mp5/files_calebfontenot/Exercise17_05.java
/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package com.calebfontenot.mp5.files_calebfontenot;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.Serializable;
import java.io.ObjectOutputStream;
import java.util.Date;

/**
 *
 * @author caleb
 */
public class Exercise17_05 {
    public static void main(String[] args) {
        File dataFile = new File("Exercise17_05.dat");
        if(!dataFile.exists()) {
            try (ObjectOutputStream fileStream = new ObjectOutputStream(new FileOutputStream(dataFile))) {
                fileStream.writeObject(new DataContainer());
            } catch (IOException ex) {
                System.out.println(ex);
            }
        }
        DataContainer data = null;
        try (ObjectInputStream fileStream = new ObjectInputStream(new FileInputStream(dataFile))) {
            data = (DataContainer) fileStream.readObject();
        } catch (IOException ex) {
                System.out.println(ex);
            } catch (ClassNotFoundException ex) {
                System.out.println(ex);
            }
        // Now print out the data!
        System.out.println("We got the data from the file!");
        System.out.println(data.toString());
    }
}

class DataContainer  implements Serializable {
    int[] intArray = {1, 2, 3, 4};
    Date currentDate = new Date();
    double doubleMoment = 5.5;

    public int[] getIntArray()
    {
        return intArray;
    }

    public Date getCurrentDate()
    {
        return currentDate;
    }

    public double getDoubleMoment()
    {
        return doubleMoment;
    }

    @Override
    public String toString()
    {
        String intString = "[";
        for (int i = 0; i < intArray.length - 1; ++i) {
            intString += intArray[i];
            if (i == (intArray.length - 2)) {
                intString += "]";
            } else {
                intString += ", ";
            }
        }
        return "DataContainer{" + "intArray=" + intString + ", currentDate=" + currentDate + ", doubleMoment=" + doubleMoment + '}';
    }
}