Help with queues, JAVA

0

I have to run a program that simulates a queue, the data of the objects must be those of a person (name, age, sex).

The program must insert the data and eliminate them in the order that is done in a queue, the data to be added must be provided by the user and the elimination of the elements in a cycle.

You must report which item was added and which item was deleted as well as print the entire queue after inserting the items and after deleting them all in the cycle.

I would greatly appreciate your response Here is my code:

import java.util.Scanner;
public class MainColas {

    public static void main(String[] args) {

        PERSONA obj1 = new PERSONA();
        LISTAPERSONA obj2 = new LISTAPERSONA();
        Scanner sc = new Scanner (System.in);
        for (int i =0; i<3; i++){       

            System.out.println("Agregue su nombre");
            String Nombre = sc.nextLine();              

            System.out.println("Inserta el sexo");  
            String Sexo = sc.nextLine();

            System.out.println("Agregue su Edad");
            String Edad = sc.nextLine();        

        obj1.setNombre(Nombre);
        obj1.setEdad(Edad);
        obj1.setSexo(Sexo);
        obj2.InsertarPersonaFinal(Nombre, Edad, Sexo);
        System.out.println("La persona que ingresó fué " +""+ obj1.getNombre() +" Años "+ obj1.getEdad() +" "+ obj1.getSexo());
        }
    }
}  

public class LISTAPERSONA {

    PERSONA PrimeraPersona;
    PERSONA UltimaPersona;
    String Lista;
    PERSONA ArregloPersonas [];
    PERSONA obj1 = new PERSONA();
        LISTAPERSONA(){
            Lista = "La listonga";


    }

        public void AgregarPersonas(int i){
            ArregloPersonas[i] = new PERSONA();

        }           

        public boolean estaVacia() {
             return PrimeraPersona == null; 
         }

         public void InsertarPersonaFinal( String n, String s, String e)
          {
             if ( estaVacia() ) 
                PrimeraPersona = UltimaPersona = new PERSONA (1);
             else 
                UltimaPersona = UltimaPersona.SiguientePersona = new PERSONA();
          }

         public PERSONA EliminarAlFinal()
          {
                 PERSONA elementoEliminado = UltimaPersona; 

                 if ( PrimeraPersona == UltimaPersona )
                     PrimeraPersona = UltimaPersona = null;
                 else
                 {
                  PERSONA actual = PrimeraPersona;                  

                 while ( actual.SiguientePersona!= UltimaPersona )
                     actual = actual.SiguientePersona;
                     UltimaPersona = actual; 
                     actual.SiguientePersona = null;
                 } 
                System.out.println("El elemento eliminado es " + elementoEliminado);
                    return elementoEliminado; 
                 }
}   

public class PERSONA {

    private int numero;
    String Nombre;
    String Sexo;
    String Edad;    
    PERSONA SiguientePersona;
    PERSONA AnteriorPersona;
    PERSONA UltimaPersona;
    PERSONA PrimeraPersona;     

    public PERSONA(){
        Nombre ="";
        Sexo = "";
        Edad = "";          
    }

    PERSONA (int num){
        this.numero = num;
        SiguientePersona = null;
        AnteriorPersona = null;
    }

    PERSONA (int num, PERSONA siguiente){
        numero = num;
        SiguientePersona = siguiente;

    }    

    public int getNumero() {
        return numero;
    }    

    public void setNumero(int numero) {
        this.numero = numero;
    }    

    public String getNombre() {
        return Nombre;
    }    

    public void setNombre(String nombre) {
        Nombre = nombre;
    }    

    public String getSexo() {
        return Sexo;
    }    

    public void setSexo(String sexo) {
        Sexo = sexo;
    }    

    public String getEdad() {
        return Edad;
    }    

    public void setEdad(String edad) {
        Edad = edad;
    }       
}
    
asked by Last Fencer 16.05.2016 в 01:37
source

2 answers

2

Here is a small functional example of the classes you need. Then add what you think is convenient, but first look how it works. I have used a stack (Stack) of LIFO type which means that the last thing to enter is the first thing to come out. If you need another type of battery or collection, look for information such as here

public class Persona {

    // Los datos miembro hazlos siempre privados
    // Inicializa siempre los datos miembro
    private String Nombre = "";
    private String Sexo = "";
    private String Edad = "";

    Persona (String Nombre, String Sexo, String Edad){
        this.Nombre = Nombre;
        this.Sexo = Sexo;
        this.Edad = Edad;
    }

    public String getNombre() {
        return Nombre;
    }    

    public void setNombre(String nombre) {
        Nombre = nombre;
    }    

    public String getSexo() {
        return Sexo;
    }    

    public void setSexo(String sexo) {
        Sexo = sexo;
    }    

    public String getEdad() {
        return Edad;
    }    

    public void setEdad(String edad) {
        Edad = edad;
    }           
}

public class ListaPersonas {
    private Stack<Persona> pila = new Stack();  

    void AñadirPersona(Persona nuevaPersona){
        pila.push(nuevaPersona);
    }

    Persona ConsultarÚltimaPersona(){
        return pila.peek();  
    }

    Persona SacarÚltimaPersona(){
        return pila.pop();
    }

    int BuscarPosiciónPersona(Persona persona){
      return pila.search(persona);
    }
}

Finally, in the main method:

   // creamos unos cuantos objetos 'Persona'
    Persona persona1 = new Persona("David","Hombre","30");
    Persona persona2 = new Persona("Montse","Mujer","25");
    Persona persona3 = new Persona("Pablo","Hombre","10");

    // creamos una 'lista' de 'Persona's
    ListaPersonas lista = new ListaPersonas();
    lista.AñadirPersona(persona1);
    lista.AñadirPersona(persona2);
    lista.AñadirPersona(persona3);

    // Ver la última persona introducida:
    System.out.println ("Última persona: " + lista.ConsultarÚltimaPersona().getNombre()); 

    // Sacar la última persona de la lista:
    System.out.println ("Eliminada la persona: " + lista.ConsultarÚltimaPersona().getNombre()); 

    // Buscar la posición de una persona en la lista:
    System.out.println ("Posición de Montse: " + lista.BuscarPosiciónPersona(persona2));
    
answered by 16.05.2016 в 18:52
0

You can use the implementation proposed by @Oundroni, what with some changes. First, if what you want is a queuing behavior, use

 LinkedList<Persona> cola=new LinkedList<Persona>() 

in the class ListarPersonas, because this data structure allows you a queue behavior, in addition you have implemented several methods such as .add () to add as a queue, with the methods .getFirst () and .getLast () pudes get the first and last element, also implements queue methods .poll () .peek (). I hope it helps you.

    
answered by 22.05.2016 в 09:28