设计一个java程序链接数据库实现模拟银行atm机
时间: 2023-07-16 11:14:35 浏览: 191
好的,下面是一个简单的Java程序实现模拟银行ATM机:
首先,我们需要创建一个ATM类,它代表了整个ATM机的控制器。在ATM类中,我们需要定义以下属性:
- cardNumber:表示插入的银行卡号码;
- cardPin:表示银行卡的密码;
- balance:表示银行卡的余额;
- isCardInserted:表示是否插入了银行卡;
- isPinValid:表示银行卡密码是否正确。
```
import java.sql.*;
public class ATM {
private String cardNumber;
private int cardPin;
private double balance;
private boolean isCardInserted;
private boolean isPinValid;
private Connection connection;
public ATM() {
isCardInserted = false;
isPinValid = false;
balance = 0;
}
public void insertCard(String cardNumber, int cardPin) {
this.cardNumber = cardNumber;
this.cardPin = cardPin;
isCardInserted = true;
validatePin();
}
public void ejectCard() {
isCardInserted = false;
isPinValid = false;
cardNumber = "";
cardPin = 0;
}
public void validatePin() {
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/atm", "root", "password");
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM accounts WHERE card_number='" + cardNumber + "'");
if (rs.next()) {
int pin = rs.getInt("pin");
if (pin == cardPin) {
isPinValid = true;
balance = rs.getDouble("balance");
}
}
rs.close();
stmt.close();
connection.close();
} catch (Exception e) {
System.out.println(e);
}
}
public void checkBalance() {
if (isCardInserted && isPinValid) {
System.out.println("Your balance is: " + balance);
} else {
System.out.println("Please insert your card and enter your PIN.");
}
}
public void withdraw(double amount) {
if (isCardInserted && isPinValid) {
if (amount <= balance) {
balance -= amount;
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/atm", "root", "password");
Statement stmt = connection.createStatement();
stmt.executeUpdate("UPDATE accounts SET balance=" + balance + " WHERE card_number='" + cardNumber + "'");
stmt.close();
connection.close();
} catch (Exception e) {
System.out.println(e);
}
System.out.println("You have withdrawn " + amount + ". Your new balance is " + balance + ".");
} else {
System.out.println("Insufficient funds. Your balance is " + balance + ".");
}
} else {
System.out.println("Please insert your card and enter your PIN.");
}
}
}
```
在ATM类中,我们定义了以下方法:
- insertCard:插入银行卡,并验证密码;
- ejectCard:退卡;
- validatePin:验证密码是否正确;
- checkBalance:查询余额;
- withdraw:取款。
在validatePin方法中,我们通过JDBC连接到MySQL数据库,根据卡号从accounts表中查询密码和余额。如果密码正确,将isPinValid设置为true,并将余额设置为balance。
在withdraw方法中,我们首先检查卡是否插入并且密码是否正确。如果是,则检查余额是否足够取款。如果足够,则将余额减去取款金额,并将新的余额更新到数据库中。
下面是一个简单的测试程序,它创建一个ATM对象并模拟了一些操作:
```
import java.util.Scanner;
public class TestATM {
public static void main(String[] args) {
ATM atm = new ATM();
Scanner scanner = new Scanner(System.in);
System.out.println("Welcome to the ATM!");
System.out.println("Please insert your card.");
System.out.print("Enter card number: ");
String cardNumber = scanner.nextLine();
System.out.print("Enter PIN: ");
int cardPin = scanner.nextInt();
atm.insertCard(cardNumber, cardPin);
while (true) {
System.out.println("What would you like to do?");
System.out.println("1. Check balance");
System.out.println("2. Withdraw");
System.out.println("3. Eject card");
int choice = scanner.nextInt();
switch (choice) {
case 1:
atm.checkBalance();
break;
case 2:
System.out.print("Enter amount to withdraw: ");
double amount = scanner.nextDouble();
atm.withdraw(amount);
break;
case 3:
atm.ejectCard();
System.out.println("Thank you for using the ATM!");
System.exit(0);
default:
System.out.println("Invalid choice. Please try again.");
break;
}
}
}
}
```
这个程序模拟了一个ATM机的基本功能,包括插卡、验证密码、查询余额和取款。当用户选择退出时,程序将退卡并结束。
阅读全文