0

Hola que tal estoy aprendiendo java y me mandaron hacer un ejercicio y quería saber porque no carga bien el array que estoy haciendo mal ya probé en sacarle la instancia al menú y poner todos los métodos en static y mandar los array por referencia pero tampoco. Debo tener algún error grabe que al no entender bien el código se me esta pasado de largo si me pueden ayudar muchas gracias.

Al ejecutar el código y al querer mostrar los servicios cargados aparece lo siguiente: practico2.Servicio@7d4991ad

Les dejo el código:

package practico2;


import java.util.ArrayList;

import java.util.Scanner;

public class Practico2 {
     Scanner sc = new Scanner(System.in);

ArrayList<Servicio> servicio=new ArrayList<Servicio>();
ArrayList<Venta> venta=new ArrayList <Venta>(30);


public static void main(String[] args) {

    Practico2 practico= new Practico2();
    practico.menu();


}



public   void menu()  {


    int opcion=0;
    do {
        System.out.println("\nPajarito S.A.\n");

        System.out.println("[1] Cargar Servicios");
        System.out.println("[2] Cargar Pasajeros");
        System.out.println("[3] Borrar Pasajero ");
        System.out.println("[4] Mostrar Servicios");
        System.out.println("[5] Mostrar Servicios Con Pasajeros");
        System.out.println("[6] Salir");

        System.out.println("Ingrese Opcion:");
        opcion=sc.nextInt();

        switch(opcion)
        {
        case 1: cargarServicio();
        break;
        case 2: cargarVenta();
        break;
        case 3: eliminarPasajero();
        break;
        case 4: mostrarServicio();
        break;
        case 5: mostrarServCompleto();
        break;
        case 6:

        }
    }while (opcion !=6);


}


public  void cargarServicio () {

    int i=0;
    int x=0;

    do {
    i++;
    int id=i;   
    int dia,mes,anio;
    System.out.println("fecha de salida con el formato dd/mm/aaaa:");
    dia=sc.nextInt();
    mes=sc.nextInt();
    anio=sc.nextInt();
    Fecha fecha1= new Fecha(dia,mes,anio);

    sc.nextLine();
    System.out.println("ciudad de salida:");
    String csalida= sc.nextLine();


    System.out.println("fecha de llegada con formato dd/mm/aaaa:");
    dia=sc.nextInt();
    mes=sc.nextInt();
    anio=sc.nextInt();
    Fecha fecha2= new Fecha(dia,mes,anio);

    sc.nextLine();
    System.out.println("cuidad de llegada");    
    String cllegada= sc.nextLine();

    Servicio serv=new Servicio (id, fecha1,csalida,fecha2,cllegada);

    servicio.add(serv);
    System.out.println("0 seguir ingresando");
    x=sc.nextInt();

    }while(x==0);



}

public  void cargarVenta(){

    int dni;
    int i;
    int id;
    String nom;
    int posicion;

    System.out.println("id servicio para agregar persona");
    id = sc.nextInt();


    posicion=servicio.indexOf(id);


    if (posicion != -1) {

        do {
            sc.nextLine();

        System.out.println("nombre:");
        nom= sc.nextLine();
        System.out.println("documento:");
        dni=sc.nextInt();



        Venta vent = new Venta(nom,dni,id);
        venta.add(vent);
        System.out.println("0 para seguir ingresando ");
        i=sc.nextInt();


        }while(i==0 && venta.size()<30);

    }else {
        System.out.println("el id de servicio no es valido");
    }

}


public void mostrarServicio() {


    if(!servicio.isEmpty()) {

        System.out.println("servicios: ");

        for(int i=0; i<servicio.size(); i++) {
            System.out.println(servicio.get(i));
        }

    }else {
        System.out.println("no hay servicios cargados");
    }


}

public  void eliminarPasajero() {


    int doc;
    int posicion;

    System.out.println("ingrese documento del pasajero:");
    doc=sc.nextInt();

    posicion=venta.indexOf(doc);

    if(posicion != -1) {
        venta.remove(posicion);
        System.out.println("pasaje eliminado");
    }else {
        System.out.println("no se encuentra el pasajero");
    }

}


public void mostrarServCompleto() {

    int id;
    int posicionS,posicionV;


    System.out.println("ingrese id del servicio:");
    id=sc.nextInt();

    posicionS=servicio.indexOf(id);

    if(posicionS != -1) {

            System.out.println(servicio.get(posicionS));

    }else {
        System.out.println("no se encuentra el servicio");
    }


posicionV=venta.indexOf(id);

    if(posicionV != -1) {
        for(int i=0; i<venta.size(); i++) {
            System.out.println(venta.get(i));
        }

    }else {
        System.out.println("no hay pasajeros cargados");
    }



}

}

Les dejo también las otras clases:

package practico2;


public class Venta{

String nombre;
int documento;
int id;

public Venta(String nom, int doc,int id){


    this.nombre=nom;
    this.documento=doc;
    this.id=id;

}

public String getNombre() {
    return nombre;
}

public void setNombre(String nombre) {
    this.nombre = nombre;
}

public int getDocumento() {
    return documento;
}

public void setDocumento(int documento) {
    this.documento = documento;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}


}


package practico2;


public class Servicio   {

int id;
Fecha Fsalida;
String Csalida;
Fecha Fllegada;
String Cllegada;


public Servicio(int id, Fecha fsalida, String csalida, Fecha fllegada, String cllegada) {

    this.id=id;
    this.Fsalida=fsalida;
    this.Csalida=csalida;
    this.Fllegada=fllegada;
    this.Cllegada=cllegada;

}


public int getId() {
    return id;
}


public void setId(int id) {
    this.id = id;
}


public Fecha getFsalida() {
    return Fsalida;
}


public void setFsalida(Fecha fsalida) {
    Fsalida = fsalida;
}


public String getCsalida() {
    return Csalida;
}


public void setCsalida(String csalida) {
    Csalida = csalida;
}


public Fecha getFllegada() {
    return Fllegada;
}


public void setFllegada(Fecha fllegada) {
    Fllegada = fllegada;
}


public String getCllegada() {
    return Cllegada;
}


public void setCllegada(String cllegada) {
    Cllegada = cllegada;
}

}


package practico2;

public class Fecha {

int dia;
int mes;
int anio;

public Fecha(int dia,int mes, int anio) {

    this.dia=dia;
    this.anio=anio;
    this.mes=mes;
}

}
1
  • Mira Cómo preguntar para que tu pregunta sea mejor recibida. También, aprovecha y haz el recorrido de bienvenida para entender mejor cómo funcionamos y de paso obtener tu primera medalla! Aclara bien en donde de todo este codigo esta tu problema...
    – gbianchi
    el 20 abr. 2020 a las 16:26

1 respuesta 1

0

Eso pasa porque implícitamente se está llamando al método toString() que el objeto Servicio hereda de la clase Object. Es el equivalente a: System.out.println(servicio.get(i).toString()); Para que muestre datos contenidos en el objeto de tipo Servicio tienes que sobreescribir el método toString() para que retorne una cadena con el formato y datos deseados:

public class Servicio   {

    int id;
    Fecha Fsalida;
    String Csalida;
    Fecha Fllegada;
    String Cllegada;

    @Override
    public String toString(){
       return id + " " + Fsalida + " ";//otros campos;
     }


 }
1
  • Muchas gracias me sirvio
    – Pepe
    el 20 abr. 2020 a las 16:51

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.