atm柜员机模拟程序java+mysql+GUI代码
时间: 2023-12-08 12:05:55 浏览: 123
java编写ATM柜员机模拟程序(GUI+javaSwing+MySQL).zip
5星 · 资源好评率100%
以下是一个简单的ATM柜员机模拟程序的Java代码示例,包括连接MySQL数据库和GUI界面的实现。这里使用了Java Swing框架实现GUI界面,以及JDBC驱动连接MySQL数据库。
代码中的数据库名为"atm",包含两个表:"account"和"transaction"。"account"表包含账户信息,包括账户号码、密码、余额等字段,"transaction"表包含交易记录,包括交易时间、账户号码、交易类型、交易金额等字段。
注意:以下代码仅供参考,具体实现需要根据实际需求进行修改和完善。
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public class ATM extends JFrame implements ActionListener {
// GUI components
private JLabel accountLabel, passwordLabel, balanceLabel;
private JTextField accountField;
private JPasswordField passwordField;
private JButton loginButton, depositButton, withdrawButton, transferButton;
private JPanel mainPanel, buttonPanel;
// database connection
private Connection conn;
private Statement stmt;
// current user account info
private String currentUser;
private double currentBalance;
public ATM() {
// initialize GUI components
accountLabel = new JLabel("Account:");
passwordLabel = new JLabel("Password:");
balanceLabel = new JLabel("Balance: $0.00");
accountField = new JTextField(10);
passwordField = new JPasswordField(10);
loginButton = new JButton("Login");
depositButton = new JButton("Deposit");
withdrawButton = new JButton("Withdraw");
transferButton = new JButton("Transfer");
mainPanel = new JPanel(new GridLayout(3, 2));
buttonPanel = new JPanel(new GridLayout(1, 4));
// add GUI components to main panel
mainPanel.add(accountLabel);
mainPanel.add(accountField);
mainPanel.add(passwordLabel);
mainPanel.add(passwordField);
mainPanel.add(balanceLabel);
mainPanel.add(new JLabel()); // placeholder
// add GUI components to button panel
buttonPanel.add(loginButton);
buttonPanel.add(depositButton);
buttonPanel.add(withdrawButton);
buttonPanel.add(transferButton);
// add action listeners to buttons
loginButton.addActionListener(this);
depositButton.addActionListener(this);
withdrawButton.addActionListener(this);
transferButton.addActionListener(this);
// add panels to frame
add(mainPanel, BorderLayout.CENTER);
add(buttonPanel, BorderLayout.SOUTH);
// set frame properties
setTitle("ATM");
setSize(300, 150);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
// connect to database
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost/atm", "root", "");
stmt = conn.createStatement();
} catch (Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(this, "Failed to connect to database.");
}
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == loginButton) {
// login button pressed
String account = accountField.getText();
String password = new String(passwordField.getPassword());
try {
// check if account and password are valid
ResultSet rs = stmt.executeQuery("SELECT * FROM account WHERE account_no = '" + account + "' AND password = '" + password + "'");
if (rs.next()) {
// login successful
currentUser = account;
currentBalance = rs.getDouble("balance");
balanceLabel.setText("Balance: $" + String.format("%.2f", currentBalance));
} else {
// login failed
JOptionPane.showMessageDialog(this, "Invalid account or password.");
}
rs.close();
} catch (Exception ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(this, "Failed to retrieve account information.");
}
} else if (e.getSource() == depositButton) {
// deposit button pressed
if (currentUser == null) {
// not logged in
JOptionPane.showMessageDialog(this, "Please log in first.");
return;
}
String input = JOptionPane.showInputDialog(this, "Enter deposit amount:");
if (input == null || input.isEmpty()) {
// cancel button pressed
return;
}
try {
double amount = Double.parseDouble(input);
if (amount <= 0) {
// invalid input
JOptionPane.showMessageDialog(this, "Invalid amount.");
} else {
// update account balance and transaction record
currentBalance += amount;
stmt.executeUpdate("UPDATE account SET balance = " + currentBalance + " WHERE account_no = '" + currentUser + "'");
stmt.executeUpdate("INSERT INTO transaction (account_no, type, amount) VALUES ('" + currentUser + "', 'Deposit', " + amount + ")");
balanceLabel.setText("Balance: $" + String.format("%.2f", currentBalance));
JOptionPane.showMessageDialog(this, "Deposit successful.");
}
} catch (NumberFormatException ex) {
// invalid input
JOptionPane.showMessageDialog(this, "Invalid amount.");
} catch (Exception ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(this, "Failed to update account information.");
}
} else if (e.getSource() == withdrawButton) {
// withdraw button pressed
if (currentUser == null) {
// not logged in
JOptionPane.showMessageDialog(this, "Please log in first.");
return;
}
String input = JOptionPane.showInputDialog(this, "Enter withdrawal amount:");
if (input == null || input.isEmpty()) {
// cancel button pressed
return;
}
try {
double amount = Double.parseDouble(input);
if (amount <= 0 || amount > currentBalance) {
// invalid input
JOptionPane.showMessageDialog(this, "Invalid amount.");
} else {
// update account balance and transaction record
currentBalance -= amount;
stmt.executeUpdate("UPDATE account SET balance = " + currentBalance + " WHERE account_no = '" + currentUser + "'");
stmt.executeUpdate("INSERT INTO transaction (account_no, type, amount) VALUES ('" + currentUser + "', 'Withdrawal', " + amount + ")");
balanceLabel.setText("Balance: $" + String.format("%.2f", currentBalance));
JOptionPane.showMessageDialog(this, "Withdrawal successful.");
}
} catch (NumberFormatException ex) {
// invalid input
JOptionPane.showMessageDialog(this, "Invalid amount.");
} catch (Exception ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(this, "Failed to update account information.");
}
} else if (e.getSource() == transferButton) {
// transfer button pressed
if (currentUser == null) {
// not logged in
JOptionPane.showMessageDialog(this, "Please log in first.");
return;
}
String input = JOptionPane.showInputDialog(this, "Enter transfer amount:");
if (input == null || input.isEmpty()) {
// cancel button pressed
return;
}
try {
double amount = Double.parseDouble(input);
if (amount <= 0 || amount > currentBalance) {
// invalid input
JOptionPane.showMessageDialog(this, "Invalid amount.");
} else {
String targetAccount = JOptionPane.showInputDialog(this, "Enter target account number:");
if (targetAccount == null || targetAccount.isEmpty()) {
// cancel button pressed
return;
}
ResultSet rs = stmt.executeQuery("SELECT * FROM account WHERE account_no = '" + targetAccount + "'");
if (rs.next()) {
// target account exists
double targetBalance = rs.getDouble("balance");
targetBalance += amount;
currentBalance -= amount;
stmt.executeUpdate("UPDATE account SET balance = " + currentBalance + " WHERE account_no = '" + currentUser + "'");
stmt.executeUpdate("UPDATE account SET balance = " + targetBalance + " WHERE account_no = '" + targetAccount + "'");
stmt.executeUpdate("INSERT INTO transaction (account_no, type, amount) VALUES ('" + currentUser + "', 'Transfer', " + amount + ")");
stmt.executeUpdate("INSERT INTO transaction (account_no, type, amount) VALUES ('" + targetAccount + "', 'Transfer', " + amount + ")");
balanceLabel.setText("Balance: $" + String.format("%.2f", currentBalance));
JOptionPane.showMessageDialog(this, "Transfer successful.");
} else {
// target account not found
JOptionPane.showMessageDialog(this, "Target account not found.");
}
rs.close();
}
} catch (NumberFormatException ex) {
// invalid input
JOptionPane.showMessageDialog(this, "Invalid amount.");
} catch (Exception ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(this, "Failed to update account information.");
}
}
}
public static void main(String[] args) {
new ATM();
}
}
```
希望以上代码对您有所帮助。如有任何疑问或需求,请随时与我交流。
阅读全文