0

I'm having a problem with 2 variables, the first of them is called "Num_Viajes" and the function is increasing it value by one everytime I call the method "de_Viaje". The second is called "Total_Pasajeros" and the function is make an addition between itself and another variable called "Num_Pasajeros", both variables are int.

Anyway the thing is, when I call the Method "Reporte_Final" both variable should print their results, for example: If I call the Method "de_Viaje" 2 times and I enter the value "45" for the variable "Num_Pasajeros" the program should return this:

Num_Viajes = 2 Total_Pasajeros = 90

But instead, it returns:

Num_Viajes = 1 Total_Pasajeros = 45

So I think that's because the program is not saving the values of my variables, so they are always restarting. How can I fix this?

Here's the code:

import javax.swing.*;

public class Empresa_Autobuses
{
    String Tipo_Vehiculo;
    String Analisis_Viaje;
    int Num_Pasajeros;
    int Num_Viajes;
    int Total_Pasajeros;
    int Prom_Pasajeros;

    public static void main(String ... args)
    {
        boolean Bandera_Falsa = true;
        Empresa_Autobuses Viajero = new Empresa_Autobuses();
        do
        {
            Viajero.de_Viaje();
            Viajero.Reporte_Viaje();
            Viajero.Solicitud_Viaje();
            Viajero.Reporte_Final();
        }while(Bandera_Falsa == true);
    }

    public void de_Viaje()
    {
        Empresa_Autobuses Viajero = new Empresa_Autobuses();
        Tipo_Vehiculo = JOptionPane.showInputDialog("Selecciona el Tipo de Autobus (G = Grande, P = Pequeño").toUpperCase();
        Num_Pasajeros = Integer.parseInt(JOptionPane.showInputDialog("Introduzca el Número de Pasajeros: "));
        if(Tipo_Vehiculo.equals ("G"))
        {
            if(Num_Pasajeros > 60)
            {
                JOptionPane.showMessageDialog(null, "¡Se ha superado la Capacidad Máxima del Autobus!");
                Viajero.Solicitud_Viaje();
            }
            else if(Num_Pasajeros >= 30)
            {
                Analisis_Viaje = "Ganancia";
                Num_Viajes++;
                Total_Pasajeros += Num_Pasajeros;
            }
            else
            {
                Analisis_Viaje = "Pérdida";
                Num_Viajes++;
                Total_Pasajeros += Num_Pasajeros;
            }
        }
        else if(Tipo_Vehiculo.equals ("P"))
        {
            if(Num_Pasajeros > 20)
            {
                JOptionPane.showMessageDialog(null, "¡Se ha superado la Capacidad Máxima del Autobus!");
                Viajero.Solicitud_Viaje();
            }
            else if(Num_Pasajeros >= 10)
            {
                Analisis_Viaje = "Ganancia";
                Num_Viajes++;
                Total_Pasajeros += Num_Pasajeros;
            }
            else
            {
                Analisis_Viaje = "Pérdida";
                Num_Viajes++;
                Total_Pasajeros += Num_Pasajeros;
            }
        }
        else
        {
            JOptionPane.showMessageDialog(null, "Opción Incorrecta");
            Viajero.Solicitud_Viaje();
        }
    }

    public void Reporte_Viaje()
    {
        JOptionPane.showMessageDialog(null, "Reporte de Viaje\nEl Tipo de Autobus fue: "+Tipo_Vehiculo+"\nEl Total de Pasajeros en el Viaje fue de: "+Num_Pasajeros+"\n"+Analisis_Viaje);
    }

    public void Solicitud_Viaje()
    {
        Empresa_Autobuses Viajero = new Empresa_Autobuses();
        String Solicitud;
        boolean flag = true;
        do
        {
            Solicitud = JOptionPane.showInputDialog("¿Quiere realizar otro Viaje? (Y/N)").toUpperCase();
            if(Solicitud.equals ("Y"))
            {
                Viajero.main();
            }
            else if(Solicitud.equals ("N"))
            {
                flag = true;
            }
            else
            {
                JOptionPane.showMessageDialog(null, "Opción Incorrecta");
                flag = false;
            }
        }while(flag == false);
    }

    public void Reporte_Final()
    {
        Prom_Pasajeros = Total_Pasajeros / Num_Viajes;
        JOptionPane.showMessageDialog(null, "El Número de Viajes realizados fue de: "+Num_Viajes+"\nEl Total de Pasajeros fue de: "+Total_Pasajeros+"\nEl Promedio de Pasajeros fue de: "+Prom_Pasajeros);
        System.exit(9999);
    }
}
Yulz
  • 75
  • 1
  • 1
  • 10
  • 1
    Try to follow Java naming conventions. Use `mixedCase` for methods/variables and use `CamelCase` for classes/interfaces. – Christian Tapia May 31 '14 at 23:03
  • Also, exiting with a status of 9999 is normally not what you want. Usually, you should just let your main method complete, unless you're dealing with GUI stuff that handles exiting somewhat differently. – dfeuer May 31 '14 at 23:10
  • @dfeuer I use the System.exit in the "Reporte_Final" method because that's the end of my program, if I put inside Main the program doesn't end. – Yulz May 31 '14 at 23:12
  • @YulzCoreSux, that's still not the right way. See http://stackoverflow.com/questions/258099/how-to-close-a-java-swing-application-from-the-code – dfeuer May 31 '14 at 23:16
  • @dfeuer Ok I'll check then. – Yulz May 31 '14 at 23:21
  • I suspect that your problem is related to calling `Viajero.main();` from `Solicitud_Viaje()`. This makes your application indirectly recursive. Your `main()` then instantiates another instance of your ` class. Your other methods are also instantiating your class when you really should be calling `this.` rather than `Viajero.` – Paulw11 Jun 01 '14 at 01:25
  • @Paulw11 Thank you so much, you're help me a lot. You're right, the main() call was the entire problem. I fix it creating a cycle with do-while and returning a boolean value instead calling the main(). Also you help me switching the Viajero. for this. and the program looks very well now. Thank you very much friend :D – Yulz Jun 01 '14 at 21:18

2 Answers2

1

The problem does not seem to be where you think it is. What happens when Num_Pasajeros>60? Why is the increment within a "then" block if it should always happen?

Edit

It appears very likely that the problem is that you run your main procedure from within your program. This second invocation creates a whole new object that is not related to the one you've been working with! Don't do that, and you should probably be fine. You have some issues with your Swing threading, but you probably shouldn't think about that yet.

Community
  • 1
  • 1
dfeuer
  • 48,079
  • 5
  • 63
  • 167
  • Ok, I try also moving the increment to another method, but still not working, the condition "Num_Pasajeros>60" is used for discard the values added because my limits in Vehicle Type A are 60 and in Type B are 20. – Yulz May 31 '14 at 23:20
  • Note: even if calling main is *not* the problem, it's still pretty much never what you want to do. – dfeuer May 31 '14 at 23:55
  • thanks for the explanation, I didn't undestand well at beginning, but then I realized that was a recursivity problem at main() method. Thanks a lot for the help :) – Yulz Jun 01 '14 at 21:21
1

Should debug it, take a break point @ Empresa_Autobuses Viajero = new Empresa_Autobuses(); and see whats happening what case (if/else branch) is running/executing, what are your variable actual values...

Anyway if you observe it carefully: if the care type is P, then you total_pasajeros will not summed with num pasarejos.

Tipo_Vehiculo.equals ("P"))

And +1 for the Java Code Convention: http://web.archive.org/web/20140222045352/http://www.oracle.com/technetwork/java/codeconv-138413.html (You make everybody job easier here if you keep youeself to that)

p.s: I do not know Italian language but common sense suggest these....

czupe
  • 4,740
  • 7
  • 34
  • 52