> 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/ud6-solucions/colas-deque-e-arraydeque.md).

# Colas: Deque e ArrayDeque

1. Xestor de impresion

```java
package Colas.impresora;

public enum TipoDocumento {
    PDF,
    DOCX,
    PPT
}
```

```java
package Colas.impresora;

public class Documento {

    private String nome;
    private int tamano;
    private TipoDocumento tipo;

    public Documento(String nome, TipoDocumento tipo, int tamano){
        this.nome = nome;
        this.tipo = tipo;
        this.tamano = tamano;
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public int getTamano() {
        return tamano;
    }

    public void setTamano(int tamano) {
        this.tamano = tamano;
    }

    public TipoDocumento getTipo() {
        return tipo;
    }

    public void setTipo(TipoDocumento tipo) {
        this.tipo = tipo;
    }

    @Override
    public String toString() {
        return "Documento{" +
                "nome='" + nome + '\'' +
                ", tamano=" + tamano +
                ", tipo=" + tipo +
                '}';
    }
}

```

```java
package Colas.impresora;

import javax.print.Doc;
import java.util.ArrayDeque;
import java.util.Deque;

public class XestorImpresion {

    private Deque<Documento> colaImpresion;

    public XestorImpresion(){
        colaImpresion = new ArrayDeque<>();
    }

    public void enviarDocumento(Documento doc) {
        colaImpresion.addLast(doc); // FIFO estándar
    }

    public void enviarUrxente(Documento doc){
        colaImpresion.addFirst(doc);
    }

    public void imprimir(){
        System.out.println("Imprimiendo");
        while (!colaImpresion.isEmpty()){
            Documento doc = colaImpresion.pollFirst();
            System.out.println(doc);
        }
        System.out.println("Fin impresión");
    }

    public void cancelarUltimo(){
        if (!colaImpresion.isEmpty()){
            Documento doc = colaImpresion.pollLast();
            System.out.println("Eliminado da cola:" + doc);
        }
    }

    @Override
    public String toString() {
        return "XestorImpresion{" +
                "colaImpresion=" + colaImpresion +
                '}';
    }
}

```

```java
package Colas.impresora;

public class App {

    public static void main(String[] args) {
        XestorImpresion xestor = new XestorImpresion();

        // 1. Crear documentos iniciais
        Documento d1 = new Documento("informe",TipoDocumento.DOCX, 2000);
        Documento d2 = new Documento("informeDireccion",TipoDocumento.PDF, 3000);
        Documento d3 = new Documento("Marketing",  TipoDocumento.PPT, 20);

        // 2. Enviar ao xestor e mostrar estado
        System.out.println("--- Enviar 3 documentos ---");
        xestor.enviarDocumento(d1);
        xestor.enviarDocumento(d2);
        xestor.enviarDocumento(d3);
        System.out.println(xestor);
        System.out.println();

        // 3. Crear ManualUsuario.pdf e enviar de urxencia
        System.out.println("--- Enviar urxente ---");
        Documento urxente = new Documento("ManualUsuario",  TipoDocumento.PDF, 1);
        xestor.enviarUrxente(urxente);
        System.out.println(xestor);
        System.out.println();

        // 4. Cancelar último envío
        System.out.println("--- Cancelar último ---");
        xestor.cancelarUltimo();
        System.out.println(xestor);
        System.out.println();

        // 5. Imprimir seguinte documento
        System.out.println("--- Executar impresión ---");
        xestor.imprimir(); // Debería imprimir o ManualUsuario.pdf
        System.out.println(xestor);
    }
}

```

2. Pacientes

```java
package EstructurasDeDatos.Colas.Pacientes;

public abstract class Paciente {

    private String nome;
    private String nnss;

    public Paciente(String nome, String nnss) {
        this.nome = nome;
        this.nnss = nnss;
    }


    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public String getNnss() {
        return nnss;
    }

    public void setNnss(String nnss) {
        this.nnss = nnss;
    }


    public abstract int getPrioridade();

    public abstract boolean requiereIngreso();

    protected String getDatos(){
        return "nome='" + nome + '\'' +
                ", nnss='" + nnss;
    }

    @Override
    public String toString() {
        return "Paciente{" +
                "nome='" + nome + '\'' +
                ", nnss='" + nnss + '\'' +
                '}';
    }

}

```

