Doubt Exercicio Collections - Queue Interface

Asked

Viewed 710 times

0

I’m having a question in this exercise about Collections in which I should use the Queue interface :

  1. Write a program that simulates control of an airfield at an airport. In this program, the user must be able to perform the following tasks:
    a) List the number of aircraft waiting in the takeoff queue;
    b) Allow take-off of the first aeroplane in line;
    c) Add an airplane to the queue;
    d) List all aircraft in the queue;
    e) List the characteristics of the first plane in line.

Consider that airplanes have a name and an integer number as identifier. Add other features as you see fit.

I started my code so 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());    
    }
}
  • I didn’t understand the difference between the takeoff line and the waiting line

1 answer

1


Although you don’t understand why two rows, the implementation would be 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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.