/*
 * 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.Ejercicio3;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

/**
 *
 * @author mario
 */
public class Ejercicio3 {
    public static void main(String[] args) {
        try {
            //Nos aseguramos de que el fichero exista
            File archivo = new File("inventario.txt");
            if (!archivo.exists()) {
                archivo.createNewFile();
            }
            Scanner sc = new Scanner(System.in);
            int opcion = 0;
            do {
                System.out.println("Opciones Disponibles:\n"
                        + "1. Registrar un nuevo producto\n"
                        + "2. Mostrar lista de productos\n"
                        + "3. Salir");
                opcion = sc.nextInt();
                switch (opcion) {
                    case 1:
                        //BufferedWritter (Escribimos el fichero)
                        BufferedWriter escritura = new BufferedWriter (new FileWriter(archivo,true));
                        System.out.println("Nombre producto: ");
                        sc.nextLine();
                        String nombre = sc.nextLine();
                        System.out.println("Cantidad producto: ");
                        int cantidad = sc.nextInt();
                        System.out.println("Precio producto: ");
                        double precio = sc.nextDouble();
                        
                        String lineaEscritura;
                        lineaEscritura= nombre + " " + cantidad + " " + precio;
                        escritura.write("\n"+lineaEscritura);
                        escritura.close();
                        break;
                    case 2:
                        //BufferedReader (Leemos el fichero)
                        BufferedReader lectura = new BufferedReader(new FileReader(archivo));
                        System.out.println("Nombre - Cantidad - Precio");
                        String linea;
                        while ((linea = lectura.readLine())!= null){
                            System.out.println(linea);
                        }
                        break;
                    case 3:
                        System.out.println("Saliendo del programa");
                        break;
                    default:
                        break;
                }

            } while (opcion != 3);

        }catch(IOException ex){
            ex.printStackTrace();
        }
    }
}
