Modify content of an arraylist

0

I need to make a bank in which account holder balances are updated after making a transaction.
The information of my clients is stored in an arraylist and I need only the balance to do it.
The information of the clients is given by no. account, name, age, address, balance, phone.

Try the command arraylist.set (index, "string"); but it does not work at all because my information is not completely a String.

public void subMenu(){       

Scanner entrada = new Scanner(System.in);   

System.out.printf( "Inserte su ID" );  
opcion1 = entrada.nextInt();     

System.out.println("\t\t Menu");    
System.out.println("\t\t  Elija una opcion ");    
System.out.println("\t\t 1. Consultar informacion");    
System.out.println("\t\t 2. Consultar saldo");    
System.out.println("\t\t 3. Realizar un deposito");    
System.out.println("\t\t 4. Realizar un retiro");    
System.out.println("\t\t 5. Traspaso");   
System.out.println("\t\t 6. Modificar datos");   
System.out.println("\t\t 7. Eliminar su registro");     
opcionMenu = entrada.nextInt();   

AperturaDeCuenta cuenta11 = new AperturaDeCuenta(); 

String info = cuenta11.toString(); 
String str[] = info.split("\n"); 


// Crear pacientes  
Clientes cliente1 = new Clientes(123423796, "Juan Camaney", 21, "Eje central no. 27 Coyoac•n CDMX", 10534.879, "5532678909");  
Clientes cliente2 = new Clientes(206317648, "Vanessa Camaney", 18, "Miguel Quevedo no. 456 Coyoac•n CDMX", 1500.12, "5578360921");  
Clientes cliente3 = new Clientes(314460847, "Miguel Camaney", 19, "La avante no.43 Iztapalapa CDMX", 22434.09, "5534812209");  
Clientes cliente4 = new Clientes(422990352, "Brandon Camaney", 32, "El rosal no.12 Benito Juare CDMX", 28332.02, "5528203756");  
Clientes cliente5 = new Clientes(597573100, "Diego Camaney", 26, "Potrero no. 89 Venustiano Carranza CDMX", 16576, "5527784148"); 
Clientes cliente6 = new Clientes(609337823, "Araceli Camaney", 47, "Florida no. 70 Viveros Coyoacán CDMX", 5486.25, "5534752309"); 
Clientes cliente7 = new Clientes(786685244,"Victor Camaney",30,"Campestre Churubusco no.90 Miguel Hidalgo CDMX", 6721109, "5634218860"); 
Clientes cliente8 = new Clientes(865979733, "Hugo Camaney",24,"Mujeres ilustres no. 36 Iztacalco CDMX",7744687.67,"5610943299"); 
Clientes cliente9 = new Clientes(962970253, "Páez Camaney",58,"Traineras no. 677 Xochimilco CDMX",56313086,"5546272100"); 
Clientes cliente10 = new Clientes(105903215, "Johan Camaney",24,"Mujeres ilustres no. 36 Iztacalco CDMX",706742680.21,"5637502184"); 
Clientes cliente11= new Clientes(numeroDeCuenta, nombre,edad,domicilio,monto,telefono); 

List<Clientes> clientes = new ArrayList<Clientes>();  
clientes.add(cliente1);  
clientes.add(cliente2);  
clientes.add(cliente3);  
clientes.add(cliente4);  
clientes.add(cliente5);  
clientes.add(cliente6);  
clientes.add(cliente7); 
clientes.add(cliente8); 
clientes.add(cliente9); 
clientes.add(cliente10); 
clientes.add(cliente11); 


//*******METODOS DE ACCESO ********//    

switch (opcionMenu){    
  case 1:    

    System.out.println("\nNumero de Cuenta: " + clientes.get(opcion1).getNumeroDeCuenta() +   
                       "\n Nombre: " + clientes.get(opcion1).getNombre() +   
                       "\n Edad: "  + clientes.get(opcion1).getEdad() +  
                       "\n Domicilio: " + clientes.get(opcion1).getDomicilio() +    
                       "\n Monto: " +  clientes.get(opcion1).getMonto() +   
                       "\n Telefono: " + clientes.get(opcion1).getTelefono());  
    break;   
  case 2:   
    System.out.println("Ver mi saldo " + clientes.get(opcion1).getMonto() );   
    break;   
  case 3:    
    System.out.println("cantidad a depositar" );  
    deposito = entrada.nextInt();  
    saldoIng = clientes.get(opcion1).getMonto();  
    saldo = deposito + saldoIng;  
    System.out.println("Tu saldo era " + clientes.get(opcion1).getMonto() + ", Cantidad ingresada " + deposito + ", Tu saldo actual es " + saldo ); 
  break;
    
asked by Mike OC 03.06.2018 в 19:04
source

2 answers

0

Try to put this after making the transaction, as long as you have the corresponding amount setter in the Customer class:

clientes.get(opcion1).setMonto(nuevaCantidad);

If you modify the object outside the list, it is also modified within it.

    
answered by 03.06.2018 в 22:34
0

For the method to work, you must overwrite the following methods in your client class

    @Override
public boolean equals (Object obj) {

        if (obj instanceof Cliente) {

        Cliente tmp = (Cliente) obj;

            if (this.id.equals(tmp.id)) { return true; } else { return false; }

    } else { return false; }

The hascode method

@Override
    public int hashCode() {
        return this.id;
    }

I've implemented an interface share to

public class Cliente implements Comparable<Cliente>{


    public int compareTo(Cliente o) {

        int resultado=0;

        if (this.id<o.id) {   resultado = -1;      }

        else if (this.id>o.id) {    resultado = 1;      }

        else {

              resultado = 0;   }

        }

        return resultado;

    }

}

in the main

List<Clientes> clientes = new ArrayList<Clientes>();
Cliente c = new Cliente(id,...);
int index= c,IDdget();
Cliente nuevo = (id,..);  
clientes.set(index, nuevo)
    
answered by 04.06.2018 в 17:59