I have a program with java beans and I need it to ask me once for the iron, but if the difference is greater than 10 between the initial iron and the new iron that has been introduced, a message pops up saying that I must do the review again. Well, that's where I want the loop so that it asks for the same thing all the time. If the difference is not greater than 10, the program ends.
main
Scanner scan = new Scanner(System.in);
int cantHierro = 10;
int nuevaHierro;
Paciente paciente1 = new Paciente ("Francisco", "Fuertes", 20, 123456789, 10, 20);
GeneraAnalisis analisis1 = new GeneraAnalisis();
analisis1.setProducto(paciente1);
System.out.print("Cantidad de hierro: ");
cantHierro = scan.nextInt();
System.out.print("Nueva cantidad de hierro: ");
nuevaHierro = scan.nextInt();
paciente1.setUltimoHierro(nuevaHierro);
paciente1.addPropertyChangeListener(analisis1);
do{
System.out.println("Vuelva a introducir la cantidad de hierro");
if((nuevaHierro - cantHierro) >10){
System.out.print("Nueva cantidad de hierro: ");
nuevaHierro = scan.nextInt();
paciente1.setUltimoHierro(nuevaHierro);
}
}while((nuevaHierro - cantHierro) > 10);
paciente1.addPropertyChangeListener(analisis1);
patient class
public class Paciente implements Serializable {
private String nombre;
private String apellidos;
private int edad;
private int telefono;
private int ultimoHierro;
private int ultimaUrea;
private PropertyChangeSupport propertySupport;
public Paciente() {
propertySupport = new PropertyChangeSupport(this);
}
public Paciente(String nombre, String apellidos , int edad, int telefono, int ultimoHierro, int ultimaUrea) {
propertySupport = new PropertyChangeSupport(this);
this.nombre = nombre;
this.apellidos = apellidos;
this.edad = edad;
this.telefono = telefono;
this.ultimoHierro = ultimoHierro;
this.ultimaUrea = ultimaUrea;
}
public String getNombre() {
return nombre;
}
public void setNombre(String descripcion) {
this.nombre = descripcion;
}
public String getApellidos() {
return apellidos;
}
public void setApellidos(String apellidos) {
this.apellidos = apellidos;
}
public int getEdad() {
return edad;
}
public void setEdad(int valorNuevo) {
this.edad = edad;
}
public int getTelefono() {
return telefono;
}
public void getTelefono(int telefono) {
this.telefono = telefono;
}
public int getUltimoHierro() {
return ultimoHierro;
}
public void setUltimoHierro(int hierroNuevo) {
int hierroAnterior = this.ultimoHierro;
this.ultimoHierro = hierroNuevo;
if ((hierroNuevo - hierroAnterior) > 10) //hay que realizar pedido
{
propertySupport.firePropertyChange("Hierro actual",
hierroAnterior, this.ultimoHierro);
//dejamos el stock anterior, no actualizamos
this.ultimoHierro = hierroAnterior;
}else{
System.out.println("Estas sano sanete");
}
}
public int getUltimaUrea() {
return ultimaUrea;
}
public void setUltimaUrea(int ultimaUrea) {
this.ultimaUrea = ultimaUrea;
}
public void addPropertyChangeListener(PropertyChangeListener
listener) {
propertySupport.addPropertyChangeListener(listener);
}
public void removePropertyChangeListener(PropertyChangeListener listener) {
propertySupport.removePropertyChangeListener(listener);
}
}
class generateAnalysis
public class GeneraAnalisis implements Serializable, PropertyChangeListener {
private int numAnalisis;
private Paciente paciente;
private Date fecha;
private int analisisPendiente;
public GeneraAnalisis() {
}
public GeneraAnalisis(int numeropedido, Paciente producto,
Date fecha, int cantidad) {
this.numAnalisis = numeropedido;
this.paciente = producto;
this.fecha = fecha;
this.analisisPendiente = cantidad;
}
public int getNumeropedido() {
return numAnalisis;
}
public void setNumeropedido(int numeropedido) {
this.numAnalisis = numeropedido;
}
public Paciente getProducto() {
return this.paciente;
}
public void setProducto(Paciente producto) {
this.paciente = producto;
}
public Date getFecha() {
return fecha;
}
public void setFecha(Date fecha) {
this.fecha = fecha;
}
public int getCantidad() {
return analisisPendiente;
}
public void setCantidad(int cantidad) {
this.analisisPendiente = cantidad;
}
public void propertyChange(PropertyChangeEvent evt) {
System.out.printf("Hierro anterior: %d%n", evt.getOldValue());
System.out.printf("Hierro Actual: %d%n", evt.getNewValue());
System.out.printf("Realizar Analisis en paciente urgentemente:%s%n",
paciente.getNombre() + " " + paciente.getApellidos());
//insertar pedido en la tabla
// BaseDatos.insertaPedido(producto, cantidad);
}
}
in the console it asks me twice in a row for the new iron
but of course, if I start the loop without having given values to the variable, it gives me an error. suggestions?
The problem is that you are using the structure
DO-WHILE
this means that the process is executed the first time and then it will evaluate the condition to repeat the code, what you need is the structureWHILE
which first evaluates the condition to determine if the code should be executed, The simplest modification would be this: