> For the complete documentation index, see [llms.txt](https://educacion.gitbook.io/programacion/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://educacion.gitbook.io/programacion/exercicios-java/ud5-solucions/csv.md).

# CSV

1. Videoxogos

```java
import java.time.LocalDate;

public class Videoxogo {

    private int id;
    private String nome;
    private LocalDate dataLanzamento;
    private String xenere;
    private String plataforma;
    private int puntuacion;

    public Videoxogo(int id, String nome, LocalDate dataLanzamento, String xenere, String plataforma, int puntuacion) {
        this.id = id;
        this.nome = nome;
        this.dataLanzamento = dataLanzamento;
        this.xenere = xenere;
        this.plataforma = plataforma;
        this.puntuacion = puntuacion;
    }

    // Getters
    public int getId() { return id; }
    public String getNome() { return nome; }
    public LocalDate getDataLanzamento() { return dataLanzamento; }
    public String getXenere() { return xenere; }
    public String getPlataforma() { return plataforma; }
    public int getPuntuacion() { return puntuacion; }

    @Override
    public String toString() {
        return id + " | " + nome + " | " + dataLanzamento + " | " + xenere + " | " + plataforma + " | " + puntuacion;
    }
}

```

```java
package dao;

import modelo.Videoxogo;

import java.io.*;
import java.time.LocalDate;

public class VideoxogoDAO {

    private String path;

    public VideoxogoDAO(String path) {
        this.path = path;
    }

    public Videoxogo[] lerVideoxogos(){

        Videoxogo[] lista = new Videoxogo[10];

        try (BufferedReader br = new BufferedReader(new FileReader(path))) {

            String liña;
            int i = 0;

            br.readLine(); // saltamos a cabeceira

            while ((liña = br.readLine()) != null && i < 10) {

                String[] partes = liña.split(",");

                int id = Integer.parseInt(partes[0]);
                String nome = partes[1];
                LocalDate data = LocalDate.parse(partes[2]);
                String xenero = partes[3];
                String plataforma = partes[4];
                int puntuacion = Integer.parseInt(partes[5]);

                lista[i] = new Videoxogo(id, nome, data, xenero, plataforma, puntuacion);
                i++;
            }
        } catch (IOException e){
            System.out.println("Error na lectura" + e.getMessage())
        }

        return lista;
    }

    public void actualizarVideoxogos(Videoxogo[] videoxogos) {

        try (BufferedWriter bw = new BufferedWriter(new FileWriter(path))) {

            // Escribimos cabeceira
            bw.write("ID,Nome,DataLanzamento,Xenero,Plataforma,Puntuacion");
            bw.newLine();

            for (Videoxogo v : videoxogos) {
                if (v != null) {
                    bw.write(v.getId() + "," +
                            v.getNome() + "," +
                            v.getDataLanzamento() + "," +
                            v.getXenero() + "," +
                            v.getPlataforma() + "," +
                            v.getPuntuacion());
                    bw.newLine();
                }
            }
        } catch (IOException e){
            System.out.println("Error na escritura" + e.getMessage())
        }
    }
}


```

```java
package service;

import dao.VideoxogoDAO;
import modelo.Videoxogo;

import java.io.IOException;

public class VideoxogoService {

    private VideoxogoDAO dao;

    public VideoxogoService(VideoxogoDAO dao) {
        this.dao = dao;
    }

    public Videoxogo[] obterVideoxogos() {
        return dao.lerVideoxogos();
    }

    public void gardarVideoxogos(Videoxogo[] videoxogos){
        dao.actualizarVideoxogos(videoxogos);
    }
}

```

```java
package app;

import dao.VideoxogoDAO;
import modelo.Videoxogo;
import service.VideoxogoService;

import java.io.IOException;
import java.time.LocalDate;

public class App {

    public static void main(String[] args) {

        try {

            VideoxogoDAO dao = new VideoxogoDAO("videoxogos.csv");
            VideoxogoService service = new VideoxogoService(dao);

            // 1️⃣ Ler ficheiro
            Videoxogo[] lista = service.obterVideoxogos();

            System.out.println("Listado de videoxogos:");
            for (Videoxogo v : lista) {
                if (v != null) {
                    System.out.println(v);
                }
            }

            // 2️⃣ Crear novo videoxogo
            Videoxogo novo = new Videoxogo(
                    6,
                    "Elden Ring",
                    LocalDate.of(2022, 2, 25),
                    "RPG",
                    "Multiplataforma",
                    9
            );

            // Engadilo no array
            for (int i = 0; i < lista.length; i++) {
                if (lista[i] == null) {
                    lista[i] = novo;
                    break;
                }
            }

            // 3️⃣ Actualizar ficheiro
            service.gardarVideoxogos(lista);

            System.out.println("Videoxogo engadido correctamente.");

        } catch (IOException e) {
            System.out.println("Erro: " + e.getMessage());
        }
    }
}

```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://educacion.gitbook.io/programacion/exercicios-java/ud5-solucions/csv.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
