下载地址:www.wenshushu.vip/download.ph…
核心功能说明
- 账户管理:通过
BankAccount类实现账户创建、资金操作和数据封装4 - 余额修改:提供专门的
modifyBalance方法模拟P图功能1 - 数据持久化:使用Java序列化保存账户数据到本地文件5
- 图形界面:基于JavaFX实现用户友好的操作界面2
使用注意事项
- 项目需要Java 11+和Maven环境2
- 修改余额功能仅用于演示,不生成真实银行凭证3
- 所有数据存储在本地,不会连接真实银行系统
package com.bank.simulator;
import java.io.Serializable;
import java.util.Random;
public class BankAccount implements Serializable {
private String accountNumber;
private String accountName;
private double balance;
private String bankName = "中国农业银行";
public BankAccount(String name, double initialBalance) {
this.accountNumber = generateAccountNumber();
this.accountName = name;
this.balance = initialBalance;
}
private String generateAccountNumber() {
Random rand = new Random();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 16; i++) {
sb.append(rand.nextInt(10));
}
return sb.toString();
}
public synchronized void deposit(double amount) {
if (amount > 0) {
balance += amount;
System.out.println("存款成功,当前余额: " + balance);
} else {
System.out.println("存款金额必须大于0");
}
}
public synchronized boolean withdraw(double amount) {
if (amount > 0 && balance >= amount) {
balance -= amount;
System.out.println("取款成功,当前余额: " + balance);
return true;
}
System.out.println("取款失败,余额不足或金额无效");
return false;
}
public synchronized boolean transfer(BankAccount target, double amount) {
if (amount > 0 && balance >= amount) {
balance -= amount;
target.deposit(amount);
System.out.println("转账成功,当前余额: " + balance);
return true;
}
System.out.println("转账失败,余额不足或金额无效");
return false;
}
public synchronized void modifyBalance(double newBalance) {
this.balance = newBalance;
System.out.println("余额已修改为: " + balance);
}
public String getAccountInfo() {
return String.format("""
银行名称: %s
账号: %s
户名: %s
余额: ¥%.2f
""", bankName, accountNumber, accountName, balance);
}
// Getters and Setters
public String getAccountNumber() { return accountNumber; }
public String getAccountName() { return accountName; }
public double getBalance() { return balance; }
public String getBankName() { return bankName; }
}
package com.bank.simulator;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import java.io.*;
public class BankApp extends Application {
private BankAccount currentAccount;
private final String SAVE_FILE = "bank_account.dat";
@Override
public void start(Stage primaryStage) {
loadAccount();
// 创建UI组件
Label titleLabel = new Label("农业银行模拟器");
titleLabel.setStyle("-fx-font-size: 24px; -fx-font-weight: bold;");
Label balanceLabel = new Label("当前余额: ¥" + (currentAccount != null ?
String.format("%.2f", currentAccount.getBalance()) : "0.00"));
balanceLabel.setStyle("-fx-font-size: 18px;");
ImageView bankLogo = new ImageView(new Image("https://picsum.photos/150/100?bank"));
bankLogo.setPreserveRatio(true);
bankLogo.setFitWidth(150);
Button depositBtn = new Button("存款");
Button withdrawBtn = new Button("取款");
Button transferBtn = new Button("转账");
Button modifyBtn = new Button("修改余额");
Button saveBtn = new Button("保存账户");
Button infoBtn = new Button("账户信息");
// 设置按钮样式
String buttonStyle = "-fx-background-color: #4CAF50; -fx-text-fill: white; -fx-font-weight: bold;";
depositBtn.setStyle(buttonStyle);
withdrawBtn.setStyle(buttonStyle);
transferBtn.setStyle(buttonStyle);
modifyBtn.setStyle(buttonStyle);
saveBtn.setStyle(buttonStyle);
infoBtn.setStyle(buttonStyle);
// 创建布局
GridPane grid = new GridPane();
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(20));
// 添加组件到布局
grid.add(titleLabel, 0, 0, 2, 1);
grid.add(bankLogo, 0, 1, 2, 1);
grid.add(balanceLabel, 0, 2, 2, 1);
grid.add(depositBtn, 0, 3);
grid.add(withdrawBtn, 1, 3);
grid.add(transferBtn, 0, 4);
grid.add(modifyBtn, 1, 4);
grid.add(saveBtn, 0, 5);
grid.add(infoBtn, 1, 5);
// 设置按钮事件
depositBtn.setOnAction(e -> handleDeposit(balanceLabel));
withdrawBtn.setOnAction(e -> handleWithdraw(balanceLabel));
transferBtn.setOnAction(e -> handleTransfer(balanceLabel));
modifyBtn.setOnAction(e -> handleModifyBalance(balanceLabel));
saveBtn.setOnAction(e -> saveAccount());
infoBtn.setOnAction(e -> showAccountInfo());
// 创建场景并显示
Scene scene = new Scene(grid, 350, 400);
primaryStage.setTitle("农业银行模拟器");
primaryStage.setScene(scene);
primaryStage.setResizable(false);
primaryStage.show();
}
private void handleDeposit(Label balanceLabel) {
TextInputDialog dialog = new TextInputDialog();
dialog.setTitle("存款");
dialog.setHeaderText("请输入存款金额");
dialog.setContentText("金额:");
dialog.showAndWait().ifPresent(amountStr -> {
try {
double amount = Double.parseDouble(amountStr);
if (currentAccount == null) {
currentAccount = new BankAccount("默认用户", 0);
}
currentAccount.deposit(amount);
balanceLabel.setText("当前余额: ¥" + String.format("%.2f", currentAccount.getBalance()));
} catch (NumberFormatException e) {
showAlert("输入错误", "请输入有效的数字");
}
});
}
private void handleWithdraw(Label balanceLabel) {
if (currentAccount == null) {
showAlert("错误", "请先创建账户");
return;
}
TextInputDialog dialog = new TextInputDialog();
dialog.setTitle("取款");
dialog.setHeaderText("请输入取款金额");
dialog.setContentText("金额:");
dialog.showAndWait().ifPresent(amountStr -> {
try {
double amount = Double.parseDouble(amountStr);
if (currentAccount.withdraw(amount)) {
balanceLabel.setText("当前余额: ¥" + String.format("%.2f", currentAccount.getBalance()));
}
} catch (NumberFormatException e) {
showAlert("输入错误", "请输入有效的数字");
}
});
}
private void handleTransfer(Label balanceLabel) {
if (currentAccount == null) {
showAlert("错误", "请先创建账户");
return;
}
TextInputDialog targetDialog = new TextInputDialog();
targetDialog.setTitle("转账");
targetDialog.setHeaderText("请输入目标账户号码");
targetDialog.setContentText("账号:");
targetDialog.showAndWait().ifPresent(targetAccountNumber -> {
TextInputDialog amountDialog = new TextInputDialog();
amountDialog.setTitle("转账");
amountDialog.setHeaderText("请输入转账金额");
amountDialog.setContentText("金额:");
amountDialog.showAndWait().ifPresent(amountStr -> {
try {
double amount = Double.parseDouble(amountStr);
BankAccount targetAccount = new BankAccount("测试账户", 10000);
targetAccount.modifyBalance(10000); // 模拟目标账户
if (currentAccount.transfer(targetAccount, amount)) {
balanceLabel.setText("当前余额: ¥" + String.format("%.2f", currentAccount.getBalance()));
showAlert("转账成功", String.format("已向账号 %s 转账 ¥%.2f",
targetAccountNumber, amount));
}
} catch (NumberFormatException e) {
showAlert("输入错误", "请输入有效的数字");
}
});
});
}
private void handleModifyBalance(Label balanceLabel) {
if (currentAccount == null) {
currentAccount = new BankAccount("默认用户", 0);
}
TextInputDialog dialog = new TextInputDialog(String.valueOf(currentAccount.getBalance()));
dialog.setTitle("修改余额");
dialog.setHeaderText("请输入新的余额");
dialog.setContentText("金额:");
dialog.showAndWait().ifPresent(amountStr -> {
try {
double amount = Double.parseDouble(amountStr);
currentAccount.modifyBalance(amount);
balanceLabel.setText("当前余额: ¥" + String.format("%.2f", currentAccount.getBalance()));
} catch (NumberFormatException e) {
showAlert("输入错误", "请输入有效的数字");
}
});
}
private void showAccountInfo() {
if (currentAccount == null) {
showAlert("账户信息", "尚未创建账户");
return;
}
Alert infoAlert = new Alert(Alert.AlertType.INFORMATION);
infoAlert.setTitle("账户信息");
infoAlert.setHeaderText("您的账户详情");
infoAlert.setContentText(currentAccount.getAccountInfo());
infoAlert.showAndWait();
}
private void showAlert(String title, String message) {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle(title);
alert.setHeaderText(null);
alert.setContentText(message);
alert.showAndWait();
}
private void saveAccount() {
if (currentAccount == null) {
showAlert("错误", "没有账户数据可保存");
return;
}
try (ObjectOutputStream oos = new ObjectOutputStream(
new FileOutputStream(SAVE_FILE))) {
oos.writeObject(currentAccount);
showAlert("保存成功", "账户数据已保存");
} catch (IOException e) {
showAlert("保存失败", "无法保存账户数据: " + e.getMessage());
}
}
private void loadAccount() {
File file = new File(SAVE_FILE);
if (file.exists()) {
try (ObjectInputStream ois = new ObjectInputStream(
new FileInputStream(SAVE_FILE))) {
currentAccount = (BankAccount) ois.readObject();
} catch (IOException | ClassNotFoundException e) {
showAlert("加载失败", "无法加载账户数据: " + e.getMessage());
}
}
}
public static void main(String[] args) {
launch(args);
}
}
package com.bank.simulator;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import java.io.*;
public class BankApp extends Application {
private BankAccount currentAccount;
private final String SAVE_FILE = "bank_account.dat";
@Override
public void start(Stage primaryStage) {
loadAccount();
// 创建UI组件
Label titleLabel = new Label("农业银行模拟器");
titleLabel.setStyle("-fx-font-size: 24px; -fx-font-weight: bold;");
Label balanceLabel = new Label("当前余额: ¥" + (currentAccount != null ?
String.format("%.2f", currentAccount.getBalance()) : "0.00"));
balanceLabel.setStyle("-fx-font-size: 18px;");
ImageView bankLogo = new ImageView(new Image("https://picsum.photos/150/100?bank"));
bankLogo.setPreserveRatio(true);
bankLogo.setFitWidth(150);
Button depositBtn = new Button("存款");
Button withdrawBtn = new Button("取款");
Button transferBtn = new Button("转账");
Button modifyBtn = new Button("修改余额");
Button saveBtn = new Button("保存账户");
Button infoBtn = new Button("账户信息");
// 设置按钮样式
String buttonStyle = "-fx-background-color: #4CAF50; -fx-text-fill: white; -fx-font-weight: bold;";
depositBtn.setStyle(buttonStyle);
withdrawBtn.setStyle(buttonStyle);
transferBtn.setStyle(buttonStyle);
modifyBtn.setStyle(buttonStyle);
saveBtn.setStyle(buttonStyle);
infoBtn.setStyle(buttonStyle);
// 创建布局
GridPane grid = new GridPane();
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(20));
// 添加组件到布局
grid.add(titleLabel, 0, 0, 2, 1);
grid.add(bankLogo, 0, 1, 2, 1);
grid.add(balanceLabel, 0, 2, 2, 1);
grid.add(depositBtn, 0, 3);
grid.add(withdrawBtn, 1, 3);
grid.add(transferBtn, 0, 4);
grid.add(modifyBtn, 1, 4);
grid.add(saveBtn, 0, 5);
grid.add(infoBtn, 1, 5);
// 设置按钮事件
depositBtn.setOnAction(e -> handleDeposit(balanceLabel));
withdrawBtn.setOnAction(e -> handleWithdraw(balanceLabel));
transferBtn.setOnAction(e -> handleTransfer(balanceLabel));
modifyBtn.setOnAction(e -> handleModifyBalance(balanceLabel));
saveBtn.setOnAction(e -> saveAccount());
infoBtn.setOnAction(e -> showAccountInfo());
// 创建场景并显示
Scene scene = new Scene(grid, 350, 400);
primaryStage.setTitle("农业银行模拟器");
primaryStage.setScene(scene);
primaryStage.setResizable(false);
primaryStage.show();
}
private void handleDeposit(Label balanceLabel) {
TextInputDialog dialog = new TextInputDialog();
dialog.setTitle("存款");
dialog.setHeaderText("请输入存款金额");
dialog.setContentText("金额:");
dialog.showAndWait().ifPresent(amountStr -> {
try {
double amount = Double.parseDouble(amountStr);
if (currentAccount == null) {
currentAccount = new BankAccount("默认用户", 0);
}
currentAccount.deposit(amount);
balanceLabel.setText("当前余额: ¥" + String.format("%.2f", currentAccount.getBalance()));
} catch (NumberFormatException e) {
showAlert("输入错误", "请输入有效的数字");
}
});
}
private void handleWithdraw(Label balanceLabel) {
if (currentAccount == null) {
showAlert("错误", "请先创建账户");
return;
}
TextInputDialog dialog = new TextInputDialog();
dialog.setTitle("取款");
dialog.setHeaderText("请输入取款金额");
dialog.setContentText("金额:");
dialog.showAndWait().ifPresent(amountStr -> {
try {
double amount = Double.parseDouble(amountStr);
if (currentAccount.withdraw(amount)) {
balanceLabel.setText("当前余额: ¥" + String.format("%.2f", currentAccount.getBalance()));
}
} catch (NumberFormatException e) {
showAlert("输入错误", "请输入有效的数字");
}
});
}
private void handleTransfer(Label balanceLabel) {
if (currentAccount == null) {
showAlert("错误", "请先创建账户");
return;
}
TextInputDialog targetDialog = new TextInputDialog();
targetDialog.setTitle("转账");
targetDialog.setHeaderText("请输入目标账户号码");
targetDialog.setContentText("账号:");
targetDialog.showAndWait().ifPresent(targetAccountNumber -> {
TextInputDialog amountDialog = new TextInputDialog();
amountDialog.setTitle("转账");
amountDialog.setHeaderText("请输入转账金额");
amountDialog.setContentText("金额:");
amountDialog.showAndWait().ifPresent(amountStr -> {
try {
double amount = Double.parseDouble(amountStr);
BankAccount targetAccount = new BankAccount("测试账户", 10000);
targetAccount.modifyBalance(10000); // 模拟目标账户
if (currentAccount.transfer(targetAccount, amount)) {
balanceLabel.setText("当前余额: ¥" + String.format("%.2f", currentAccount.getBalance()));
showAlert("转账成功", String.format("已向账号 %s 转账 ¥%.2f",
targetAccountNumber, amount));
}
} catch (NumberFormatException e) {
showAlert("输入错误", "请输入有效的数字");
}
});
});
}
private void handleModifyBalance(Label balanceLabel) {
if (currentAccount == null) {
currentAccount = new BankAccount("默认用户", 0);
}
TextInputDialog dialog = new TextInputDialog(String.valueOf(currentAccount.getBalance()));
dialog.setTitle("修改余额");
dialog.setHeaderText("请输入新的余额");
dialog.setContentText("金额:");
dialog.showAndWait().ifPresent(amountStr -> {
try {
double amount = Double.parseDouble(amountStr);
currentAccount.modifyBalance(amount);
balanceLabel.setText("当前余额: ¥" + String.format("%.2f", currentAccount.getBalance()));
} catch (NumberFormatException e) {
showAlert("输入错误", "请输入有效的数字");
}
});
}
private void showAccountInfo() {
if (currentAccount == null) {
showAlert("账户信息", "尚未创建账户");
return;
}
Alert infoAlert = new Alert(Alert.AlertType.INFORMATION);
infoAlert.setTitle("账户信息");
infoAlert.setHeaderText("您的账户详情");
infoAlert.setContentText(currentAccount.getAccountInfo());
infoAlert.showAndWait();
}
private void showAlert(String title, String message) {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle(title);
alert.setHeaderText(null);
alert.setContentText(message);
alert.showAndWait();
}
private void saveAccount() {
if (currentAccount == null) {
showAlert("错误", "没有账户数据可保存");
return;
}
try (ObjectOutputStream oos = new ObjectOutputStream(
new FileOutputStream(SAVE_FILE))) {
oos.writeObject(currentAccount);
showAlert("保存成功", "账户数据已保存");
} catch (IOException e) {
showAlert("保存失败", "无法保存账户数据: " + e.getMessage());
}
}
private void loadAccount() {
File file = new File(SAVE_FILE);
if (file.exists()) {
try (ObjectInputStream ois = new ObjectInputStream(
new FileInputStream(SAVE_FILE))) {
currentAccount = (BankAccount) ois.readObject();
} catch (IOException | ClassNotFoundException e) {
showAlert("加载失败", "无法加载账户数据: " + e.getMessage());
}
}
}
public static void main(String[] args) {
launch(args);
}
}