用Java的GUI编写一个程序,采用图形用户界面,模拟银行自动取款机的工作流程。
时间: 2024-04-03 11:31:17 浏览: 27
好的,我可以为您提供一份Java GUI程序示例,模拟银行自动取款机的工作流程。以下是示例代码:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class BankATM extends JFrame implements ActionListener {
private JLabel cardLabel, passwordLabel, balanceLabel;
private JTextField cardText, passwordText, balanceText;
private JButton loginBtn, queryBtn, withdrawBtn, depositBtn, changePwdBtn;
private String card = "888888"; // 银行卡号
private String password = "123456"; // 密码
private double balance = 50000.0; // 余额
public BankATM() {
setTitle("银行自动取款机");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
// 创建面板并设置布局
JPanel panel = new JPanel(new GridLayout(6, 2, 10, 10));
cardLabel = new JLabel("银行卡号:");
cardText = new JTextField();
passwordLabel = new JLabel("密 码:");
passwordText = new JTextField();
loginBtn = new JButton("登录");
loginBtn.addActionListener(this);
queryBtn = new JButton("查询余额");
queryBtn.addActionListener(this);
withdrawBtn = new JButton("ATM取款");
withdrawBtn.addActionListener(this);
depositBtn = new JButton("ATM存款");
depositBtn.addActionListener(this);
changePwdBtn = new JButton("修改密码");
changePwdBtn.addActionListener(this);
balanceLabel = new JLabel("余 额:");
balanceText = new JTextField();
balanceText.setEditable(false);
// 添加组件到面板
panel.add(cardLabel);
panel.add(cardText);
panel.add(passwordLabel);
panel.add(passwordText);
panel.add(loginBtn);
panel.add(queryBtn);
panel.add(withdrawBtn);
panel.add(depositBtn);
panel.add(changePwdBtn);
panel.add(balanceLabel);
panel.add(balanceText);
// 添加面板到窗口
add(panel);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == loginBtn) {
// 登录
String inputCard = cardText.getText();
String inputPwd = passwordText.getText();
if (card.equals(inputCard) && password.equals(inputPwd)) {
JOptionPane.showMessageDialog(this, "登录成功!");
} else {
JOptionPane.showMessageDialog(this, "卡号或密码错误,请重新输入!");
}
} else if (e.getSource() == queryBtn) {
// 查询余额
JOptionPane.showMessageDialog(this, "当前余额为:" + balance);
balanceText.setText(String.valueOf(balance));
} else if (e.getSource() == withdrawBtn) {
// ATM取款
String input = JOptionPane.showInputDialog(this, "请输入取款金额(100的倍数,不超过5000元):");
int amount = Integer.parseInt(input);
if (amount % 100 != 0 || amount > 5000 || amount > balance) {
JOptionPane.showMessageDialog(this, "取款失败!");
} else {
balance -= amount;
JOptionPane.showMessageDialog(this, "取款成功!");
balanceText.setText(String.valueOf(balance));
}
} else if (e.getSource() == depositBtn) {
// ATM存款
String input = JOptionPane.showInputDialog(this, "请输入存款金额:");
double amount = Double.parseDouble(input);
if (amount < 0) {
JOptionPane.showMessageDialog(this, "存款失败!");
} else {
balance += amount;
JOptionPane.showMessageDialog(this, "存款成功!");
balanceText.setText(String.valueOf(balance));
}
} else if (e.getSource() == changePwdBtn) {
// 修改密码
String inputOldPwd = JOptionPane.showInputDialog(this, "请输入旧密码:");
if (!inputOldPwd.equals(password)) {
JOptionPane.showMessageDialog(this, "旧密码错误,请重新输入!");
} else {
String inputNewPwd1 = JOptionPane.showInputDialog(this, "请输入新密码:");
String inputNewPwd2 = JOptionPane.showInputDialog(this, "请再次输入新密码:");
if (!inputNewPwd1.equals(inputNewPwd2)) {
JOptionPane.showMessageDialog(this, "两次输入不一致,请重新输入!");
} else {
password = inputNewPwd1;
JOptionPane.showMessageDialog(this, "密码修改成功!");
}
}
}
}
public static void main(String[] args) {
new BankATM();
}
}
```
这个程序提供了以下功能:
1. 登录银行自动取款机,需要输入银行卡号和密码;
2. 查询余额;
3. 进行ATM取款,每次取款金额为100的倍数,总额不超过5000元,支取金额不允许透支;
4. 进行ATM存款,不允许负存款;
5. 修改密码,需要输入旧密码和新密码,并确认新密码。
请注意,这只是一个简单的示例程序,实际的银行自动取款机需要考虑更多的业务逻辑和安全性。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_lunwen.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)