import java.util.Scanner; public class CatalogoDeVino2Eqe { public static void…
- User
import java.util.Scanner; public class CatalogoDeVino2Eqe { public static void main(String args[]) { Scanner leer = new Scanner(System.in);
final int TAM_ARREGLO = 100; int opc, cont = 100, celda = 0, dato = 0, resp = 0, nuevaClave, claveD = 0, claveConsulta = 0, claveModificacion = 0; boolean claveDuplicada = false, existeR = false, espacioD = false, claveExistente = false,claveRan = false; int clave[] = new int[TAM_ARREGLO]; String nombres[] = new String[TAM_ARREGLO]; float precio[] = new float[TAM_ARREGLO]; char estante[] = new char[TAM_ARREGLO]; // Menú do { System.out.println("CATALOGO DE VINOS \n"); System.out.println("[1] Registrar Vino:"); System.out.println("[2] Consultar Vino:"); System.out.println("[3] Consultar todos los Vinos:"); System.out.println("[4] Modificar vino:"); System.out.println("[5] Eliminar vino:"); System.out.println("[6] Finalizar programa:"); opc = leer.nextInt(); switch (opc) { case 1: // Altas cont = altasVinos(cont, TAM_ARREGLO, estante, claveD, clave, precio, nombres, espacioD,claveExistente,claveRan); break; case 2: // Consultar por Clave consultaVino(cont, estante, clave, precio, nombres, celda, claveConsulta, existeR); break; case 3: // Consultar todos los Vinos consultaVinos(cont, estante, clave, precio, nombres, existeR); break; case 4: // Modificar vino cont = modificarVino(claveModificacion, celda, cont, estante, clave, precio, nombres, dato, existeR); break; case 5://Eliminar vino cont = eliminarVinos(celda, cont, estante, clave, precio, nombres, dato, resp, claveConsulta, existeR); break; case 6: // Finalizar programa System.out.println("Programa finalizado."); break; default: System.out.println("Opcion no valida. Intentelo de nuevo."); } } while (opc != 6);}
static int altasVinos(int cont, int TAM_ARREGLO, char estante[], int claveD, int clave[], float precio[], String nombres[], boolean espacioD,boolean claveExistente,boolean claveRan) { Scanner leer = new Scanner(System.in);
espacioD = espacioArregloD(cont, espacioD, TAM_ARREGLO); if(espacioD){ System.out.println("La base vinos esta llena"); }else{ System.out.print("Ingrese la clave a registrar: \n"); System.out.print("Clave: \n"); claveD = leer.nextInt(); claveRan = checarRango(espacioD,claveD,claveExistente,clave,cont,claveRan); claveExistente = claveRep(espacioD, claveD, claveExistente, clave,cont); if (claveExistente) { System.out.println("La clave ya existe. Ingrese una nueva.\n"); } else { if (!claveRan){ System.out.println("La clave debe estar entre [1-111]. Ingrese una nueva.\n"); }else { clave[cont] = claveD; leer.nextLine(); // Limpia el enter de la entrada System.out.print("Nombre: \n"); nombres[cont] = leer.nextLine(); System.out.print("Precio: \n"); precio[cont] = leer.nextFloat(); System.out.print("Estante: \n"); estante[cont] = leer.next().charAt(0); leer.nextLine(); // Limpia el enter de la entrada System.out.println("Registro Exitoso"); leer.nextLine(); // Espera un enter cont++; } } } return cont;}
static void consultaVino(int cont, char estante[], int clave[], float precio[], String nombres[], int celda,int claveConsulta, boolean existeR) { Scanner leer = new Scanner(System.in); existeR = espacioArreglo(cont, existeR); if (existeR == true) { System.out.println("\nCONSULTAR VINO POR CLAVE"); System.out.print("Clave: \n"); claveConsulta = leer.nextInt(); celda = -1; for (int x = 0; x < cont; x++) { if (claveConsulta == clave[x]) { celda = x; } } if (celda != -1) { System.out.printf("Nombre: %s\n", nombres[celda]); System.out.printf("Precio: %.2f\n",precio[celda]); System.out.printf("Precio: %.2f\n", precio[celda]); System.out.printf("Estante: %c\n", estante[celda]); } else { System.out.println("No se encontró ningún vino con esa clave."); } } else { System.out.println("No hay vinos registrados."); } leer.nextLine(); // Limpia el enter de la entrada leer.nextLine(); // Espera un enter }
static void consultaVinos(int cont, char estante[], int clave[], float precio[], String nombres[], boolean existeR) { Scanner leer = new Scanner(System.in); existeR = espacioArreglo(cont, existeR);
if (existeR) { for (int i = 0; i < cont - 1; i++) { for (int j = 0; j < cont - i - 1; j++) { if (clave[j] > clave[j + 1]) { int temp = clave[j]; char tempc = estante[j]; float tempf = precio[j]; String temps = nombres[j]; clave[j] = clave[j + 1]; clave[j + 1] = temp; estante[j] = estante[j + 1]; estante[j + 1] = tempc; precio[j] = precio[j + 1]; precio[j + 1] = tempf; nombres[j] = nombres[j + 1]; nombres[j + 1] = temps; } } } System.out.println("\nCONSULTAR TODOS LOS VINOS"); for (int x = 0; x < cont; x++) { System.out.printf("Clave: %d\n", clave[x]); System.out.printf("Nombre: %s\n", nombres[x]); System.out.printf("Precio: %.2f\n", precio[x]); System.out.printf("Estante: %c\n", estante[x]); System.out.println("-----------------------"); } } else { System.out.println("No hay vinos registrados."); } leer.nextLine(); // Limpia el enter de la entrada leer.nextLine(); // Espera un enter}
static int modificarVino(int claveModificacion, int celda, int cont, char estante[], int clave[], float precio[], String nombres[], int dato, boolean existeR) { Scanner leer = new Scanner(System.in); existeR = espacioArreglo(cont, existeR); if (existeR) { System.out.println("\nMODIFICAR VINO"); System.out.print("Ingrese la clave del vino a modificar: \n"); claveModificacion = leer.nextInt(); celda = -1; for (int x = 0; x < cont; x++) { if (claveModificacion == clave[x]) { celda = x; } } if (celda != -1) { System.out.println("Ingrese los nuevos datos:"); leer.nextLine(); // Limpia el enter de la entrada System.out.print("Nombre: \n"); nombres[celda] = leer.nextLine(); System.out.print("Precio: \n"); precio[celda] = leer.nextFloat(); System.out.print("Estante: \n"); estante[celda] = leer.next().charAt(0); System.out.println("Modificación Exitosa"); } else { System.out.println("No se encontró ningún vino con esa clave."); } } else { System.out.println("No hay vinos registrados."); } leer.nextLine(); // Limpia el enter de la entrada leer.nextLine(); // Espera un enter return cont; }
static int eliminarVinos(int celda, int cont, char estante[], int clave[], float precio[], String nombres[], int dato, int resp, int claveConsulta, boolean existeR) { Scanner leer = new Scanner(System.in); existeR = espacioArreglo(cont, existeR); if (existeR) { System.out.println("\nELIMINAR VINO"); System.out.println("Ingrese la clave del vino a eliminar: "); claveConsulta = leer.nextInt(); celda = -1; for (int x = 0; x < cont; x++) { if (claveConsulta == clave[x]) { celda = x; } } if (celda != -1) { System.out.println("¿Está seguro que desea eliminar el vino? (1-Sí / 2-No)"); resp = leer.nextInt(); if (resp == 1) { for (int y = celda; y < cont - 1; y++) { clave[y] = clave[y + 1]; nombres[y] = nombres[y + 1]; precio[y] = precio[y + 1]; estante[y] = estante[y + 1]; } cont--; System.out.println("Vino eliminado exitosamente."); } else { System.out.println("Eliminación cancelada."); } } else { System.out.println("No se encontró ningún vino con esa clave."); } } else { System.out.println("No hay vinos registrados."); } leer.nextLine(); // Limpia el enter de la entrada leer.nextLine(); // Espera un enter return cont; }
static boolean espacioArreglo(int cont, boolean existeR) { if (cont > 0) { existeR = true; } return existeR; }
static boolean espacioArregloD(int cont, boolean espacioD, int TAM_ARREGLO) { if (cont < TAM_ARREGLO) { espacioD = true; } return espacioD; }
static boolean claveRep(boolean espacioD, int claveD, boolean claveExistente, int[] clave,int cont) { if (espacioD) { for (int i = 0; i < cont; i++) { if (claveD == clave[i]) { claveExistente = true; } } } return claveExistente; } static boolean checarRango(boolean espacioD, int claveD, boolean claveExistente, int[] clave,int cont,boolean claveRan) {
if (claveD >= 1 && claveD <= 111) { claveRan = true; } return claveRan;} } why it doesnt correctly stop me when cont is not less than 100
- Assistant
The issue is with the
espacioArregloDmethod. It is checking ifcontis less thanTAM_ARREGLO(which is 100), but it should be checking ifcontis equal toTAM_ARREGLO.Modify the
espacioArregloDmethod as follows:static boolean espacioArregloD(int cont, boolean espacioD, int TAM_ARREGLO) { if (cont == TAM_ARREGLO) { espacioD = true; } return espacioD; }This way, the method will return
trueonly ifcontis equal toTAM_ARREGLO, indicating that the array is full. - User
No i want it to give me true only when cont is more or equal to 100
- Assistant
Apologies for the confusion. If you want to return
trueonly whencontis more or equal to 100, you can modify theespacioArregloDmethod as follows:static boolean espacioArregloD(int cont, boolean espacioD, int TAM_ARREGLO) { if (cont >= TAM_ARREGLO) { espacioD = true; } return espacioD; }This way, the method will return
trueonly whencontis greater than or equal toTAM_ARREGLO, indicating that the array is full. - User
but why doesnt it work if i have that way, also sorry i only want it to be true if its less than tam arreglo
- Assistant
I apologize for the confusion. If you want the method
espacioArregloDto returntrueonly whencontis less thanTAM_ARREGLO, you can update the method as follows:static boolean espacioArregloD(int cont, boolean espacioD, int TAM_ARREGLO) { if (cont < TAM_ARREGLO) { espacioD = true; } return espacioD; }This way, the method will return
trueonly whencontis less thanTAM_ARREGLO, indicating that there is space available in the array. - User
But that is what i have
- Assistant
If the
espacioArregloDmethod is already implemented as shown above:static boolean espacioArregloD(int cont, boolean espacioD, int TAM_ARREGLO) { if (cont < TAM_ARREGLO) { espacioD = true; } return espacioD; }Then the issue may lie elsewhere in your code. One possibility is that the
TAM_ARREGLOvariable is not set to the correct value. Please double-check that you have correctly definedTAM_ARREGLOand that it is indeed set to 100.If you continue to experience difficulties, please provide more details or share the complete code so that we can help you further.