Duvida Exercises Collections - Interface Queue

0

Personal I have a question in this exercise about Collections where I should use the Queue interface:

  • Write a program that simulates control of an airplane runway at an airport. In this program, the user must be able to perform the following tasks:
    a) List the number of airplanes waiting in the takeoff queue; Home b) To authorize the takeoff of the first airplane in the queue; Home c) Add an airplane to the queue; Home d) List all the airplanes in the queue; Home e) List the characteristics of the first airplane in the queue.
  • Consider that airplanes have a name and an integer as an identifier. Add other features as needed.

    I started my code like this but I stopped in question "B", I have doubts if it is necessary to use a method

    import java.util.ArrayList;
    
    public class ControledeTrafego {
    
        private String nome;
        private int codigo;
    
        public ControledeTrafego(String nome, int codigo) {
            super();
            this.nome = nome;
            this.codigo = codigo;
        }
        public String getNome() {
            return nome;
        }
        public void setNome(String nome) {
            this.nome = nome;
        }
        public int getCodigo() {
            return codigo;
        }
        public void setCodigo(int codigo) {
            this.codigo = codigo;
        }
    
        public void autorizacao(ControledeTrafego autorizacao){
            System.out.println("Favor comnfirmar a autorização do voo");
        }       
    }
    

    This is the Program:

    package Exercicio3;
    
    import java.util.LinkedList;
    import java.util.Queue;
    
    public class Controle {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Queue<ControledeTrafego> aviao =  new LinkedList<>();
    
            ControledeTrafego aviao1 = new ControledeTrafego(" Tam ", 747);
            ControledeTrafego aviao2 = new ControledeTrafego(" Gol ", 737);
            ControledeTrafego aviao3 = new ControledeTrafego(" Azul", 722);
    
            aviao.add(aviao1);
            aviao.add(aviao2);
            aviao.add(aviao3);
    
            System.out.println("Numero de avioes na fila de embarque : " +aviao.size());    
        }
    }
    
        
    asked by anonymous 12.09.2017 / 21:38

    1 answer

    1

    Although you do not understand why two queues, the implementation would look something like this:

    Class Aviao :

    public class Aviao {
    
      private String nome;
      private int codigo;
    
      public Aviao(String nome, int codigo) {
        super();
        this.nome = nome;
        this.codigo = codigo;
      }
    
      public String getNome() {
        return nome;
      }
    
      public void setNome(String nome) {
        this.nome = nome;
      }
    
      public int getCodigo() {
        return codigo;
      }
    
      public void setCodigo(int codigo) {
        this.codigo = codigo;
      }
    
      public void autorizacao(Aviao autorizacao) {
        System.out.println("Favor confirmar a autorização do vôo.");
      }
    
      @Override
      public String toString() {
        return this.getCodigo() + " - " + this.getNome();
      }
    }
    

    Class Controle :

    import java.util.LinkedList;
    import java.util.Queue;
    
    public class Controle {
    
      private final Queue<Aviao> espera = new LinkedList<>();
    
      public void listar() {
        System.out.println("============ LISTANDO ============");
        this.espera.forEach(a -> System.out.println(a));
      }
    
      public void listarPrimeiro() {
        System.out.println("============ PRIMEIRO ============");
        System.out.println(this.espera.element());
      }
    
      public void adicionar(Aviao aviao) {
        System.out.println("=========== ADICIONANDO ===========");
        System.out.println(aviao);
        this.espera.add(aviao);
      }
    
      public Aviao autorizarDecolagem() {
        Aviao aviao;
    
        aviao = this.espera.remove();
        System.out.println("====== AUTORIZANDO DECOLAGEM ======");
        System.out.println(aviao);
    
        return aviao;
      }
    
      public static void main(String[] args) {
        Controle controle = new Controle();
    
        controle.adicionar(new Aviao("Tam", 747));
        controle.adicionar(new Aviao("Gol", 737));
        controle.adicionar(new Aviao("Azul", 722));
    
        controle.listar();
        controle.autorizarDecolagem();
        controle.listar();
      }
    }
    

    The output generated for this implementation is as follows:

    =========== ADICIONANDO ===========
    747 - Tam
    =========== ADICIONANDO ===========
    737 - Gol
    =========== ADICIONANDO ===========
    722 - Azul
    ============ LISTANDO ============
    747 - Tam
    737 - Gol
    722 - Azul
    ====== AUTORIZANDO DECOLAGEM ======
    747 - Tam
    ============ LISTANDO ============
    737 - Gol
    722 - Azul
    
        
    12.09.2017 / 22:30