Está en la página 1de 12

Dentro del JFrame Cajero Automatico donde vamos a insertar los botones se crea el siguiente

código para cada botón

public class cajeroAutomatico extends javax.swing.JFrame {

cuentaBancaria []tarjetas; //Arreglo de cuentas bancarias

Persona cliente;

/**

* Creates new form cajeroAutomatico

*/

public cajeroAutomatico() {

initComponents();

tarjetas = new cuentaBancaria[3]; //Se crea el arreglo de 3 elementos

cliente = new Persona("Juan Perez Ibañez",(byte)32,'M', "PEIJ890312");

tarjetas[0] = new cuentaBancaria(cliente,12000,"3245");

cliente = new Persona("Carla Lira Martinez",(byte)50,'M', "LIMC700308");

tarjetas[1] = new cuentaBancaria(cliente,5000,"4451");

cliente = new Persona("Aida Zamora Dieguez",(byte)42, 'M', "ZADA79811");

tarjetas[2] = new cuentaBancaria(cliente,700,"2578");

/**

* Este metodo busca en un arreglo de cuentaBancaria un numero de cuenta espcifico

* @param t arreglo de cuentas bancarias

* @param b cuenta bancaria

* @return indice del arreglo que corresponde a la cuenta buscar.


* si no se encuentra la cuenta en el arreglo regresara -1

*/

private int buscaCuenta(cuentaBancaria []t, cuentaBancaria b) {

if (Arrays.asList(t).contains(b)){

return Arrays.asList(t).indexOf(b);

else{

return -1;

/**

* This method is called from within the constructor to initialize the form.

* WARNING: Do NOT modify this code. The content of this method is always

* regenerated by the Form Editor.

*/

@SuppressWarnings("unchecked")

// <editor-fold defaultstate="collapsed" desc="Generated Code">

private void initComponents() {

jButton1 = new javax.swing.JButton();

btnRetirar = new javax.swing.JButton();

btnDepositar = new javax.swing.JButton();

btnSaldo = new javax.swing.JButton();

jLabel1 = new javax.swing.JLabel();

txtNumeroCuenta = new javax.swing.JTextField();

btnBuscar = new javax.swing.JButton();

lblTitular = new javax.swing.JLabel();

lblSaldo = new javax.swing.JLabel();

jLabel2 = new javax.swing.JLabel();


txtCantidad = new javax.swing.JTextField();

lblNuevoSaldo = new javax.swing.JLabel();

jButton1.setText("jButton1");

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

setTitle("CajeroAutomatico");

btnRetirar.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N

btnRetirar.setMnemonic('R');

btnRetirar.setText("Retirar");

btnRetirar.setToolTipText("Retira dinero de tu cuenta");

btnRetirar.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

btnRetirarActionPerformed(evt);

});

btnDepositar.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N

btnDepositar.setMnemonic('D');

btnDepositar.setText("Depositar");

btnDepositar.setToolTipText("Aumenta el saldo de tu cuenta");

btnDepositar.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

btnDepositarActionPerformed(evt);

});

btnSaldo.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N


btnSaldo.setMnemonic('S');

btnSaldo.setText("Saldo");

btnSaldo.setToolTipText("Consulta tu saldo de tu cuenta");

btnSaldo.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

btnSaldoActionPerformed(evt);

});

jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N

jLabel1.setText("Numero de cuenta");

txtNumeroCuenta.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N

txtNumeroCuenta.setToolTipText("introduce el numero de cuenta bancaria ");

btnBuscar.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N

btnBuscar.setMnemonic('B');

btnBuscar.setText("Buscar");

btnBuscar.setToolTipText("busqueda de la cuenta ");

btnBuscar.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

btnBuscarActionPerformed(evt);

});

lblTitular.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N

lblTitular.setText("Titular:");

lblSaldo.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N


lblSaldo.setText("Saldo:");

jLabel2.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N

jLabel2.setText("Cantidad");

txtCantidad.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N

txtCantidad.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

txtCantidad.setText("0");

lblNuevoSaldo.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N

lblNuevoSaldo.setText("Nuevo Saldo:");

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());

getContentPane().setLayout(layout);

layout.setHorizontalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addComponent(jLabel1)

.addGap(0, 108, Short.MAX_VALUE))

.addComponent(txtNumeroCuenta)

.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
layout.createSequentialGroup()

.addGap(0, 0, Short.MAX_VALUE)

.addComponent(btnBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 118,


javax.swing.GroupLayout.PREFERRED_SIZE))

.addComponent(lblTitular, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

.addGroup(layout.createSequentialGroup()
.addContainerGap()

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addComponent(jLabel2)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)

.addComponent(txtCantidad))

.addComponent(lblNuevoSaldo, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))

.addGap(40, 40, 40)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING,
false)

.addComponent(btnRetirar, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

.addComponent(btnDepositar, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

.addComponent(btnSaldo, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))

.addGap(25, 25, 25))

.addGroup(layout.createSequentialGroup()

.addContainerGap()

.addComponent(lblSaldo, javax.swing.GroupLayout.PREFERRED_SIZE, 235,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))

);

