/*
 * 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 examen10y11.Examen.Ejercicio4;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Date;
import java.util.Scanner;

/**
 *
 * @author mario
 */
public class Ejercicio4 {

    public static void main(String[] args) {

        try {

            File fichero = new File("niveles.dat");
            if (!fichero.exists()) {
                fichero.createNewFile();
            }

            Object[] registros = new Object[0];
            // Se lee fichero inicialmente para obtener array de registros actualizado.
            if (fichero.length() > 0) {
                ObjectInputStream lectura = new ObjectInputStream(new FileInputStream(fichero));
                registros = (Object[]) lectura.readObject();
            }

            Scanner sc = new Scanner(System.in);
            int opcion = 0;
            do {
                System.out.println("Introduce opcion");
                opcion = sc.nextInt();
                switch (opcion) {
                    case 1:
                        System.out.println("1.Añadir registro");

                        double PM;
                        System.out.println("Introduce PM: ");
                        PM = sc.nextDouble();

                        LocalDateTime ahora = LocalDateTime.now();

                        registros = Arrays.copyOf(registros, registros.length + 2);

                        registros[registros.length - 2] = PM;
                        registros[registros.length - 1] = ahora;

                        break;
                    case 2:
                        System.out.println("2.Mostrar registro");

                        // Mostrar Registros Leídos.
                        for (int i = 0; i < registros.length; i++) {
                            if (i % 2 == 0) { // Si es PAR se trata de Double
                                double pm = (Double) registros[i];
                                System.out.println(pm);
                            } else {
                                LocalDateTime fYh = (LocalDateTime) registros[i];
                                System.out.println(fYh);
                            }
                        }

                        break;
                    case 3:
                        System.out.println("3.Salir");

                        break;

                    default:
                        System.out.println("Opción no válida");
                        break;

                }
            } while (opcion != 3);

            ObjectOutputStream escritura = new ObjectOutputStream(new FileOutputStream(fichero));
            escritura.writeObject(registros);

        } catch (Exception ex) {
            ex.printStackTrace();
            System.out.println(ex.getMessage());
        }

    }
}
