标题:我这个程序是关于 ATM 的!!问题出在转账时,在运行时就是他(收钱)的账户 ...
只看楼主
llt2013
Rank: 2
等 级:论坛游民
帖 子:15
专家分:20
注 册:2013-12-28
结帖率:50%
已结贴  问题点数:20 回复次数:6 
我这个程序是关于 ATM 的!!问题出在转账时,在运行时就是他(收钱)的账户上不能显示转账后的价钱!!求大神,代码有点长,请谅解!!
//ATM.java
import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
import java.nio.channels.SelectableChannel;
import java.sql.*;

public class ATM extends JFrame{
    private JTextArea messageArea;
    private JPanel buttonPanel;
    private JPanel commandPanel;
    private JButton buttonNumber[]=new JButton[10];
    private String buttonName[]={"1","2","3","4","5","6","7","8","9","0"};
    private JButton queryButton;
    private JButton ckButton;
    private JButton withdrawButton;
    private JButton enterButton;
    private JButton exitButton;
    private JButton zhuanButton;
    private JButton alterButton;
    private double balance,x,sum;
    private double accountAmount;
    private String accountNumber="",accountPin="",accountPin1="",accountPin2="",accountPin3="";
    private int enterState=0;
    private String s="";
    private String account;
   
    private Connection myCon;
    private Statement myStatement;
    private ResultSet myResultSet;
   
    public ATM()
    {
        String driverClass = "sun.jdbc.odbc.JdbcOdbcDriver";  
        String url = "jdbc:odbc:account";
         try {
            //①加载驱动程序,必须要安装驱动程序并注册数据源,此处实例的数据源名称为account
            Class.forName(driverClass);
          } catch (ClassNotFoundException e) {
            //驱动程序加载不成功,打印错误信息并退出
            System.out.println("Can not find driver " + driverClass);
            System.exit( -1);
          }

          try {
            //②获得jdbc 连接,参数包括连接数据源,数据源名称,密码
            myCon = DriverManager.getConnection(url, "account","");
           
            //③创建Statement对象,用于将 SQL 语句发送到数据库中,并返回结果。
            myStatement = myCon.createStatement();
         
            } catch (SQLException sqe) {
            sqe.printStackTrace();
          }
  
        CreateUI();
        
    }
   
