用Java编写一个银行ATM机模拟系统,实现账户管理,存钱,取钱,转账,销户等操作。账户数据保存在Mysql数据库中
时间: 2023-10-06 07:13:44 浏览: 165
好的,下面是一个简单的银行ATM机模拟系统的Java代码,实现了账户管理,存钱,取钱,转账,销户等操作,并且使用了Mysql数据库保存账户数据。
```java
import java.sql.*;
public class ATMSystem {
private static final String DB_URL = "jdbc:mysql://localhost:3306/atm";
private static final String USER = "root";
private static final String PASS = "password";
private static Connection conn = null;
public static void main(String[] args) {
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
System.out.println("Connected to database successfully!");
createTable();
menu();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
private static void createTable() {
try {
Statement stmt = conn.createStatement();
String sql = "CREATE TABLE IF NOT EXISTS accounts " +
"(id INTEGER not NULL AUTO_INCREMENT," +
" name VARCHAR(255)," +
" balance DOUBLE," +
" PRIMARY KEY ( id ))";
stmt.executeUpdate(sql);
System.out.println("Table created successfully!");
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
private static void menu() {
int choice;
do {
System.out.println("\nWelcome to ATM System!");
System.out.println("1. Create Account");
System.out.println("2. Deposit Money");
System.out.println("3. Withdraw Money");
System.out.println("4. Transfer Money");
System.out.println("5. Close Account");
System.out.println("6. Exit");
choice = Input.getInteger("Enter your choice: ");
switch (choice) {
case 1:
createAccount();
break;
case 2:
depositMoney();
break;
case 3:
withdrawMoney();
break;
case 4:
transferMoney();
break;
case 5:
closeAccount();
break;
case 6:
System.out.println("Goodbye!");
break;
default:
System.out.println("Invalid choice!");
}
} while (choice != 6);
}
private static void createAccount() {
String name = Input.getString("Enter your name: ");
double balance = Input.getDouble("Enter your initial balance: ");
try {
PreparedStatement pstmt = conn.prepareStatement("INSERT INTO accounts (name, balance) VALUES (?, ?)");
pstmt.setString(1, name);
pstmt.setDouble(2, balance);
pstmt.executeUpdate();
System.out.println("Account created successfully!");
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
private static void depositMoney() {
int id = Input.getInteger("Enter your account ID: ");
double amount = Input.getDouble("Enter the amount to deposit: ");
try {
PreparedStatement pstmt = conn.prepareStatement("UPDATE accounts SET balance = balance + ? WHERE id = ?");
pstmt.setDouble(1, amount);
pstmt.setInt(2, id);
int rowsUpdated = pstmt.executeUpdate();
if (rowsUpdated > 0) {
System.out.println("Amount deposited successfully!");
} else {
System.out.println("Account not found!");
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
private static void withdrawMoney() {
int id = Input.getInteger("Enter your account ID: ");
double amount = Input.getDouble("Enter the amount to withdraw: ");
try {
PreparedStatement pstmt = conn.prepareStatement("SELECT balance FROM accounts WHERE id = ?");
pstmt.setInt(1, id);
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
double balance = rs.getDouble("balance");
if (balance >= amount) {
pstmt = conn.prepareStatement("UPDATE accounts SET balance = balance - ? WHERE id = ?");
pstmt.setDouble(1, amount);
pstmt.setInt(2, id);
int rowsUpdated = pstmt.executeUpdate();
if (rowsUpdated > 0) {
System.out.println("Amount withdrawn successfully!");
} else {
System.out.println("Account not found!");
}
} else {
System.out.println("Insufficient balance!");
}
} else {
System.out.println("Account not found!");
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
private static void transferMoney() {
int fromId = Input.getInteger("Enter your account ID: ");
int toId = Input.getInteger("Enter the recipient's account ID: ");
double amount = Input.getDouble("Enter the amount to transfer: ");
try {
PreparedStatement pstmt = conn.prepareStatement("SELECT balance FROM accounts WHERE id = ?");
pstmt.setInt(1, fromId);
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
double fromBalance = rs.getDouble("balance");
if (fromBalance >= amount) {
pstmt = conn.prepareStatement("SELECT balance FROM accounts WHERE id = ?");
pstmt.setInt(1, toId);
rs = pstmt.executeQuery();
if (rs.next()) {
double toBalance = rs.getDouble("balance");
pstmt = conn.prepareStatement("UPDATE accounts SET balance = balance - ? WHERE id = ?");
pstmt.setDouble(1, amount);
pstmt.setInt(2, fromId);
int rowsUpdated1 = pstmt.executeUpdate();
pstmt = conn.prepareStatement("UPDATE accounts SET balance = balance + ? WHERE id = ?");
pstmt.setDouble(1, amount);
pstmt.setInt(2, toId);
int rowsUpdated2 = pstmt.executeUpdate();
if (rowsUpdated1 > 0 && rowsUpdated2 > 0) {
System.out.println("Amount transferred successfully!");
} else {
System.out.println("Account not found!");
}
} else {
System.out.println("Recipient's account not found!");
}
} else {
System.out.println("Insufficient balance!");
}
} else {
System.out.println("Account not found!");
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
private static void closeAccount() {
int id = Input.getInteger("Enter your account ID: ");
try {
PreparedStatement pstmt = conn.prepareStatement("DELETE FROM accounts WHERE id = ?");
pstmt.setInt(1, id);
int rowsDeleted = pstmt.executeUpdate();
if (rowsDeleted > 0) {
System.out.println("Account closed successfully!");
} else {
System.out.println("Account not found!");
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
class Input {
private static Scanner scanner = new Scanner(System.in);
public static int getInteger(String prompt) {
int value;
do {
System.out.print(prompt);
while (!scanner.hasNextInt()) {
System.out.println("Invalid input!");
scanner.next();
}
value = scanner.nextInt();
} while (value < 0);
return value;
}
public static double getDouble(String prompt) {
double value;
do {
System.out.print(prompt);
while (!scanner.hasNextDouble()) {
System.out.println("Invalid input!");
scanner.next();
}
value = scanner.nextDouble();
} while (value < 0);
return value;
}
public static String getString(String prompt) {
System.out.print(prompt);
return scanner.nextLine();
}
}
```
在上述代码中,我们使用了Java的JDBC API来连接Mysql数据库,并实现了账户管理,存钱,取钱,转账,销户等操作。同时,我们使用了Input类来获取用户输入的整数、小数和字符串。
请注意,在运行此代码之前,需要确保您已经正确安装了Mysql,并创建了名为“atm”的数据库。另外,您需要将代码中的数据库连接URL、用户名和密码替换为您自己的值。
阅读全文