layout.setVerticalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addGap(23, 23, 23)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)

.addComponent(btnRetirar, javax.swing.GroupLayout.PREFERRED_SIZE, 77,


javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup()

.addComponent(jLabel1)

.addGap(18, 18, 18)

.addComponent(txtNumeroCuenta, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(btnDepositar, javax.swing.GroupLayout.DEFAULT_SIZE, 90,


Short.MAX_VALUE)

.addComponent(lblTitular))

.addComponent(btnBuscar))

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

.addComponent(lblSaldo)

.addGap(18, 18, 18)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING,
false)

.addGroup(layout.createSequentialGroup()

.addComponent(btnSaldo, javax.swing.GroupLayout.PREFERRED_SIZE, 82,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addContainerGap())

.addGroup(layout.createSequentialGroup()

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(jLabel2)

.addComponent(txtCantidad, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

.addComponent(lblNuevoSaldo)

.addGap(28, 28, 28))))


);

pack();

setLocationRelativeTo(null);

}// </editor-fold>

private void btnBuscarActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

cuentaBancaria cuenta;

int indice;

cuenta = new cuentaBancaria();

cuenta.setNumeroCuenta(txtNumeroCuenta.getText());

indice = buscaCuenta(tarjetas,cuenta);

if (indice > -1){

lblTitular.setText("Titular: "+tarjetas[indice].getTitular().getNombre());

lblSaldo.setText("Saldo: "+tarjetas[indice].getSaldo());

else{

JOptionPane.showMessageDialog(this, "La cuenta bancaria NO EXISTE. Verificala");

private void btnDepositarActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

cuentaBancaria cuenta;

int indice;

cuenta = new cuentaBancaria();

cuenta.setNumeroCuenta(txtNumeroCuenta.getText());

indice = buscaCuenta(tarjetas,cuenta);
if (indice > -1){

tarjetas[indice].depositar(Float.parseFloat(txtCantidad.getText()));

lblNuevoSaldo.setText("NuevoSaldo: "+tarjetas[indice].getSaldo());

else{

JOptionPane.showMessageDialog(this, "La cuenta bancaria NO EXISTE. Verificala");

private void btnRetirarActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

cuentaBancaria cuenta;

int indice;

cuenta = new cuentaBancaria();

cuenta.setNumeroCuenta(txtNumeroCuenta.getText());

indice = buscaCuenta(tarjetas,cuenta);

if (indice > -1){

tarjetas[indice].retirar(Float.parseFloat(txtCantidad.getText()));

lblNuevoSaldo.setText("NuevoSaldo: "+tarjetas[indice].getSaldo());

else{

JOptionPane.showMessageDialog(this, "La cuenta bancaria NO EXISTE. Verificala");

private void btnSaldoActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

cuentaBancaria cuenta;

int indice;
cuenta = new cuentaBancaria();

cuenta.setNumeroCuenta(txtNumeroCuenta.getText());

indice = buscaCuenta(tarjetas,cuenta);

if (indice > -1){

tarjetas[indice].verSaldo();

else{

JOptionPane.showMessageDialog(this, "La cuenta bancaria NO EXISTE. Verificala");

/**

* @param args the command line arguments

*/

public static void main(String args[]) {

/* Set the Nimbus look and feel */

//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">

/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.

* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html

*/

try {

for (javax.swing.UIManager.LookAndFeelInfo info :


javax.swing.UIManager.getInstalledLookAndFeels()) {

if ("Nimbus".equals(info.getName())) {

javax.swing.UIManager.setLookAndFeel(info.getClassName());

break;

} catch (ClassNotFoundException ex) {


java.util.logging.Logger.getLogger(cajeroAutomatico.class.getName()).log(java.util.logging.Level.SE
VERE, null, ex);

} catch (InstantiationException ex) {

java.util.logging.Logger.getLogger(cajeroAutomatico.class.getName()).log(java.util.logging.Level.SE
VERE, null, ex);

} catch (IllegalAccessException ex) {

java.util.logging.Logger.getLogger(cajeroAutomatico.class.getName()).log(java.util.logging.Level.SE
VERE, null, ex);

} catch (javax.swing.UnsupportedLookAndFeelException ex) {

java.util.logging.Logger.getLogger(cajeroAutomatico.class.getName()).log(java.util.logging.Level.SE
VERE, null, ex);

//</editor-fold>

/* Create and display the form */

java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {

new cajeroAutomatico().setVisible(true);

});

// Variables declaration - do not modify

private javax.swing.JButton btnBuscar;

private javax.swing.JButton btnDepositar;

private javax.swing.JButton btnRetirar;

private javax.swing.JButton btnSaldo;

private javax.swing.JButton jButton1;


private javax.swing.JLabel jLabel1;

private javax.swing.JLabel jLabel2;

private javax.swing.JLabel lblNuevoSaldo;

private javax.swing.JLabel lblSaldo;

private javax.swing.JLabel lblTitular;

private javax.swing.JTextField txtCantidad;

private javax.swing.JTextField txtNumeroCuenta;

// End of variables declaration

También podría gustarte