    private void CreateUI()
    {
        Container contentPane=getContentPane();
        contentPane.setLayout(null);
        messageArea=new JTextArea();
        messageArea.setBounds(20,16,250,80);
        messageArea.setText("欢迎使用ATM,请输入帐号:\n");
        messageArea.setEditable(false);
        contentPane.add(messageArea);
        
        buttonPanel=new JPanel();
        buttonPanel.setBounds(24,110,140,150);
        buttonPanel.setLayout(new GridLayout(4,3,5,6));
        contentPane.add(buttonPanel);
        
        commandPanel=new JPanel();
        commandPanel.setBounds(180,110,100,150);
        commandPanel.setLayout(new GridLayout(7,1,5,1));
        contentPane.add(commandPanel);
        
        for(int i=0;i<10;i++)
        {
            buttonNumber[i]=new JButton(buttonName[i]);
            buttonNumber[i].addActionListener(new buttonNumber());
            buttonPanel.add(buttonNumber[i]);
        }

        
        queryButton=new JButton("查询");
        queryButton.addActionListener(new queryButtonAction());
        commandPanel.add(queryButton);
        
        withdrawButton=new JButton("取款");
        withdrawButton.addActionListener(new withdrawButtonAction());
        commandPanel.add(withdrawButton);
        
        ckButton=new JButton("存款");
        ckButton.addActionListener(new ckButtonAction());
        commandPanel.add(ckButton);
        
        zhuanButton=new JButton("转账");
        zhuanButton.addActionListener(new zhuanButtonAction());
        commandPanel.add(zhuanButton);
        
        alterButton=new JButton("修改密码");
        alterButton.addActionListener(new alterButtonAction());
        commandPanel.add(alterButton);

        enterButton=new JButton("确定");
        enterButton.addActionListener(new enterButtonAction());
        commandPanel.add(enterButton);
        
        exitButton=new JButton("退出");
        exitButton.addActionListener(new exitButtonAction());
        commandPanel.add(exitButton);
        
        
        setTitle("ATM");
        setSize(320,300);
        setVisible(true);
        addWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent e){
                //⑤窗口关闭时,要关闭数据库连接
                try{
                    myStatement.close();
                    myCon.close();
                }catch(SQLException exp){exp.printStackTrace();}
                finally
                {
                    System.exit(0);
                }
            }
        });
    }
    class buttonNumber implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
        
            if(enterState==0)
                {s=s+e.getActionCommand().charAt(0);//e.getActionCommand() 返回的是当前动作指向对象的名称,charAt(0)检名称中的第一个字符
                messageArea.append(e.getActionCommand());}//append():往尾部增加元素
            else if(enterState==1)//输入密码
                {s=s+e.getActionCommand().charAt(0);
                messageArea.append("*");}
            else if(enterState==2)  //enterState=2表示取款状态
                {s=s+e.getActionCommand().charAt(0);
                messageArea.append(e.getActionCommand());}
            else if(enterState==4)  //enterState=4表示存款状态
            {s=s+e.getActionCommand().charAt(0);
            messageArea.append(e.getActionCommand());}
                else if(enterState==5)  //enterState=5表示转账状态
                {s=s+e.getActionCommand().charAt(0);
                messageArea.append(e.getActionCommand());}
                else if(enterState==6)  
                {s=s+e.getActionCommand().charAt(0);
                messageArea.append(e.getActionCommand());}
                else if(enterState==8)  //enterState=8表示修改密码状态
                {s=s+e.getActionCommand().charAt(0);
                messageArea.append("*");}
                else if(enterState==9)
                {s=s+e.getActionCommand().charAt(0);
                messageArea.append("*");}
                else if(enterState==10)  
                {s=s+e.getActionCommand().charAt(0);
                messageArea.append("*");}
        }
    }
   
    class queryButtonAction implements ActionListener//查询按钮
    {
        public void actionPerformed(ActionEvent e)
        {
            messageArea.setText("当前余额为:"+accountAmount);
            messageArea.append("\n\n需要继续服务请按确定,否则请按退出!");
            enterState=3;
        }
    }
   
    class withdrawButtonAction implements ActionListener//取款按钮
    {
        public void actionPerformed(ActionEvent e)
        {
            messageArea.setText("请输入取款金额(应为50或100的整数倍):\n");
            enterState=2;
            s="";            
        }
    }
   
    class ckButtonAction implements ActionListener//存款
    {
        public void actionPerformed(ActionEvent e)
        {
            messageArea.setText("请输入存款金额(应为0到50000之间):\n");
            enterState=4;
            s="";            
        }
    }
    class zhuanButtonAction implements ActionListener//转账按钮
    {
        public void actionPerformed(ActionEvent e)
        {
            messageArea.setText("请输入要转入的帐号:\n");
            enterState=5;
            s="";
        }
    }
    class alterButtonAction implements ActionListener//修改密码按钮
    {
        public void actionPerformed(ActionEvent e)
        {
             messageArea.setText("请输入您原来的密码:\n");
             enterState=8;
            s="";   
        }
    }
    class exitButtonAction implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            enterState=0;
            s="";
            accountAmount=0;
            accountNumber="";
            accountPin="";
            messageArea.setText("欢迎使用ATM,请输入帐号:\n");
        }
    }
    class enterButtonAction implements ActionListener//确定按钮
    {
        public void actionPerformed(ActionEvent e)
        {
            enterState++;
            enterState%=13;
            if(enterState==1)//判断用户名是否存在
            {
                accountNumber=s;
                account=s;
                s="";

                try{
                   // ④得到查询结果
                    myResultSet=myStatement.executeQuery("SELECT * FROM account WHERE accountNumber='"+accountNumber+"'");
                    if(myResultSet.next())
                      messageArea.setText("请输入密码:\n");
                    else{
                          messageArea.setText("帐号输入错误,请重新输入!\n");
                                     enterState=0;
                        }
                    }catch(SQLException exp)
                    {exp.printStackTrace();}
                }
            if(enterState==2)//判断密码是否正确
            {
                accountPin=s;
                s="";
                try{
                // ④得到查询结果
                if(myResultSet.getString("accountPin").compareTo(accountPin)==0)
                {
                    accountAmount=myResultSet.getDouble("accountAmount");
                    messageArea.setText("请选择服务种类\n");
                }
               
                else {messageArea.setText("密码错误,请重新输入\n");
                        enterState=1;}
                }catch(SQLException exp)
                    {
                        exp.printStackTrace();
                        }
            }
            if(enterState==3)//取款
            {
                balance=Double.parseDouble(s);
                s="";
                if(accountAmount>balance)
                {accountAmount-=balance;

                messageArea.setText("\n请等待。。。");
                try{
                    // ④执行更新更新操作
                    myStatement.executeUpdate("UPDATE account SET accountAmount="
                    +accountAmount+" WHERE accountNumber='"+accountNumber+"'");
                messageArea.setText("\n请取走钞票\n继续服务请按确定,退出请按取消!");
                }catch(SQLException exp)
                    {
                        exp.printStackTrace();
                        }
                }

             else {messageArea.setText("对不起,余额不足!请重新输入金额!\n");
                     enterState=2;}   
            }
            if(enterState==4)
            {
                s="";
                messageArea.setText("请选择服务种类\n");
            }
            if(enterState==5)//存款
            {
                balance=Double.parseDouble(s);
                s="";
                if(balance>0||balance<50000)
                {accountAmount+=balance;

                messageArea.setText("\n请等待。。。");
                try{
                    // ④执行更新更新操作
                    myStatement.executeUpdate("UPDATE account SET accountAmount="
                    +accountAmount+" WHERE accountNumber='"+accountNumber+"'");
                messageArea.setText("\n已存入钞票\n继续服务请按确定,退出请按取消!");
                }catch(SQLException exp)
                    {
                        exp.printStackTrace();
                        }
                }

             else {messageArea.setText("对不起!请重新输入金额!\n");
                     enterState=4;}   
            }
            
            if(enterState==6)//判断要转帐的用户是否存在
            {
               
                accountNumber=s;
                //x=SELECT accountAmount FROM account WHERE accountNumber='"+accountNumber+"'";
                s="";
                //messageArea.setText("请选择服务种类\n");
                try{
                       // ④得到查询结果
                        myResultSet=myStatement.executeQuery("SELECT * FROM account WHERE accountNumber='"+accountNumber+"'");
                        
                        if(myResultSet.next())
                          messageArea.setText("请输入要转入的金额:\n");
                        else{
                              messageArea.setText("帐号输入错误,请重新输入!\n");
                            enterState=5;
                            }
                        }catch(SQLException exp)
                        {exp.printStackTrace();}
               
        }
            if(enterState==7)//转账
            {
                balance=Double.parseDouble(s);
                s="";
                try{x=myStatement.executeUpdate("select accountAmount from account where accountNumber='"+account+"'");//这条语句是在求收账的用户的原有金额吗?主要问题应该在这附近
                    //System.out.print(x);
                }catch(SQLException exp)
                {exp.printStackTrace();
                }
                if(accountAmount>balance)
                {
                    accountAmount-=balance;
                    messageArea.setText("\n请等待。。。");
                    //sum=x+balance;
                try{
                    // ④执行更新更新操作
                    myStatement.executeUpdate("UPDATE account SET accountAmount="
                    +accountAmount+" WHERE accountNumber='"+account+"'");
                    myStatement.executeUpdate("UPDATE account SET accountAmount="
                            +x+" WHERE accountNumber='"+accountNumber+"'");
                messageArea.setText("\n已转出!\n继续服务请按确定,退出请按取消!");
                }catch(SQLException exp)
                    {exp.printStackTrace();
                        }}
                else {messageArea.setText("对不起,余额不足!请重新输入金额!\n");
                 enterState=6;}   
            }
            if(enterState==8)
            {
                s="";
                messageArea.setText("请选择服务种类\n");
            }
            if(enterState==9)//修改密码,判断输入的密码是否与原密码相等
            {
                accountPin1=s;
                s="";
                // ④得到查询结果
                if(accountPin.trim().equals(accountPin1.trim()))//myResultSet.getString("accountPin").compareTo(accountPin1)==0)
                {
                    //accountAmount=myResultSet.getDouble("accountAmount");
                    messageArea.setText("请输入新密码\n");
                }
               
                else {messageArea.setText("密码错误,请重新输入\n");
                        enterState=8;}
            }
            if(enterState==10)//输入新密码
            {
                accountPin2=s;
                s="";
                messageArea.setText("请再一次输入密码\n");
            }
            if(enterState==11)//再次输入密码,且判断两次输入的密码是否相同
            {
                accountPin3=s;
                s="";
                try {
                        if (accountPin2.trim().equals(accountPin3.trim())) {
                        myStatement.executeUpdate("UPDATE account SET accountPin="
                                +accountPin3+" WHERE accountNumber='"+account+"'");
                    }
                        else {
                            messageArea.setText("两次输入的密码不一致,请重新输入\n");
                            enterState=10;
                        }}catch (SQLException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                }
            if(enterState==12)
            {
                s="";
                messageArea.setText("请选择服务种类\n");
            }
            }
    }
    public static void main(String args[])
    {
        ATM atm=new ATM();
        
    }
   
}
搜索更多相关主题的帖子: private public import 
2014-06-28 22:12
砖家的谎言
Rank: 12Rank: 12Rank: 12
等 级:禁止访问
威 望:30
帖 子:693
专家分:3898
注 册:2013-12-6
得分:5 
你一个账户转出的同时就要触发另一个,数据库里面的这个事务处理

