Help to find the error

0

I need if you can help me find the error to this program, which consists of bank accounts, the problem is that I throw error when making and turn a deposit of an account created.

Thank you

package control_7;

import javax.swing.JOptionPane;

public class Control_7 {

    int contar=0, nro=201801;
    CuentaBancaria cuenta [] = new CuentaBancaria[5];

    public Control_7(){

        String ax;
        int op=0;

        do{
            ax = JOptionPane.showInputDialog(null, "1. CREAR UNA CUENTA BANCARIA \n"
                                                  +"2. VER LA LISTA DE CLIENTES \n"
                                                  +"3. EFECTUAR UN DEPOSITO \n"
                                                  +"4. EFECTUAR UN GIRO \n"
                                                  +"5. SALIR");
            if(ax!=null && !ax.equals("")){
                op = Integer.parseInt(ax);

                switch(op){
                    case 1:
                        ingresar();
                        break;
                    case 2:
                        mostrar();
                        break;
                    case 3:
                        deposito();
                        break;
                    case 4:
                        girar();
                        break;
                    case 5:
                        break;
                    default:
                        JOptionPane.showMessageDialog(null, "INGRESE UNA OPCION CORRECTA!!!");                  
                }
            }

        }while(op!=5);
    }

    public void ingresar(){        


        String nombre, apellido, direccion;
        Double saldo;        

        nombre = JOptionPane.showInputDialog(null, "INGRESAR NOMBRE DEL CLIENTE: ");
        apellido = JOptionPane.showInputDialog(null, "INGRESAR EL APELLIDO: ");
        direccion = JOptionPane.showInputDialog(null, "INGRESE LA DIRECCION DEL CLIENTE: ");
        saldo = Double.parseDouble(JOptionPane.showInputDialog(null, "INGRESE DEPOSITO INICIAL: "));

        CuentaBancaria temp = new CuentaBancaria();

        temp.setNro(nro); 
        temp.setNombre(nombre); 
        temp.setApellido(apellido);
        temp.setDireccion(direccion);
        temp.setSaldo(saldo);        

        cuenta[contar] = temp;
        contar++;  
        nro++;      
    }

    private void mostrar(){

        String ay="";

        for(int i=0; i<contar; i++){
            ay+="NUMERO DE CUENTA:    "+cuenta[i].getNro()+"\n"
              + "NOMBRE:              "+cuenta[i].getNombre()+"\n"
              + "APELLIDO:            "+cuenta[i].getApellido()+"\n"
              + "DIRECCION:           "+cuenta[i].getDireccion()+"\n"
              + "SALDO DE LA CUENTA:  "+cuenta[i].getSaldo()+"\n\n";        
        }
        JOptionPane.showMessageDialog(null, ay);
    }

    private void deposito(){

        String ax;
        Double dep = null;
        int c;

        ax = JOptionPane.showInputDialog(null, "INGRESE EL NUMERO DE CUENTA A LA CUAL DESEA EFECTUAR EL DEPOSITO");

        if(ax!=null && !ax.equals("")){
            c = Integer.parseInt(ax); ax="";
            if(contar!=0){
                for(int i=0; i<contar; i++){
                    if(cuenta[i].getNro() == c){
                        ax="";
                        dep =Double.parseDouble(JOptionPane.showInputDialog(null, "INGRESE EL MONTO DEL DEPOSITO"));
                        if (dep>0){
                        dep+=cuenta[i].getSaldo();
                        ax+="Número de Cuenta:   "+cuenta[i].getNro()+"\n"
                          + "Nombre:             "+cuenta[i].getNombre()+"\n"
                          + "Apellido:           "+cuenta[i].getApellido()+"\n"
                          + "Direccion:          "+cuenta[i].getDireccion()+"\n"
                          + "Saldo:              "+cuenta[i].getSaldo()+"\n";

                          break;
                        }
                    }
                    else{
                        ax="  LA CUENTA NO ESTA ASIGNADA \n"
                          +"       INTENTE NUEVAMENTE... \n"; 
                    }
                }
                JOptionPane.showMessageDialog(null, ax);
            }

        }
    }


    private String girar(){

        String an;
        Double ret=null;
        int c;

        an = JOptionPane.showInputDialog(null, "INGRESE EL NUMERO DE CUENTA A LA CUAL DESEA EFECTUAR EL GIRO");

        if(an!=null && !an.equals("")){
            c = Integer.parseInt(an); an="";
            if(contar!=0){
                for(int i=0; i<contar; i++){
                    if(cuenta[i].getNro() == c){
                        an="";
                        ret =Double.parseDouble(JOptionPane.showInputDialog(null, "INGRESE EL MONTO DE RETIRO"));
                        if(ret>cuenta[i].getSaldo()){
                            return "SALDO INSUFICIENTE\n"
                                 + "INGRESE NUEVO MONTO\n";
                        }else{
                        ret-=cuenta[i].getSaldo();
                        an+="Número de Cuenta:   "+cuenta[i].getNro()+"\n"
                          + "Nombre:             "+cuenta[i].getNombre()+"\n"
                          + "Apellido:           "+cuenta[i].getApellido()+"\n"
                          + "Direccion:          "+cuenta[i].getDireccion()+"\n"
                          + "Saldo:              "+cuenta[i].getSaldo()+"\n";
                          break;
                        }
                    }
                    else{
                        an="  LA CUENTA NO ESTA ASIGNADA \n"
                          +"       INTENTE NUEVAMENTE... \n"; 
                    }
                }
                JOptionPane.showMessageDialog(null, an);
            }
        }
        return null;
    }

    public static void main(String[] args) {

        Control_7 y = new Control_7();
        System.exit(0);        
    }
    
asked by Danilo Antonio Lopez Vega 16.10.2018 в 03:13
source

1 answer

1

What I see is that in your deposit method () you are not changing the value of the new balance, these are adding but you are not changing it.

dep =Double.parseDouble(JOptionPane.showInputDialog(null, "INGRESE EL MONTO DEL DEPOSITO"));
                        if (dep>0){
                        dep+=cuenta[i].getSaldo();  //Solo se suma pero no se cambia

To be able to change the balance you will need to use the set of your variable, in such a way that it would stay like this:

dep =Double.parseDouble(JOptionPane.showInputDialog(null, "INGRESE EL MONTO DEL DEPOSITO"));
                        if (dep>0){
                        dep+=cuenta[i].getSaldo();  //Solo se suma pero no se cambia
                        cuenta[i].setSaldo(dep);    //Se actualiza el nuevo saldo

And in the method of turning () your error lies in the misuse of the assignment operator - =.

In your code you are placing it like this

ret-=cuenta[i].getSaldo();

If we review the role of that operator in an example:

A- = B is the equivalent of saying A = A-B.

Then applied to your solution, the equivalent would be

ret = ret - cuenta[i].getSaldo() 

which causes the result to be cast as negative.

To solve that, it is enough to restructure the way in which it is subtracting and then as in the method of depositing, updating.

            double total = cuenta[i].getSaldo()-ret;   //Resta el monto del saldo actual                                        
            cuenta[i].setSaldo(total);  //Actualiza el total de la cuenta

With this you would fix the problems of addition and subtraction of the initial balance. Greetings.

    
answered by 16.10.2018 / 04:41
source