Ayuda - Búsqueda - Miembros - Calendario
Versión Completa: AYUDA CON TRABAJO - JAVA
::ChileWarez.org - Compartir No tiene Limites! > Rincón Técnico > Programación (NO Soporte PC)
Felipin_182
TENGO ESTAS CLASES DE UN CAJERO AUTOMATICO, LOGRO INGRESAR SIN PROBLEMAS PERO AL MOMENTO DE GIRAR ME LANZA UN ERROR QUE EL INDICE DEL ARREGLO SALDO ESTA EN -1 (ESTA INICIALIZADO ASI) PERO AL MOMENTO DE INGRESAR DEBERIA CAMBIAR DE ACUERDO AL INDICE DEL USER QUE INGRESO.

AYUDA PORFA

CLASE ATM
CITA
public class ATM {
double [] fondos = new double[3];
private int seleccionado =-1;
Usuario [] clientes = new Usuario [3];

public ATM(){
inicializarFondos();
inicializarUsuarios();
}


public void inicializarUsuarios(){
clientes[0] = new Usuario("111","123");
clientes[1] = new Usuario("222","123");
clientes[2] = new Usuario("333","123");
}

public void inicializarFondos(){
fondos[0]=100000;
fondos[1]=200000;
fondos[2]=300000;
}

public boolean girar(double cantidad){
if(cantidad <= fondos[seleccionado]){
fondos[seleccionado] = fondos[seleccionado] - cantidad;
return true;
}else{
return false;
}
}

public boolean depositar(double cantidad){
fondos[seleccionado] = fondos[seleccionado] + cantidad;
return true;

}

public boolean identificarUsuario(String rut, String password){
for (int i =0; i< clientes.length; i++){
if(clientes[i].validar(rut, password)){
seleccionado = i;

return true;
}
}
return false;
}
public double getMonto(){
double monto;
monto = fondos[seleccionado];
return monto;
}

}



CLASE USUARIO:
CÓDIGO
public class Usuario {
    public String rut;
    public String password;
    
    public Usuario(String rut, String password){
        this.rut = rut;
        this.password = password;
    }
        
    public boolean validar(String rut, String password){
                  
        boolean comp_login = (this.rut.equals(rut));
        boolean comp_pass = (this.password.equals(password));
        boolean comp_ambos = (comp_login && comp_pass);
        return comp_ambos;
    }
}


CLASE IDENTIFICACION:
CITA
CITA
public class Identificacion extends javax.swing.JFrame {
JOptionPane msj;
Menu me;
String rut;
String pass;
int largo;


/** Creates new form Identificacion */
private ATM e = new ATM();

public Identificacion() {
initComponents();
this.setLocationRelativeTo(null);//esto es para que el formulario aparezca en el medio
e.inicializarUsuarios(); //inicializamos los fondos
e.inicializarFondos(); //inicializamos los votant
}

/** 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() {

btnIngresar = new javax.swing.JButton();
btnBorrar = new javax.swing.JButton();
btnCancelar = new javax.swing.JButton();
txtPass = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
jLabel1 = new javax.swing.JLabel();
txtRut = new javax.swing.JTextField();
jLabel4 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("BANCO ESTAFO");

btnIngresar.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
btnIngresar.setText("Ingresar");
btnIngresar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnIngresarActionPerformed(evt);
}
});

btnBorrar.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
btnBorrar.setForeground(new java.awt.Color(255, 51, 51));
btnBorrar.setText("Borrar");
btnBorrar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnBorrarActionPerformed(evt);
}
});

btnCancelar.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
btnCancelar.setText("Cancelar");
btnCancelar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnCancelarActionPerformed(evt);
}
});

txtPass.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txtPassActionPerformed(evt);
}
});
txtPass.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent evt) {
txtPassKeyPressed(evt);
}
});

jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11));
jLabel2.setForeground(new java.awt.Color(255, 0, 0));
jLabel2.setText("PASSWORD:");

jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11));
jLabel1.setForeground(new java.awt.Color(255, 0, 0));
jLabel1.setText("RUT:");

txtRut.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txtRutActionPerformed(evt);
}
});
txtRut.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent evt) {
txtRutKeyPressed(evt);
}
});

jLabel4.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
jLabel4.setText("Ingreso de Datos");

jLabel3.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel3.setText("CAJERO AUTOMATICO");

org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(100, 100, 100)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(30, 30, 30)
.add(jLabel4))
.add(jLabel3)))
.add(layout.createSequentialGroup()
.add(90, 90, 90)
.add(jLabel1)
.add(52, 52, 52)
.add(txtRut, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 90, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.add(layout.createSequentialGroup()
.add(90, 90, 90)
.add(jLabel2)
.add(10, 10, 10)
.add(txtPass, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 90, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(74, Short.MAX_VALUE))
.add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
.addContainerGap(70, Short.MAX_VALUE)
.add(btnIngresar)
.add(18, 18, 18)
.add(btnBorrar)
.add(18, 18, 18)
.add(btnCancelar)
.add(35, 35, 35))
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(10, 10, 10)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(20, 20, 20)
.add(jLabel4))
.add(jLabel3))
.add(3, 3, 3)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jLabel1)
.add(txtRut, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.add(11, 11, 11)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jLabel2)
.add(txtPass, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.add(21, 21, 21)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(btnCancelar)
.add(btnBorrar)
.add(btnIngresar))
.addContainerGap(17, Short.MAX_VALUE))
);

pack();
}// </editor-fold>

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

validarIngreso();// TODO add your handling code here:
}
private void validarIngreso(){
try{
String rut = txtRut.getText();
String pass = txtPass.getText();


//int largo = rut.length();

if(enviarDatos(rut,pass)){
JOptionPane.showMessageDialog(null, "BIENVENIDO A NUESTRO SISTEMA:"+ txtRut.getText(), "INGRESO OK", JOptionPane.INFORMATION_MESSAGE);
presentarPantallaMenu();

}else{
JOptionPane.showMessageDialog(null, "DATOS INCORRECTOS", "ERROR", JOptionPane.INFORMATION_MESSAGE);
estadoInicial();
}
}catch(IOException e){
JOptionPane.showMessageDialog(null, "DATOS INCORRECTOS", "ERROR", JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
}
public boolean enviarDatos(String rut,String pass)throws IOException{
if(e.identificarUsuario(rut, pass)){
return true;
}
return false;
}
private void presentarPantallaMenu(){
me = new Menu(rut, pass);
me.setLocationRelativeTo(null); //centro la nueva pantalla
this.setVisible(false); //oculto la actual


me.setVisible(true); //muestro la pantalla de votaciones
}
private void salir(){
int resp = msj.showConfirmDialog(null, "¿ABANDONAR EL CAJERO?"
,"Confirmacion", JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
if (resp == JOptionPane.YES_OPTION){
System.exit(0);
}else{
estadoInicial();

}
}
private void estadoInicial(){
txtRut.setText("");
txtPass.setText("");
txtRut.requestFocus();
}

private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {
salir();// TODO add your handling code here:
}



private void btnBorrarActionPerformed(java.awt.event.ActionEvent evt) {
Borrar();
}
private void Borrar(){
String borrar = txtPass.getText().trim();
for(int i=0;i < borrar.length();i++){
borrar = borrar.substring(0, borrar.length()-1);
txtPass.setText(borrar);
}
}


private void txtRutKeyPressed(java.awt.event.KeyEvent evt) {
if (evt.getKeyCode() == evt.VK_ENTER){
//presionaron enter después de ingresar el rut de la persona
txtPass.requestFocus();
}
}

private void txtPassKeyPressed(java.awt.event.KeyEvent evt) {
if (evt.getKeyCode() == evt.VK_ENTER){
//presionaron enter después de ingresar el rut de la persona
btnIngresar.requestFocus();
}

}


public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Identificacion().setVisible(true);
}
});
}


¡ CONTENIDO OCULTO BLOQUEADO !

Identifícate o Registrate para desbloquearlo.
::ChileWarez::



CLASE GIRAR:
CITA


¡ CONTENIDO OCULTO BLOQUEADO !

Identifícate o Registrate para desbloquearlo.
::ChileWarez::




CLASE DEPOSITAR:
CITA
CITA
public class Depositar extends javax.swing.JFrame {
JOptionPane msj;
Identificacion Id;
Menu m;
String rut;
String pass;


private ATM e = new ATM();
/** Creates new form Depositar */
public Depositar(String rut, String pass) {
initComponents();
this.rut = rut;
this.pass = pass;
lblMonto.setText("" + e.getMonto());
}

/** 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() {

lblMonto = new javax.swing.JLabel();
txtDeposito = new javax.swing.JTextField();
btnDepositar1 = new javax.swing.JButton();
btnVolver = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
setTitle("DEPOSITAR");

lblMonto.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(255, 0, 0)));

btnDepositar1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
btnDepositar1.setText("DEPOSITAR");
btnDepositar1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnDepositar1ActionPerformed(evt);
}
});

btnVolver.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
btnVolver.setText("VOLVER AL MENU");
btnVolver.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnVolverActionPerformed(evt);
}
});

jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18));
jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel1.setText("DEPOSITAR DINERO");

org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(101, 101, 101)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(40, 40, 40)
.add(btnVolver))
.add(layout.createSequentialGroup()
.add(27, 27, 27)
.add(btnDepositar1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 160, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))))
.add(layout.createSequentialGroup()
.add(137, 137, 137)
.add(txtDeposito, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 140, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.add(layout.createSequentialGroup()
.add(161, 161, 161)
.add(lblMonto, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 90, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.add(layout.createSequentialGroup()
.add(114, 114, 114)
.add(jLabel1)))
.addContainerGap(114, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.addContainerGap()
.add(jLabel1)
.add(8, 8, 8)
.add(lblMonto, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 20, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
.add(txtDeposito, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(19, 19, 19)
.add(btnDepositar1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 30, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
.add(btnVolver)
.addContainerGap(28, Short.MAX_VALUE))
);

pack();
}// </editor-fold>
private void volverIdentificacion(){
Id = new Identificacion();
Id.setLocationRelativeTo(null);
this.setVisible(false);
Id.setVisible(true);
}
private void volverMenu(){
m = new Menu(rut, pass);
m.setLocationRelativeTo(null);
this.setVisible(false);
m.setVisible(true);
}
private void btnVolverActionPerformed(java.awt.event.ActionEvent evt) {
volverMenu();
// TODO add your handling code here:
}

private void btnDepositar1ActionPerformed(java.awt.event.ActionEvent evt) {
accionDepositar();
// TODO add your handling code here:
}


public void accionDepositar(){
double t;
lblMonto.setText("" + e.getMonto());
String monto = txtDeposito.getText();
t = Double.parseDouble(monto);
if(e.depositar(t)){
lblMonto.setText("" + e.getMonto());
JOptionPane.showMessageDialog(null, "DEPOSITO REALIZADO CORRECTAMENTE", "BIEN", JOptionPane.INFORMATION_MESSAGE);
txtDeposito.setText("");
}else{
lblMonto.setText("" + e.getMonto());
JOptionPane.showMessageDialog(null, "NO SE PUDO REALIZAR LA TRANSACCION", "ERROR", JOptionPane.INFORMATION_MESSAGE);
txtDeposito.setText("");
}
}

/**
* @param args the command line arguments
*/


// Variables declaration - do not modify
private javax.swing.JButton btnDepositar1;
private javax.swing.JButton btnVolver;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel lblMonto;
private javax.swing.JTextField txtDeposito;
// End of variables declaration

}
[hide]