我不是砖家,要努力成为砖家。
2014-06-28 22:18
hhwz
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:87
帖 子:687
专家分:4502
注 册:2011-5-22
得分:5 
优点 加了注释 缺点 没有进行封装
2014-06-30 14:36
xl881221
Rank: 3Rank: 3
等 级:论坛游侠
威 望:2
帖 子:30
专家分:177
注 册:2014-3-23
得分:5 
改了你两段代码,能解决转账问题,整个程序有很多问题的
if (enterState == 6)// 判断要转帐的用户是否存在
{
    account = s;//把输入的要转入账户的accountNumber放到这个变量里
    s = "";
    // messageArea.setText("请选择服务种类\n");
    try {
        // ④得到查询结果
        myResultSet = myStatement.executeQuery("SELECT * FROM account WHERE accountNumber='"+ account + "'");
        if (myResultSet.next())
            messageArea.setText("请输入要转入的金额:\n");
        else {
            messageArea.setText("帐号输入错误,请重新输入!\n");
            enterState = 5;
        }
    } catch (SQLException exp) {
        exp.printStackTrace();
    }
}
if (enterState == 7)// 转账
{
    balance = Double.parseDouble(s);//转账金额
    s = "";
    if (accountAmount > balance) {
        accountAmount-=balance;//程序里的登录账户的accountAmount减去banlance
        messageArea.setText("\n请等待。。。");
        try {
            // ④执行更新更新操作
            myStatement.executeUpdate("UPDATE account SET accountAmount=accountAmount-"+balance+"where accountNumber="+accountNumber);数据库中转账帐号的accountAmount减balance
            myStatement.executeUpdate("UPDATE account SET accountAmount=accountAmount+"+balance+"where accountNumber="+account);数据库中转入帐号的accountAmount加balance

            messageArea.setText("\n已转出!\n继续服务请按确定,退出请按取消!");
        } catch (SQLException exp) {
            exp.printStackTrace();
        }
    } else {
        messageArea.setText("对不起,余额不足!请重新输入金额!\n");
        enterState = 6;
    }
}
2014-07-02 23:15
邢东宇
Rank: 1
来 自:北京 昌平
等 级:新手上路
帖 子:1
专家分:0
注 册:2014-7-24
得分:0 
大神
2014-07-24 19:19
darkness_jh
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2014-7-31
得分:0 
Transaction
2014-07-31 22:54



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-433456-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.128148 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved