Reputation: 75
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);
}
}
Upvotes: 0
Views: 616
Reputation: 48631
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?
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.
Upvotes: 1
Reputation: 4919
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....
Upvotes: 1