```java
package EstructurasDeDatos.Colas.Pacientes;

import java.time.LocalDate;

public abstract class PacienteIngresable extends Paciente {

    private LocalDate fechaIni;
    private LocalDate fechaFin;

    public PacienteIngresable(String nome, String nnss) {
        super(nome, nnss);
    }

    public PacienteIngresable(String nome, String nnss, LocalDate ini, LocalDate fin) {
        super(nome, nnss);
        this.fechaFin = fin;
        this.fechaIni = ini;
    }

    public LocalDate getFechaIni() {
        return fechaIni;
    }

    public void setFechaIni(LocalDate fechaIni) {
        this.fechaIni = fechaIni;
    }

    public LocalDate getFechaFin() {
        return fechaFin;
    }

    public void setFechaFin(LocalDate fechaFin) {
        this.fechaFin = fechaFin;
    }

    @Override
    public String toString() {
        return "PacienteIngresable{" +
                super.getDatos() +
                ", fechaIni=" + fechaIni +
                ", fechaFin=" + fechaFin +
                '}';
    }
}

```

```java
package EstructurasDeDatos.Colas.Pacientes;

public class PacienteLeve extends Paciente{

    public PacienteLeve(String nome, String nnss) {
        super(nome, nnss);
    }

    @Override
    public int getPrioridade() {
        return 3;
    }

    @Override
    public boolean requiereIngreso() {
        return false;
    }
}

```

```java
package EstructurasDeDatos.Colas.Pacientes;

import java.time.LocalDate;

public class PacienteModerado extends PacienteIngresable {

    public PacienteModerado(String nome, String nnss, LocalDate ini, LocalDate fin) {
        super(nome, nnss, ini, fin);
    }

    public PacienteModerado(String nome, String nnss) {
        super(nome, nnss, null, null);
    }

    @Override
    public int getPrioridade() {
        return 2;
    }

    @Override
    public boolean requiereIngreso() {
        return (this.getFechaIni()!=null);
    }
}

```

```java
package EstructurasDeDatos.Colas.Pacientes;

import java.time.LocalDate;

public class PacienteGrave extends PacienteIngresable{

    public PacienteGrave(String nome, String nnss, LocalDate ini, LocalDate fin) {
        super(nome, nnss, ini, fin);
    }

    @Override
    public int getPrioridade() {
        return 1;
    }

    @Override
    public boolean requiereIngreso() {
        return true;
    }
}

```

```java
package EstructurasDeDatos.Colas.Pacientes;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;

public class Hospital {
    private String nome;
    private String direccion;
    private Deque<Paciente> cola;
    private List<Paciente> ingresados;
    private List<Paciente> atendidos;

    public Hospital(String nome, String direccion){
        this.nome = nome;
        this.direccion = direccion;
        this.cola = new ArrayDeque<>();
        this.ingresados = new ArrayList<>();
        this.atendidos = new ArrayList<>();
    }

    public void chegarPaciente(Paciente p) {
        cola.addLast(p);
    }

    public void chegarPacienteUrxencias(Paciente p) {
        if (p.getPrioridade() == 1) {
            cola.addFirst(p);
        } else {
            cola.addLast(p);
        }
    }

    public void atenderPaciente() {
        if (!cola.isEmpty()) {
            Paciente p = cola.poll();
            System.out.println("Atendendo a: " + p);

            if (p.requiereIngreso()) {
                if (ingresados.size() < 5) {
                    ingresados.add(p);
                    System.out.println("Paciente ingresado: " + p);
                } else {
                    System.out.println("Paciente derivado a outro hospital" + p);
                }
            } else {
                atendidos.add(p);
                System.out.println("Paciente dado de alta:" + p);
            }
        }

    }

    public void atenderTodos() {
        while (!cola.isEmpty()) {
            atenderPaciente();
        }
    }

    @Override
    public String toString() {
        return "Hospital: " + nome + "\n" +
                "Direccion: " + direccion + "\n" +
                "Cola: " + cola + "\n" +
                "Ingresados: " + ingresados + "\n" +
                "Atendidos: " + atendidos + "\n";
    }


}

```

```java
package EstructurasDeDatos.Colas.Pacientes;

import java.time.LocalDate;

public class App {
    public static void main(String[] args) {
        Hospital h = new Hospital("CHUAC", "A Coruña");

        h.chegarPaciente(new PacienteLeve("Ana", "1"));
        h.chegarPaciente(new PacienteGrave("Luis", "2", LocalDate.of(2026,01,23),LocalDate.of(2026,02,23) ));
        h.chegarPaciente(new PacienteModerado("Marta", "3"));

        h.chegarPacienteUrxencias(new PacienteModerado("Fernando", "4", LocalDate.of(2026,01,15),LocalDate.of(2026,02,01) ));
        h.chegarPacienteUrxencias(new PacienteGrave("Maria", "5", LocalDate.of(2026,3,23),LocalDate.of(2026,03,5) ));

        System.out.println(h);

        h.atenderTodos();

        h.chegarPaciente(new PacienteModerado("David", "6"));
        h.chegarPaciente(new PacienteLeve("Sara", "7"));

        System.out.println("\n" + h);
    }
}
```


---

# 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/ud6-solucions/colas-deque-e-arraydeque.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.
