Reputation: 3
So I have an assignment of doing a little inventory for a car company, that saves a code, brand, model, year and prize for the cars, but when i try to run the program it works great, it asks what it needs to ask and everything, the issue is that it doesnt save the new cars that I type in with the addCar method (nuevoVehiculo()) and I have been trying to figure it out for about 3 hours but with no success, any help would be appreciated.
Also sorry that some of the stuff inside the code is in spanish, Im from a latinoamerican country so spanish is what we do it with, any questions about it i can translate if necessary.
package javaapplication32;
import javax.swing.JOptionPane;
/**
*
* @author Luis
*/
public class Rutinas {
static Vehiculo loteVehiculos[] = new Vehiculo[5]; //Crea arreglo con objeto.
static Venta laVenta[] = new Venta[10];
public static void Inicializa() {
loteVehiculos[0] = new Vehiculo(1, "Toyota", "Yaris", 2018, 21000);//Se llena el primer vehiculo en el arreglo para empezar la lista.
loteVehiculos[1] = new Vehiculo(2, "Honda", "Civic", 2017, 18000);
for (int k = 2; k < loteVehiculos.length; k++) {
loteVehiculos[k] = new Vehiculo();//Se crean los otros vehiculos sin informacion.
}
}//Fin Inicializa.
public static int posicionVacia() {
int indice = -1;
for (int r = 0; r < loteVehiculos.length; r++) {
if (loteVehiculos[r].codigo == 0) {
indice = r;
break;
}
}
return indice;
}//Fin Posicion Vacia.
public static void nuevoVehiculo() { //Para llenar la informacion de los nuevos vehiculos.
int posicion = posicionVacia();
if (posicion > -1) {
int codigo = Integer.parseInt(JOptionPane.showInputDialog("Digite el codigo del nuevo vehiculo."));
String marca = JOptionPane.showInputDialog("Digite la marca del nuevo vehiculo");
String modelo = JOptionPane.showInputDialog("Digite el modelo del nuevo vehiculo");
int anio = Integer.parseInt(JOptionPane.showInputDialog("Digite el año del nuevo vehiculo"));
float precio = Float.parseFloat(JOptionPane.showInputDialog("Digite el precio del nuevo vehiculo en dolares"));
loteVehiculos[posicion] = new Vehiculo(codigo, marca, modelo, anio, precio);
} else {
JOptionPane.showMessageDialog(null, "No hay espacio para un nuevo vehiculo");
}
}//Fin Nuevo Vehiculo.
public static int buscaVehiculo(int codigo) {
int indice = -1;
for (int k = 0; k < loteVehiculos.length; k++) {
if (loteVehiculos[k].codigo == codigo) {
indice = k;
break;
}
}
return indice;
}//Fin Busca Vehiculo.
public static void eliminarVehiculo() {
int vehiculo = Integer.parseInt(JOptionPane.showInputDialog("Digite el codigo del vehiculo que desea borrar."));
int posicion = buscaVehiculo(vehiculo);
if (posicion >= 0) {
loteVehiculos[posicion].codigo = 0;
} else {
JOptionPane.showMessageDialog(null, "No se encuentra el vehiculo. ");
}
}//Fin Eliminar Vehiculo.
public static void listaVehiculos() {
for (int i = 0; i < loteVehiculos.length; i++) {
if (loteVehiculos[i].codigo > 0) {
System.out.println(loteVehiculos[i]);
}
}
}//Fin Lista de Vehiculos.
public static void InicioVentas() {
laVenta[0] = new Venta(1, 12345, 1, "20 de Abril 2018", 15000);
laVenta[1] = new Venta(4, 67890, 2, "10 de Abril 2018", 24000);
for (int g = 2; g < laVenta.length; g++) {
laVenta[g] = new Venta(); //Se crean las otras ventas.
}
}
public static void agregarVenta() {
}
public static void menuVehiculos() {
Rutinas.Inicializa();
int n = Integer.parseInt(JOptionPane.showInputDialog("Digite \n1.Ingresar nuevo vehiculo. \n2.Lista actual de vehiculos. \n3.Eliminar vehiculo. \n4.Salir "));
switch (n) {
case 1:
Rutinas.nuevoVehiculo();
Rutinas.menuVehiculos();
break;
case 2:
Rutinas.listaVehiculos();
Rutinas.menuVehiculos();
break;
case 3:
Rutinas.eliminarVehiculo();
Rutinas.menuVehiculos();
break;
case 4:
JOptionPane.showMessageDialog(null, "Gracias.");
break;
}
}//Fin Menu.
}
Upvotes: 0
Views: 38
Reputation: 4713
It's because every time you run method menuVehiculos
it performs this: Rutinas.Inicializa();
To fix this you could call the initialization from another class. For example:
public class Main {
public static void main(String[] args) {
Rutinas.Inicializa();
Rutinas.menuVehiculos();
}
}
Then your method menuVehiculos
does not need to do any initialization.
public static void menuVehiculos() {
int n = Integer.parseInt(JOptionPane.showInputDialog("Digite \n1.Ingresar nuevo vehiculo. \n2.Lista actual de vehiculos. \n3.Eliminar vehiculo. \n4.Salir "));
switch (n) {
case 1:
Rutinas.nuevoVehiculo();
Rutinas.menuVehiculos();
break;
case 2:
Rutinas.listaVehiculos();
Rutinas.menuVehiculos();
break;
case 3:
Rutinas.eliminarVehiculo();
Rutinas.menuVehiculos();
break;
case 4:
JOptionPane.showMessageDialog(null, "Gracias.");
break;
}
}//Fin Menu.
Upvotes: 1