domingo, 23 de setembro de 2012

Fila



Fila
• Contêiner onde objetos são inseridos e removidos de acordo com o princípio:
– “o primeiro que entra é o primeiro que sai”
– FIFO – First In, First Out


Aplicações de Fila
• Aplicações Diretas
– Filas de espera (restaurantes, passagens, etc)
– Acesso a recursos compartilhados
• Aplicações indiretas
– Estrutura de dados auxiliares para algoritmos



 /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Fila;

/**
 *
 * @author Admin
 */
class MetodosQueue {

    private int maxSize;
    private long[] queArray;
    private int front;
    private int rear;
    private int nItems;
//--------------------------------------------------------------

    public MetodosQueue(int s) // constructor
    {
        maxSize = s;
        queArray = new long[maxSize];
        front = 0;
        rear = -1;
        nItems = 0;
    }
//--------------------------------------------------------------

    public void insert(long j) // put item at rear of queue
    {
        if (rear == maxSize - 1) // deal with wraparound
        {
            rear = -1;
        }
        queArray[++rear] = j;         // increment rear and insert
        nItems++;                     // one more item
    }
//--------------------------------------------------------------

    public long remove() // take item from front of queue
    {
        long temp = queArray[front++]; // get value and incr front
        if (front == maxSize) // deal with wraparound
        {
            front = 0;
        }
        nItems--;                      // one less item
        return temp;
    }
//--------------------------------------------------------------

    public long peekFront() // peek at front of queue
    {
        return queArray[front];
    }
//--------------------------------------------------------------

    public boolean isEmpty() // true if queue is empty
    {
        return (nItems == 0);
    }
//--------------------------------------------------------------

    public boolean isFull() // true if queue is full
    {
        return (nItems == maxSize);
    }
//--------------------------------------------------------------

    public int size() // number of items in queue
    {
        return nItems;
    }
}




package Fila;

import javax.swing.JOptionPane;

public class Consultorio {

    private Integer codPaciente;
// private String nmPaciente;
    public Integer getCodPaciente() {
        return codPaciente;
    }

    public void setCodPaciente(Integer codPaciente) {
        this.codPaciente = codPaciente;
    }

    public void displayConsultorio() {
        JOptionPane.showMessageDialog(null, "Código do Paciente: " + codPaciente);
    }
}


package Fila;

import javax.swing.JOptionPane;

public class Menu {

    public static void main(String[] args) {
        int item;
        int codPaciente;
// String nmPaciente;
        MetodosQueue metodosQueue = new MetodosQueue(10);
        do { //inicio do enquanto
            item = Integer.parseInt(JOptionPane.showInputDialog(null, "******************************\n"
                    + "** MENU DE OPÇÕES **\n"
                    + "* 1 - Inserir Paciente*\n"
                    + "* 2 - Chamar o Paciente p/ ser atendido *\n"
                    + "* 3 - Fila cheia ou vazia?* \n"
                    + "* 4 - Próximo Paciente a ser atendido *\n"
                    + "* 5 - Quantidade de Pacientes na fila*\n"
                    + "* 6 - Sair *\n"
                    + "******************************\n"
                    + "Digite sua opção: \n", "Consultório Médico",
                    JOptionPane.INFORMATION_MESSAGE));

            switch (item) { //inicio do case
                case 1: { //cadastrar correspondencias
                    if (!metodosQueue.isFull()) {
                        codPaciente = Integer.parseInt(JOptionPane.showInputDialog(null,
                                "Digite o Código do { paciente }  ", " Consultório Médico !", JOptionPane.INFORMATION_MESSAGE));

                        metodosQueue.insert(codPaciente);
                    } else {
                        JOptionPane.showMessageDialog(null, "Fila cheia! ");
                    }
                }
                break;
                case 2: { //remover paciente
                    // delete item from stack
                    if (!metodosQueue.isEmpty()) {
                        long value = metodosQueue.remove();
                        JOptionPane.showMessageDialog(null,
                                "Paciente chamado para ser atendido: " + value);
                    }
                }
                break;
                case 3: {
                    if (metodosQueue.isEmpty()) {
                        JOptionPane.showMessageDialog(null, "Fila vazia!");
                    } else {
                        if (metodosQueue.isFull()) {
                            JOptionPane.showMessageDialog(null, "Fila cheia!");
                        } else {
                            JOptionPane.showMessageDialog(null,
                                    "A Fila não está cheia, mas também , não está vazia !");
                        }
                    }
                }
                break;
                case 4: {
                    if (!metodosQueue.isEmpty()) {
                        long value = metodosQueue.peekFront(); // identificar o proximo.
                        JOptionPane.showMessageDialog(null, "Proxima da fila: " + value);
                    }
                }
                break;
                case 5: {
                    long value = metodosQueue.size(); // qtd de pacientes na fila
                    JOptionPane.showMessageDialog(null, "Quantidade de pacientes na fila: " + value);
                }
                break;
                default: {
                    if (item != 6) {
                        JOptionPane.showMessageDialog(null, "Valor inválido, digite novamente!");
                    } else {
                        if (item == 6) {
                            JOptionPane.showMessageDialog(null, "Você está saindo do programa!");
                        }
                    }
                }
                break;
            }
        } while (item != 6);
    }
}




Nenhum comentário:

Postar um comentário