No se imprimen los casos 2 y 3, fallo en los bucles
Publicado por lele (2 intervenciones) el 17/06/2019 11:21:42
Como puedo hacer para que el código encuentre dos Strings iguales (con el fin de que encuentre el producto previamente registrado)?
Muchas gracias.
Muchas gracias.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package seis;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner teclado = new Scanner (System.in);
Producto uno[]= new Producto[10];
for(int i=0; i<uno.length; i++) {
uno[i]= new Producto();
}
int i=0;
int n=1;
do {
System.out.println("Para dar de alta un nuevo producto, pulse 1");
System.out.println("Para buscar un producto, pulse 2");
System.out.println("Para modificar un producto, pulse 3");
System.out.println("Para salir, pulse 4");
int p=teclado.nextInt();
switch(p) {
case 1:
System.out.println("Introduzca el nombre del producto");
uno[i].setNombre(teclado.next());
System.out.println("Introduzca el precio del producto");
uno[i].setPrecio(teclado.nextDouble());
System.out.println("Introduzca las unidades en stock del producto");
uno[i].setCantidad(teclado.nextInt());
System.out.println("PRODUCTO REGISTRADO");
System.out.println();
System.out.println();
i++;
break;
case 2:
System.out.println("Introduzca el nombre del producto que desea buscar");
String m=teclado.next();
for(int k=0; k<uno.length; k++) {
if(uno[k].getNombre()==m) {
System.out.println(uno[k].toString());
System.out.println();
System.out.println();
}
}
break;
case 3:
System.out.println("Introduzca el nombre del producto a modificar");
String s=teclado.next();
for(int k=0; k<uno.length; k++) {
if(uno[k].getNombre()==s) {
System.out.println("Introduzca el nuevo precio del producto");
uno[k].setPrecio(teclado.nextDouble());
System.out.println("Introduzca las unidades en stock del producto");
uno[k].setCantidad(teclado.nextInt());
System.out.println();
System.out.println();
}
}
break;
case 4:
System.out.println("Gracias por utilizar el programa");
n=0;
break;
default:
System.out.println("OPERACIÓN INVÁLIDA. INTRODUZCA UN COMANDO VÁLIDO.");
System.out.println();
System.out.println();
}
}while(n>0);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package seis;
public class Producto {
private String nombre;
private double precio;
private int cantidad;
public Producto() {
}
public void setNombre (String nombre) {
this.nombre=nombre;
}
public void setPrecio (double precio) {
this.precio=precio;
}
public void setCantidad (int cantidad) {
this.cantidad=cantidad;
}
public String getNombre(){
return nombre;
}
public double getPrecio() {
return precio;
}
public int getCantidad() {
return cantidad;
}
public String toString() {
String mensaje="El precio es "+precio+" euros y hay "+cantidad+" unidades en stock.";
return mensaje;
}
}
Valora esta pregunta


0