&QfYang& 2021-06-10 13:16 采纳率: 66.7%
浏览 41
已结题

Java atm实现

大神们帮我看看,程序本身没有错误,但是运行时第一次取款,结果正在,第二次点取款,比如说取200,正常来说是总金额减二百,但是有一个bug就是会减两次200,百思不得其解,,请问是什么原因???

public class UserInterface {
    static FileReader fr;// FileReader类
    static FileWriter fw;// FileWriter类
    static SimpleDateFormat time;
    static Date date;
  
    // 取款界面组件
    static JDialog diag_withdrawMoney = new JDialog(mainJFrame_3);
    static JLabel lb_withdrawMoney = new JLabel();
    static JTextField tf_withdrawMoney = new JTextField(20);
    static JButton bt_determine_withdrawMoney = new JButton("确定");
    static JButton bt_return_withdrawMoney = new JButton("返回");
    // 存款界面组件
    static JDialog diag_deposit = new JDialog(mainJFrame_3);
    static JLabel lb_deposit = new JLabel();
    static JTextField tf_deposit = new JTextField(20);
    static JButton bt_determine_deposit = new JButton("确定");
    static JButton bt_return_deposit = new JButton("返回");

    public static void main(String[] args) {
    
        // 取款界面
        container.add(bt_withdrawMoney);
        bt_withdrawMoney.setBounds(0, 300, 200, 50);
        bt_withdrawMoney.setFont(new Font("黑体", Font.BOLD, 20));
        bt_withdrawMoney.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JButton bt = (JButton) e.getSource();
                if (bt == bt_withdrawMoney) {
                    diag_withdrawMoney.setTitle("取款");
                    diag_withdrawMoney.setSize(400, 300);
                    diag_withdrawMoney.setLayout(new FlowLayout(FlowLayout.CENTER, 100, 50));
                    diag_withdrawMoney.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
                    diag_withdrawMoney.add(lb_withdrawMoney);
                    lb_withdrawMoney.setText("您的余额为 " + UserTest.currentUserAccount.balance + " 元");
                    lb_withdrawMoney.setFont(new Font("楷体", Font.BOLD, 20));
                    diag_withdrawMoney.add(tf_withdrawMoney);
                    tf_withdrawMoney.setText("请输入取款金额");
                    tf_withdrawMoney.setFont(new Font("楷体", Font.BOLD, 20));
                    diag_withdrawMoney.add(bt_determine_withdrawMoney);
                    bt_determine_withdrawMoney.setFont(new Font("黑体", Font.BOLD, 15));
                    bt_determine_withdrawMoney.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                            JButton bt = (JButton) e.getSource();
                            if (bt == bt_determine_withdrawMoney) {
                                // 每次取款金额为100的倍数
                                if (Float.parseFloat(tf_withdrawMoney.getText()) % 100 != 0) {
                                    diag_withdrawMoney.dispose();
                                    diag_message.setLocationRelativeTo(null);
                                    diag_message.setVisible(true);
                                    lb_message.setText("请注意:每次取款金额应为100的倍数,请检查后再试!");
                                    lb_message.setFont(new Font("楷体", Font.BOLD, 20));
                                }
                                // 每次取款总额不超过5000元
                                else if (Float.parseFloat(tf_withdrawMoney.getText()) > 5000.00) {
                                    diag_withdrawMoney.dispose();
                                    diag_message.setLocationRelativeTo(null);
                                    diag_message.setVisible(true);
                                    lb_message.setText("请注意:每次取款总额不得超过 5000.00 元,请检查后再试!");
                                    lb_message.setFont(new Font("楷体", Font.BOLD, 20));
                                }
                                // 支取金额不允许透支
                                else if (Float
                                        .parseFloat(tf_withdrawMoney.getText()) > UserTest.currentUserAccount.balance) {
                                    diag_withdrawMoney.dispose();
                                    diag_message.setLocationRelativeTo(null);
                                    diag_message.setVisible(true);
                                    lb_message.setText("请注意:支取金额不允许透支,请检查后再试!");
                                    lb_message.setFont(new Font("楷体", Font.BOLD, 20));
                                }
                                // 非正数
                                else if (Float.parseFloat(tf_withdrawMoney.getText()) <= 0) {
                                    diag_withdrawMoney.dispose();
                                    diag_message.setLocationRelativeTo(null);
                                    diag_message.setVisible(true);
                                    lb_message.setText("请注意:您的输入有误,请检查后再试!");
                                    lb_message.setFont(new Font("楷体", Font.BOLD, 20));
                                }
                                // 成功取款
                                else {
                                    time = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
                                    date = new Date();
                                    UserTest.currentUserAccount.balance = UserTest.currentUserAccount.balance-Float.parseFloat(tf_withdrawMoney.getText());
                                    // 记录操作
                                    try {
                                        fw = new FileWriter(UserTest.currentUserAccount.userAccount + ".txt", true);
                                    } catch (IOException e1) {
                                        e1.printStackTrace();
                                    }
                                    try {// "时 间\t\t\t\t操作\t\t余额\t\t备注\r\n"
                                        fw.write(time.format(date) + "\t\t取款"
                                                + Float.parseFloat(tf_withdrawMoney.getText()) + "元\t\t"
                                                + UserTest.currentUserAccount.balance + "元\t\t无\r\n");
                                    } catch (NumberFormatException | IOException e1) {
                                        e1.printStackTrace();
                                    }
                                    try {
                                        fw.flush();
                                    } catch (IOException e1) {
                                        e1.printStackTrace();
                                    }
                                    try {
                                        fw.close();
                                    } catch (IOException e1) {
                                        e1.printStackTrace();
                                    }
                                    try {
                                        fw = new FileWriter("historyOfAll.txt", true);
                                    } catch (IOException e1) {
                                        e1.printStackTrace();
                                    }
                                    try {
                                        fw.write(UserTest.currentUserAccount.userAccount + "\t\t" + time.format(date)
                                                + "\t\t取款" + Float.parseFloat(tf_withdrawMoney.getText())
                                                + "元\t\t无\t\t其余额" + UserTest.currentUserAccount.balance + "元\r\n");
                                    } catch (IOException e1) {
                                        e1.printStackTrace();
                                    }
                                    try {
                                        fw.flush();
                                    } catch (IOException e1) {
                                        e1.printStackTrace();
                                    }
                                    try {
                                        fw.close();
                                    } catch (IOException e1) {
                                        e1.printStackTrace();
                                    }
                                    try {
                                        fr = new FileReader("administratorFile.txt");
                                    } catch (FileNotFoundException e1) {
                                        e1.printStackTrace();
                                    }
                                    BufferedReader br = new BufferedReader(fr);
                                    String temp = "";
                                    String[] tempString = new String[5];
                                    String A = null;
                                    String B = null;
                                    String C = null;
                                    try {
                                        while ((temp = br.readLine()) != null) {
                                            tempString = temp.split("\\s+");
                                            A = tempString[0];
                                            B = tempString[1];
                                            C = tempString[2];
                                        }
                                    } catch (IOException e1) {
                                        e1.printStackTrace();
                                    }
                                    float balance = Float.parseFloat(C) - Float.parseFloat(tf_withdrawMoney.getText());
                                    try {
                                        fw = new FileWriter("administratorFile.txt");
                                    } catch (IOException e1) {
                                        e1.printStackTrace();
                                    }
                                    StringBuilder sb = new StringBuilder();
                                    sb.append(A + " " + B + " " + balance + "\r\n");
                                    try {
                                        fw.write(sb.toString());
                                    } catch (IOException e1) {
                                        e1.printStackTrace();
                                    }
                                    try {
                                        fw.flush();
                                    } catch (IOException e1) {
                                        e1.printStackTrace();
                                    }
                                    try {
                                        fw.close();
                                    } catch (IOException e1) {
                                        e1.printStackTrace();
                                    }
                                    try {
                                        fw = new FileWriter("recentFund.txt", true);
                                    } catch (IOException e1) {
                                        e1.printStackTrace();
                                    }
                                    try {
                                        fw.write(time.format(date) + "\t\t支出"
                                                + Float.parseFloat(tf_withdrawMoney.getText()) + "元\t\t至"
                                                + UserTest.currentUserAccount.userAccount + "\t\t本机余额" + balance
                                                + "元\r\n");
                                    } catch (NumberFormatException | IOException e1) {
                                        e1.printStackTrace();
                                    }
                                    try {
                                        fw.flush();
                                    } catch (IOException e1) {
                                        e1.printStackTrace();
                                    }
                                    try {
                                        fw.close();
                                    } catch (IOException e1) {
                                        e1.printStackTrace();
                                    }
                                    // 更新UserFile
                                    try {
                                        UserTest.userArrayListWrite();
                                    } catch (Exception e1) {
                                        e1.printStackTrace();
                                    }
                                    diag_withdrawMoney.dispose();
                                    diag_message.setLocationRelativeTo(null);
                                    diag_message.setVisible(true);
                                    lb_message.setText("请注意:取款成功!");
                                    lb_message.setFont(new Font("楷体", Font.BOLD, 20));
                                }
                            }
                        }
                    });
                    diag_withdrawMoney.add(bt_return_withdrawMoney);
                    bt_return_withdrawMoney.setFont(new Font("黑体", Font.BOLD, 15));
                    bt_return_withdrawMoney.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                            JButton bt = (JButton) e.getSource();
                            if (bt == bt_return_withdrawMoney) {
                                diag_withdrawMoney.dispose();
                            }
                        }
                    });
                    diag_withdrawMoney.setLocationRelativeTo(null);
                    diag_withdrawMoney.setVisible(true);
                }
            }
        });
      
        // 用户界面
        mainJFrame_3.setVisible(true);
    }
}
 

 

  • 写回答

2条回答 默认 最新

  • CSDN专家-sinJack 2021-06-10 13:41
    关注

    程序调用了两次方法,是触发了两次导致的吧。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 8月21日
  • 已采纳回答 8月13日

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序