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);
}
}
程序调用了两次方法,是触发了两次导致的吧。
您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~
如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~
ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632