lunes, 19 de marzo de 2012

Parte 3: Simulando la Maquina de Recargas del Mexibus

//Clase Recarga

import java.awt.*;
import java.awt.event.*;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.swing.*;


public class Recarga extends JFrame{
ImageIcon img[]={new ImageIcon("Cancelar.jpg"),new ImageIcon("Aceptar.jpg"),new ImageIcon("Signo.jpg"),new ImageIcon("Dinero.jpg")};
JButton btnCancelar=new JButton(img[0]);
JButton btnContinuar=new JButton(img[1]);

JLabel lblRecarga=new JLabel("Recarga Max.");
JLabel lblRec=new JLabel("$500.00");

JLabel lblCantidad=new JLabel("Cantidad");
JLabel lblpesos=new JLabel("$");
JLabel lblpes=new JLabel("0");

JLabel lblDinero=new JLabel("Introdusca Dinero");
JTextField txtCantidad=new JTextField();
JButton btnAceptar=new JButton("Aceptar");

JLabel lblBuscar=new JLabel("Clave");
JTextField txtBuscar=new JTextField();
JButton btnBuscar=new JButton("Buscar");

JLabel lblSaldo=new JLabel("Saldo");
JLabel lblTotal=new JLabel("Total");


public Recarga(){
Panel p=new Panel();
setTitle("Recargar Targeta");
setSize(800,650);

p.setLayout(null);

lblRecarga.setFont(new Font("Arial", Font.BOLD,30));
p.add(lblRecarga); lblRecarga.setBounds(20,100,200,50);
lblRec.setFont(new Font("Arial", Font.BOLD,20));
p.add(lblRec); lblRec.setBounds(50,140,100,50);
p.add(btnCancelar); btnCancelar.setBounds(50,500,100,100);
p.add(btnContinuar); btnContinuar.setBounds(500,500,100,100);

lblCantidad.setFont(new Font("Arial", Font.BOLD,40));
p.add(lblCantidad); lblCantidad.setBounds(350,140,250,50);
lblpesos.setFont(new Font("Arial", Font.BOLD,40));
p.add(lblpesos); lblpesos.setBounds(350,190,250,50);
lblpes.setFont(new Font("Arial", Font.BOLD,40));
p.add(lblpes); lblpes.setBounds(380,190,250,50);

p.add(lblDinero); lblDinero.setBounds(650,10,150,30);
p.add(txtCantidad); txtCantidad.setBounds(650,50,50,30);
p.add(btnAceptar); btnAceptar.setBounds(650,90,100,30);

p.add(lblBuscar); lblBuscar.setBounds(650,170,150,30);
p.add(txtBuscar); txtBuscar.setBounds(650,200,50,30);
p.add(btnBuscar); btnBuscar.setBounds(650,240,100,30);

p.add(lblSaldo); //lblSaldo.setBounds(100,10,100,100);

p.add(lblTotal); //lblTotal.setBounds(190,10,100,100);

Container c=getContentPane();
c.add(p);

btnContinuar.addActionListener(new ActionListener (){
public void actionPerformed(ActionEvent pes){
Procesando p=new Procesando(lblTotal.getText());
p.setVisible(true);
dispose();
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
      Connection conn = DriverManager.getConnection("jdbc:odbc:Database"," "," ");
      String query = "update Tabla1 set Saldo=? where Clave=? ";
      CallableStatement csta = conn.prepareCall(query);

     
      csta.setInt(1, Integer.parseInt(lblTotal.getText()));
   
      csta.setInt(2, Integer.parseInt(txtBuscar.getText()));

      int r = csta.executeUpdate();
     
      //if(r > 0)
         // JOptionPane.showMessageDialog(null, "Se Actualizo correctamente");
     // else
      //    JOptionPane.showMessageDialog(null, "No se Actualizo");
      }
      catch(Exception t){
      JOptionPane.showMessageDialog(null,"Error" + t);
      }
}
});

btnCancelar.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent can){
Sistema s=new Sistema();
s.setVisible(true);
dispose();

}
});

btnAceptar.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent pes){
int Cantidad = Integer.parseInt(txtCantidad.getText());
int Saldo=Integer.parseInt(lblSaldo.getText());
int res=Cantidad + Saldo;

lblTotal.setText(String.valueOf(res));
lblpes.setText(String.valueOf(Cantidad));
}
});

btnBuscar.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent b){
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
       Connection conn = DriverManager.getConnection("jdbc:odbc:Database"," "," ");
       String query = "select * from Tabla1 Where Clave = ?";
       CallableStatement csta = conn.prepareCall(query);
       csta.setInt(1, Integer.parseInt(txtBuscar.getText()));
       ResultSet rs = csta.executeQuery();
       if(rs.next())
       {
           lblSaldo.setText(rs.getString(2));
       }
      }
      catch(Exception r){
      JOptionPane.showMessageDialog(null,"Error" + r);
      }

}
});

}

}

//Clase Saldo

import java.awt.*;
import java.awt.event.*;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;

import javax.swing.*;


public class Saldo extends JFrame{
ImageIcon img[]={new ImageIcon("Cancelar.jpg"),new ImageIcon("Aceptar.jpg")};
JButton btnCancelar=new JButton(img[0]);
JButton btnContinuar=new JButton(img[1]);
JLabel lblCantidad=new JLabel("Saldo Actual");
JLabel lblpesos=new JLabel("$");
JLabel lblpes=new JLabel("0.00");
JLabel lblBuscar=new JLabel("Clave");
JTextField txtBuscar=new JTextField();
JButton btnBuscar=new JButton("Buscar");
public Saldo(){
Panel p=new Panel();
setTitle("Saldo");
setSize(800,650);
p.setLayout(null);

p.add(btnCancelar); btnCancelar.setBounds(50,500,100,100);
p.add(btnContinuar); btnContinuar.setBounds(500,500,100,100);
lblCantidad.setFont(new Font("Arial", Font.BOLD,40));
p.add(lblCantidad); lblCantidad.setBounds(300,140,250,50);
lblpesos.setFont(new Font("Arial", Font.BOLD,40));
p.add(lblpesos); lblpesos.setBounds(350,190,250,50);
lblpes.setFont(new Font("Arial", Font.BOLD,40));
p.add(lblpes); lblpes.setBounds(380,190,250,50);
p.add(lblBuscar); lblBuscar.setBounds(650,170,150,30);
p.add(txtBuscar); txtBuscar.setBounds(650,200,50,30);
p.add(btnBuscar); btnBuscar.setBounds(650,240,100,30);
Container c=getContentPane();
c.add(p);
btnCancelar.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent can){
Sistema s=new Sistema();
s.setVisible(true);
dispose();
}
});
btnContinuar.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent can){
Sistema s=new Sistema();
s.setVisible(true);
dispose();
}
});
btnBuscar.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent b){
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
       Connection conn = DriverManager.getConnection("jdbc:odbc:Database"," "," ");
       String query = "select * from Tabla1 Where Clave = ?";
       CallableStatement csta = conn.prepareCall(query);
       csta.setInt(1, Integer.parseInt(txtBuscar.getText()));
       ResultSet rs = csta.executeQuery();
       if(rs.next())
       {
           lblpes.setText(rs.getString(2));
       }
      }
      catch(Exception r){
      JOptionPane.showMessageDialog(null,"Error" + r);
      }
}
});
}

}

No hay comentarios:

Publicar un comentario