Advertisement
teslariu

Ejemplo de interaccion entre clases

Nov 23rd, 2023
1,060
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.26 KB | None | 0 0
  1. /*
  2.  * Problema: un cliente interactua con una caja de ahorro y deposita
  3.  * $100000, ingresando su clave para  realizar el depósito
  4.  *
  5.  *
  6.  */
  7.  
  8.  
  9. public class Principal {
  10.    
  11.     class CajaDeAhorro{
  12.        
  13.         private float saldo;
  14.        
  15.         public void depositar(float monto){
  16.             saldo = saldo + monto;
  17.         }
  18.        
  19.         public void setSaldo(float s){
  20.             saldo = s;
  21.         }
  22.        
  23.         public float getSaldo(){
  24.             return saldo;
  25.         }
  26.        
  27.         // Constructor
  28.         CajaDeAhorro(float s){
  29.                 saldo = s;
  30.         }
  31.        
  32.     }
  33.    
  34.     class Cliente{
  35.    
  36.         private int DNI;
  37.         private String Clave;
  38.         private CajaDeAhorro Cuenta;
  39.        
  40.         // Constructor
  41.         Cliente(int d, String c, CajaDeAhorro cda){
  42.                 DNI = d;
  43.                 Clave = c;
  44.                 Cuenta = cda;
  45.         }
  46.        
  47.         // metodos
  48.         public CajaDeAhorro getCajaDeAhorro(){
  49.             return Cuenta;
  50.         }
  51.        
  52.         // getters y setters de DNI y Clave
  53.         //  ........
  54.    
  55.    
  56.    
  57.     }
  58.    
  59.     public static void main (String[] args) {
  60.        
  61.         // Construimos una instancia de caja de ahorro con $50.000
  62.         CajaDeAhorro c1 = new CajaDeAhorro(50000)
  63.        
  64.         // Construyo el cliente
  65.         Cliente cliente = new Cliente(38555122,"clave",c1)
  66.        
  67.         // deposito el dinero
  68.         cliente.Cuenta.depositar(100000)
  69.        
  70.         // verifico el saldo:
  71.         System.out.println("Saldo: " + cliente.Cuenta.getSaldo())
  72.        
  73.     }
  74. }
  75.  
  76.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement