⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 atm.java

📁 这是一个ATM的程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
// Tutorial 26: ATM.java
// ATM application allows users to access an account, 
// view the balance and withdraw money from the account.
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.text.*;
import javax.swing.*;
import javax.swing.event.*;

public class ATM extends JFrame
{
   // JTextArea to display message
   private JTextArea messageJTextArea;

   // JTextField to enter PIN or withdrawal amount
   private JTextField numberJTextField;

   // JPanel for number JButtons
   private JPanel buttonsJPanel;

   // JButtons for input of PIN or withdrawal amount
   private JButton oneJButton;
   private JButton twoJButton;
   private JButton threeJButton;
   private JButton fourJButton;
   private JButton fiveJButton;
   private JButton sixJButton;
   private JButton sevenJButton;
   private JButton eightJButton;
   private JButton nineJButton;
   private JButton zeroJButton;

   // JButton to submit PIN or withdrawal amount
   private JButton enterJButton;

   // JButton to view balance
   private JButton balanceJButton;

   // JButton to withdraw from account
   private JButton withdrawJButton;

   // JButton to close the transaction
   private JButton doneJButton;

   // JPanel to get account numbers
   private JPanel accountNumberJPanel;

   // JLabel and JComboBox for account numbers
   private JLabel accountNumberJLabel;
   private JComboBox accountNumberJComboBox;

   // constants for user action
   private final static int ENTER_PIN = 1;
   private final static int WITHDRAWAL = 2;

   // instance variables used to store PIN and
   // firstName from database
   private String pin, firstName;

   // instance variable used to distinguish user action
   private int action;

   // instance variables used to store user selected account number
   // and PIN
   private String userAccountNumber, userPIN;

   // instance variable used to store account balance
   private double balance;

   // instance variables used to manipulate database
   private Connection myConnection;
   private Statement myStatement;
   private ResultSet myResultSet;

   // constructor
   public ATM( String databaseDriver, String databaseURL )
   {
      // establish connection to database
      try
      {
         // load Cloudscape driver
         Class.forName( databaseDriver );

         // connect to database
         myConnection = 
            DriverManager.getConnection( databaseURL );

         // create Statement for executing SQL
         myStatement = myConnection.createStatement();
      }
      catch ( SQLException exception )
      {
         exception.printStackTrace();
      }
      catch ( ClassNotFoundException exception )
      {
         exception.printStackTrace();
      }

      createUserInterface(); // set up GUI
      
   } // end constructor

   // create and position GUI components; register event handler
   private void createUserInterface()
   {
      // get content pane for attaching GUI components
      Container contentPane = getContentPane();

      // enable explicit positioning of GUI components
      contentPane.setLayout( null );

      // set up messageJTextArea  
      messageJTextArea = new JTextArea();
      messageJTextArea.setBounds( 40, 16, 288, 88 );
      messageJTextArea.setText(
         "Please select your account number." );
      messageJTextArea.setBorder( 
         BorderFactory.createLoweredBevelBorder() );
      messageJTextArea.setEditable( false );
      contentPane.add( messageJTextArea );

      // set up numberJTextField 
      numberJTextField = new JTextField();
      numberJTextField.setBounds( 110, 120, 128, 21 );
      numberJTextField.setBorder( 
         BorderFactory.createLoweredBevelBorder() );
      numberJTextField.setEditable( false );
      contentPane.add( numberJTextField );
      
      // set up buttonsJPanel
      buttonsJPanel = new JPanel();
      buttonsJPanel.setBounds( 44, 160, 276, 150 );
      buttonsJPanel.setBorder( BorderFactory.createEtchedBorder() );
      buttonsJPanel.setLayout( null );
      contentPane.add( buttonsJPanel );

      // set up oneJButton
      oneJButton = new JButton();
      oneJButton.setBounds( 53, 28, 24, 24 );
      oneJButton.setText( "1" );
      oneJButton.setBorder( 
         BorderFactory.createRaisedBevelBorder() );
      buttonsJPanel.add( oneJButton );
      oneJButton.addActionListener(

         new ActionListener() // anonymous inner class
         {
            // event handler called when oneJButton is clicked
            public void actionPerformed( ActionEvent event )
            {
               oneJButtonActionPerformed( event );
            }

         } // end anonymous inner class

      ); // end call to addActionListener

      // set up twoJButton
      twoJButton = new JButton();
      twoJButton.setBounds( 77, 28, 24, 24 );
      twoJButton.setText( "2" );
      twoJButton.setBorder( 
         BorderFactory.createRaisedBevelBorder() );
      buttonsJPanel.add( twoJButton );
      twoJButton.addActionListener(

         new ActionListener() // anonymous inner class
         {
            // event handler called when twoJButton is clicked
            public void actionPerformed( ActionEvent event )
            {
               twoJButtonActionPerformed( event );
            }

         } // end anonymous inner class

      ); // end call to addActionListener

      // set up threeJButton
      threeJButton = new JButton();
      threeJButton.setBounds( 101, 28, 24, 24 );
      threeJButton.setText( "3" );
      threeJButton.setBorder( 
         BorderFactory.createRaisedBevelBorder() );
      buttonsJPanel.add( threeJButton );
      threeJButton.addActionListener(

         new ActionListener() // anonymous inner class
         {
            // event handler called when threeJButton is clicked
            public void actionPerformed( ActionEvent event )
            {
               threeJButtonActionPerformed( event );
            }

         } // end anonymous inner class

      ); // end call to addActionListener

      // set up fourJButton
      fourJButton = new JButton();
      fourJButton.setBounds( 53, 52, 24, 24 );
      fourJButton.setText( "4" );
      fourJButton.setBorder( 
         BorderFactory.createRaisedBevelBorder() );
      buttonsJPanel.add( fourJButton );
      fourJButton.addActionListener(

         new ActionListener() // anonymous inner class
         {
            // event handler called when fourJButton is clicked
            public void actionPerformed( ActionEvent event )
            {
               fourJButtonActionPerformed( event );
            }

         } // end anonymous inner class

      ); // end call to addActionListener
      
      // set up fiveJButton
      fiveJButton = new JButton();
      fiveJButton.setBounds( 77, 52, 24, 24 );
      fiveJButton.setText( "5" );
      fiveJButton.setBorder( 
         BorderFactory.createRaisedBevelBorder() );
      buttonsJPanel.add( fiveJButton );
      fiveJButton.addActionListener(

         new ActionListener() // anonymous inner class
         {
            // event handler called when fiveJButton is clicked
            public void actionPerformed( ActionEvent event )
            {
               fiveJButtonActionPerformed( event );
            }

         } // end anonymous inner class

      ); // end call to addActionListener

      // set up sixJButton
      sixJButton = new JButton();
      sixJButton.setBounds( 101, 52, 24, 24 );
      sixJButton.setText( "6" );
      sixJButton.setBorder( 
         BorderFactory.createRaisedBevelBorder() );
      buttonsJPanel.add( sixJButton );
      sixJButton.addActionListener(

         new ActionListener() // anonymous inner class
         {
            // event handler called when sixJButton is clicked
            public void actionPerformed( ActionEvent event )
            {
               sixJButtonActionPerformed( event );
            }

         } // end anonymous inner class

      ); // end call to addActionListener

      // set up sevenJButton
      sevenJButton = new JButton();
      sevenJButton.setBounds( 53, 76, 24, 24 );
      sevenJButton.setText( "7" );
      sevenJButton.setBorder( 
         BorderFactory.createRaisedBevelBorder() );
      buttonsJPanel.add( sevenJButton );
      sevenJButton.addActionListener(

         new ActionListener() // anonymous inner class
         {
            // event handler called when sevenJButton is clicked
            public void actionPerformed( ActionEvent event )
            {
               sevenJButtonActionPerformed( event );
            }

         } // end anonymous inner class

      ); // end call to addActionListener

      // set up eightJButton
      eightJButton = new JButton();
      eightJButton.setBounds( 77, 76, 24, 24 );
      eightJButton.setText( "8" );
      eightJButton.setBorder( 
         BorderFactory.createRaisedBevelBorder() );
      buttonsJPanel.add( eightJButton );
      eightJButton.addActionListener(

         new ActionListener() // anonymous inner class
         {
            // event handler called when eightJButton is clicked
            public void actionPerformed( ActionEvent event )
            {
               eightJButtonActionPerformed( event );
            }

         } // end anonymous inner class

      ); // end call to addActionListener

      // set up nineJButton
      nineJButton = new JButton();
      nineJButton.setBounds( 101, 76, 24, 24 );
      nineJButton.setText( "9" );
      nineJButton.setBorder( 
         BorderFactory.createRaisedBevelBorder() );
      buttonsJPanel.add( nineJButton );
      nineJButton.addActionListener(

         new ActionListener() // anonymous inner class
         {
            // event handler called when nineJButton is clicked
            public void actionPerformed( ActionEvent event )
            {
               nineJButtonActionPerformed( event );
            }

         } // end anonymous inner class

      ); // end call to addActionListener

      // set up zeroJButton
      zeroJButton = new JButton();
      zeroJButton.setBounds( 77, 100, 24, 24 );
      zeroJButton.setText( "0" );
      zeroJButton.setBorder(
         BorderFactory.createRaisedBevelBorder() );
      buttonsJPanel.add( zeroJButton );
      zeroJButton.addActionListener(

         new ActionListener() // anonymous inner class
         {
            // event handler called when zeroJButton is clicked
            public void actionPerformed( ActionEvent event )
            {
               zeroJButtonActionPerformed( event );
            }

         } // end anonymous inner class

      ); // end call to addActionListener

      disableKeyPad(); // disable numeric JButtons

      // set up enterJButton
      enterJButton = new JButton();
      enterJButton.setBounds( 149, 17, 72, 24 );
      enterJButton.setText( "Enter" );
      enterJButton.setBorder( 
         BorderFactory.createRaisedBevelBorder() );
      buttonsJPanel.add( enterJButton );
      enterJButton.setEnabled( false );
      enterJButton.addActionListener(

         new ActionListener() // anonymous inner class
         {
            // event handler called when enterJButton is clicked
            public void actionPerformed( ActionEvent event )
            {
               enterJButtonActionPerformed( event );
            }

         } // end anonymous inner class

      ); // end call to addActionListener

      // set up balanceJButton
      balanceJButton = new JButton();
      balanceJButton.setBounds( 149, 49, 72, 24 );
      balanceJButton.setText( "Balance" );
      balanceJButton.setBorder( 
         BorderFactory.createRaisedBevelBorder() );
      buttonsJPanel.add( balanceJButton );
      balanceJButton.setEnabled( false );
      balanceJButton.addActionListener(

         new ActionListener() // anonymous inner class
         {
            // event handler called when balanceJButton is clicked
            public void actionPerformed( ActionEvent event )
            {
               balanceJButtonActionPerformed( event );
            }

         } // end anonymous inner class

      ); // end call to addActionListener

      // set up withdrawJButton
      withdrawJButton = new JButton();
      withdrawJButton.setBounds( 149, 81, 72, 24 );
      withdrawJButton.setText( "Withdraw" );
      withdrawJButton.setBorder(
         BorderFactory.createRaisedBevelBorder() );
      withdrawJButton.setEnabled( false );
      buttonsJPanel.add( withdrawJButton );
      withdrawJButton.addActionListener(

         new ActionListener() // anonymous inner class
         {
            // event handler called when withdrawJButton is clicked
            public void actionPerformed( ActionEvent event )
            {
               withdrawJButtonActionPerformed( event );
            }

         } // end anonymous inner class

      ); // end call to addActionListener

      // set up doneJButton
      doneJButton = new JButton();
      doneJButton.setBounds( 149, 113, 72, 24 );
      doneJButton.setText( "Done" );
      doneJButton.setBorder( 
         BorderFactory.createRaisedBevelBorder() );
      doneJButton.setEnabled( false );
      buttonsJPanel.add( doneJButton );
      doneJButton.addActionListener(

         new ActionListener() // anonymous inner class
         {
            // event handler called when doneJButton is clicked
            public void actionPerformed( ActionEvent event )
            {
               doneJButtonActionPerformed( event );
            }

         } // end anonymous inner class

      ); // end call to addActionListener

      // set up accountNumberJPanel
      accountNumberJPanel = new JPanel();
      accountNumberJPanel.setBounds( 44, 320, 276, 48 );
      accountNumberJPanel.setBorder( 
         BorderFactory.createEtchedBorder() );
      accountNumberJPanel.setLayout( null );
      contentPane.add( accountNumberJPanel );

      // set up accountNumberJLabel
      accountNumberJLabel = new JLabel();
      accountNumberJLabel.setBounds( 25, 15, 100, 20 );
      accountNumberJLabel.setText( "Account Number:" );
      accountNumberJPanel.add( accountNumberJLabel );

      // set up accountNumberJComboBox
      accountNumberJComboBox = new JComboBox();
      accountNumberJComboBox.setBounds( 150, 12, 96, 25 );
      accountNumberJComboBox.addItem( "" );
      accountNumberJComboBox.setSelectedIndex( 0 );
      accountNumberJPanel.add( accountNumberJComboBox );
      accountNumberJComboBox.addItemListener(

         new ItemListener() // anonymous inner class
         {
            // event handler called when account number is chosen
            public void itemStateChanged( ItemEvent event )
            {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -