java跟mysql编程实现银行ATM机模拟系统,包含转账、存款、取款、销户、银行账户管理功能等功能,并可以在键盘上输入1、2、3实现上述功能。
时间: 2023-12-28 10:16:09 浏览: 146
下面是一个简单的Java程序,可以实现银行ATM机模拟系统,包含转账、存款、取款、销户、银行账户管理功能等功能。这个程序使用了MySQL数据库来存储银行账户信息和交易记录。
请注意,这个程序只是一个简单的示例,没有进行严格的错误处理和安全性检查。在实际应用中,请务必进行充分的测试和验证,以确保程序的正确性和安全性。
```java
import java.sql.*;
import java.util.Scanner;
public class BankATMSystem {
private static final String DB_URL = "jdbc:mysql://localhost:3306/bank";
private static final String DB_USER = "root";
private static final String DB_PASSWORD = "password";
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Connection conn = null;
try {
conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
while (true) {
System.out.println("请选择操作:");
System.out.println("1. 转账");
System.out.println("2. 存款");
System.out.println("3. 取款");
System.out.println("4. 销户");
System.out.println("5. 银行账户管理");
System.out.println("6. 退出");
int choice = scanner.nextInt();
switch (choice) {
case 1:
transfer(conn, scanner);
break;
case 2:
deposit(conn, scanner);
break;
case 3:
withdraw(conn, scanner);
break;
case 4:
closeAccount(conn, scanner);
break;
case 5:
manageAccounts(conn, scanner);
break;
case 6:
return;
default:
System.out.println("无效的选择。");
break;
}
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
private static void transfer(Connection conn, Scanner scanner) throws SQLException {
System.out.println("请输入转出账户号码:");
int fromAccount = scanner.nextInt();
System.out.println("请输入转入账户号码:");
int toAccount = scanner.nextInt();
System.out.println("请输入转账金额:");
double amount = scanner.nextDouble();
conn.setAutoCommit(false);
try (PreparedStatement stmt1 = conn.prepareStatement("UPDATE account SET balance = balance - ? WHERE id = ?");
PreparedStatement stmt2 = conn.prepareStatement("UPDATE account SET balance = balance + ? WHERE id = ?");
PreparedStatement stmt3 = conn.prepareStatement("INSERT INTO transaction (from_account, to_account, amount) VALUES (?, ?, ?)")) {
stmt1.setDouble(1, amount);
stmt1.setInt(2, fromAccount);
int rows1 = stmt1.executeUpdate();
if (rows1 != 1) {
throw new SQLException("更新账户信息失败。");
}
stmt2.setDouble(1, amount);
stmt2.setInt(2, toAccount);
int rows2 = stmt2.executeUpdate();
if (rows2 != 1) {
throw new SQLException("更新账户信息失败。");
}
stmt3.setInt(1, fromAccount);
stmt3.setInt(2, toAccount);
stmt3.setDouble(3, amount);
int rows3 = stmt3.executeUpdate();
if (rows3 != 1) {
throw new SQLException("插入交易记录失败。");
}
conn.commit();
System.out.println("转账成功。");
} catch (SQLException e) {
conn.rollback();
throw e;
} finally {
conn.setAutoCommit(true);
}
}
private static void deposit(Connection conn, Scanner scanner) throws SQLException {
System.out.println("请输入账户号码:");
int accountId = scanner.nextInt();
System.out.println("请输入存款金额:");
double amount = scanner.nextDouble();
try (PreparedStatement stmt1 = conn.prepareStatement("UPDATE account SET balance = balance + ? WHERE id = ?");
PreparedStatement stmt2 = conn.prepareStatement("INSERT INTO transaction (to_account, amount) VALUES (?, ?)")) {
stmt1.setDouble(1, amount);
stmt1.setInt(2, accountId);
int rows1 = stmt1.executeUpdate();
if (rows1 != 1) {
throw new SQLException("更新账户信息失败。");
}
stmt2.setInt(1, accountId);
stmt2.setDouble(2, amount);
int rows2 = stmt2.executeUpdate();
if (rows2 != 1) {
throw new SQLException("插入交易记录失败。");
}
System.out.println("存款成功。");
}
}
private static void withdraw(Connection conn, Scanner scanner) throws SQLException {
System.out.println("请输入账户号码:");
int accountId = scanner.nextInt();
System.out.println("请输入取款金额:");
double amount = scanner.nextDouble();
try (PreparedStatement stmt1 = conn.prepareStatement("UPDATE account SET balance = balance - ? WHERE id = ?");
PreparedStatement stmt2 = conn.prepareStatement("INSERT INTO transaction (from_account, amount) VALUES (?, ?)")) {
stmt1.setDouble(1, amount);
stmt1.setInt(2, accountId);
int rows1 = stmt1.executeUpdate();
if (rows1 != 1) {
throw new SQLException("更新账户信息失败。");
}
stmt2.setInt(1, accountId);
stmt2.setDouble(2, amount);
int rows2 = stmt2.executeUpdate();
if (rows2 != 1) {
throw new SQLException("插入交易记录失败。");
}
System.out.println("取款成功。");
}
}
private static void closeAccount(Connection conn, Scanner scanner) throws SQLException {
System.out.println("请输入账户号码:");
int accountId = scanner.nextInt();
try (PreparedStatement stmt1 = conn.prepareStatement("DELETE FROM account WHERE id = ?");
PreparedStatement stmt2 = conn.prepareStatement("DELETE FROM transaction WHERE from_account = ? OR to_account = ?")) {
stmt1.setInt(1, accountId);
int rows1 = stmt1.executeUpdate();
stmt2.setInt(1, accountId);
stmt2.setInt(2, accountId);
int rows2 = stmt2.executeUpdate();
if (rows1 != 1 || rows2 < 1) {
throw new SQLException("删除账户信息失败。");
}
System.out.println("销户成功。");
}
}
private static void manageAccounts(Connection conn, Scanner scanner) throws SQLException {
while (true) {
System.out.println("请选择操作:");
System.out.println("1. 新增账户");
System.out.println("2. 修改账户");
System.out.println("3. 查询账户");
System.out.println("4. 返回上级菜单");
int choice = scanner.nextInt();
switch (choice) {
case 1:
createAccount(conn, scanner);
break;
case 2:
updateAccount(conn, scanner);
break;
case 3:
queryAccount(conn, scanner);
break;
case 4:
return;
default:
System.out.println("无效的选择。");
break;
}
}
}
private static void createAccount(Connection conn, Scanner scanner) throws SQLException {
System.out.println("请输入账户号码:");
int accountId = scanner.nextInt();
System.out.println("请输入账户名称:");
String accountName = scanner.next();
System.out.println("请输入账户类型(1:储蓄账户,2:信用账户):");
int accountType = scanner.nextInt();
System.out.println("请输入账户初始余额:");
double balance = scanner.nextDouble();
try (PreparedStatement stmt = conn.prepareStatement("INSERT INTO account (id, name, type, balance) VALUES (?, ?, ?, ?)")) {
stmt.setInt(1, accountId);
stmt.setString(2, accountName);
stmt.setInt(3, accountType);
stmt.setDouble(4, balance);
int rows = stmt.executeUpdate();
if (rows != 1) {
throw new SQLException("插入账户信息失败。");
}
System.out.println("新增账户成功。");
}
}
private static void updateAccount(Connection conn, Scanner scanner) throws SQLException {
System.out.println("请输入账户号码:");
int accountId = scanner.nextInt();
System.out.println("请输入要修改的字段(1:账户名称,2:账户类型,3:账户余额):");
int field = scanner.nextInt();
String sql = null;
switch (field) {
case 1:
System.out.println("请输入新的账户名称:");
String accountName = scanner.next();
sql = "UPDATE account SET name = ? WHERE id = ?";
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setString(1, accountName);
stmt.setInt(2, accountId);
int rows = stmt.executeUpdate();
if (rows != 1) {
throw new SQLException("更新账户信息失败。");
}
}
break;
case 2:
System.out.println("请输入新的账户类型(1:储蓄账户,2:信用账户):");
int accountType = scanner.nextInt();
sql = "UPDATE account SET type = ? WHERE id = ?";
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setInt(1, accountType);
stmt.setInt(2, accountId);
int rows = stmt.executeUpdate();
if (rows != 1) {
throw new SQLException("更新账户信息失败。");
}
}
break;
case 3:
System.out.println("请输入新的账户余额:");
double balance = scanner.nextDouble();
sql = "UPDATE account SET balance = ? WHERE id = ?";
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setDouble(1, balance);
stmt.setInt(2, accountId);
int rows = stmt.executeUpdate();
if (rows != 1) {
throw new SQLException("更新账户信息失败。");
}
}
break;
default:
System.out.println("无效的选择。");
break;
}
}
private static void queryAccount(Connection conn, Scanner scanner) throws SQLException {
System.out.println("请输入账户号码(留空表示查询所有账户):");
String accountIdStr = scanner.next();
String sql = "SELECT * FROM account";
if (!accountIdStr.isEmpty()) {
sql += " WHERE id = " + accountIdStr;
}
try (Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql)) {
System.out.println("账户号码\t账户名称\t账户类型\t账户余额");
while (rs.next()) {
int accountId = rs.getInt("id");
String accountName = rs.getString("name");
int accountType = rs.getInt("type");
double balance = rs.getDouble("balance");
System.out.println(accountId + "\t" + accountName + "\t" + accountType + "\t" + balance);
}
}
}
}
```
在程序中,我们首先定义了MySQL数据库的连接信息(DB_URL、DB_USER、DB_PASSWORD),然后通过JDBC连接到这个数据库。在程序的主循环中,我们根据用户的选择调用不同的函数来实现不同的功能。
在转账函数中,我们首先从用户输入中获取转出账户号码、转入账户号码和转账金额。然后我们使用事务来执行转账操作:我们首先更新转出账户的余额,然后再更新转入账户的余额,最后插入一条交易记录。如果在这个过程中出现了任何错误,我们就回滚事务,以保证数据的一致性。
在存款和取款函数中,我们只需要更新账户的余额,并插入一条交易记录。
在销户函数中,我们首先从用户输入中获取要销户的账户号码,然后删除这个账户的信息和相关的交易记录。
在银行账户管理函数中,我们可以进行新增账户、修改账户和查询账户的操作。在新增账户函数中,我们从用户输入中获取账户号码、账户名称、账户类型和账户初始余额,并插入一条账户信息。在修改账户函数中,我们先从用户输入中获取要修改的字段和新的值,然后更新相应的账户信息。在查询账户函数中,我们可以根据账户号码来查询特定的账户信息,或者查询所有账户的信息。
这样,我们就完成了银行ATM机模拟系统的开发。
阅读全文