SI ALGUIEN LOGRARA AYUDARME ESTARIA MUY AGRADECIDO.[color=#FF0000]
luffymk
podrias subir el codigo fuente, completo, asi podria ayudarte con mas detalle.
Felipin_182
CODIGO EN NETBEANS 6.1
ToroNegro
Cabro es que estas inicializando en todas las clases un objeto ATM, entonces en todos son diferentes, cuando creas los objetos mandalo por parametros en los constructores y asi los asignas y mantienes siempre uno solo, o sino setealo......... ese es tu error los cambie y funca el programa pero tienes q cambiarlo, lo otro eso de hacer visible o no igual es "molesto" que no se cierren nunca............ aaa y lo otro cuanto deposites o gires, debes comprobar que el jtextfield sea distinto a vacio pq sino te arroja error espero que te sirvan los consejos..............


Por ejemplo


me = new Menu(rut, pass,e);

y en menu tienes el constructor

private ATM e;// = new ATM();

/** Creates new form Menu */
public Menu(String rut,String pass, ATM e) {
initComponents();
this.rut = rut;
this.pass = pass;
this.e = e;

}

saluos carita15.gif
Esta es una versión "reducida (lo-fi)" de nuestro contenido. Para ver la versión completa con mas información, formato e imágenes, por favor haz click aquí.