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;
}
}