Initial commit
This commit is contained in:
142
BankSystem/src/main/java/BankSystemUI.java
Normal file
142
BankSystem/src/main/java/BankSystemUI.java
Normal file
@@ -0,0 +1,142 @@
|
||||
import javax.swing.*;
|
||||
|
||||
public class BankSystemUI extends JDialog {
|
||||
private JTextField accountIdField;
|
||||
private JButton QueryButton, CreateButton, DepositButton, WithdrawButton, TransferButton, DeleteButton;
|
||||
private JTextField NameField, BalanceField;
|
||||
private JPanel contentPane;
|
||||
|
||||
private BankSystemCore core = new BankSystemCore();
|
||||
|
||||
public BankSystemUI() {
|
||||
setResizable(false);
|
||||
setContentPane(contentPane);
|
||||
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
|
||||
setTitle("简易银行系统");
|
||||
|
||||
QueryButton.addActionListener(e -> queryAccountAction());
|
||||
|
||||
CreateButton.addActionListener(e -> {
|
||||
String name = JOptionPane.showInputDialog(this, "请输入账户名:", "创建账户", JOptionPane.PLAIN_MESSAGE);
|
||||
if (name == null || name.trim().isEmpty()) {
|
||||
if (name != null) {
|
||||
JOptionPane.showMessageDialog(this, "账户名不能为空。", "创建账户错误", JOptionPane.ERROR_MESSAGE);
|
||||
}
|
||||
return;
|
||||
}
|
||||
try {
|
||||
Account account = core.CreateAccount(name.trim());
|
||||
if (account != null) {
|
||||
JOptionPane.showMessageDialog(this, "账户创建成功!\n账户ID: " + account.getAccountId(), "创建账户成功", JOptionPane.INFORMATION_MESSAGE);
|
||||
accountIdField.setText(account.getAccountId());
|
||||
queryAccountAction();
|
||||
} else {
|
||||
JOptionPane.showMessageDialog(this, "创建账户失败。", "创建账户错误", JOptionPane.ERROR_MESSAGE);
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
JOptionPane.showMessageDialog(this, "创建账户时发生错误: " + ex.getMessage(), "创建账户错误", JOptionPane.ERROR_MESSAGE);
|
||||
}
|
||||
});
|
||||
|
||||
DepositButton.addActionListener(e -> {
|
||||
String id = accountIdField.getText().trim();
|
||||
if (id.isEmpty()) {
|
||||
JOptionPane.showMessageDialog(this, "请输入账户ID。", "存款错误", JOptionPane.ERROR_MESSAGE);
|
||||
return;
|
||||
}
|
||||
String amt = JOptionPane.showInputDialog(this, "请输入存款金额:", "存款", JOptionPane.PLAIN_MESSAGE);
|
||||
if (amt == null || amt.trim().isEmpty()) return;
|
||||
try {
|
||||
core.Deposit(id, Double.parseDouble(amt.trim()));
|
||||
JOptionPane.showMessageDialog(this, "存款成功。", "存款成功", JOptionPane.INFORMATION_MESSAGE);
|
||||
queryAccountAction();
|
||||
} catch (NumberFormatException ex) {
|
||||
JOptionPane.showMessageDialog(this, "无效的金额格式。", "存款错误", JOptionPane.ERROR_MESSAGE);
|
||||
} catch (Exception ex) {
|
||||
JOptionPane.showMessageDialog(this, ex.getMessage(), "存款错误", JOptionPane.ERROR_MESSAGE);
|
||||
}
|
||||
});
|
||||
|
||||
WithdrawButton.addActionListener(e -> {
|
||||
String id = accountIdField.getText().trim();
|
||||
if (id.isEmpty()) {
|
||||
JOptionPane.showMessageDialog(this, "请输入账户ID。", "取款错误", JOptionPane.ERROR_MESSAGE);
|
||||
return;
|
||||
}
|
||||
String amt = JOptionPane.showInputDialog(this, "请输入取款金额:", "取款", JOptionPane.PLAIN_MESSAGE);
|
||||
if (amt == null || amt.trim().isEmpty()) return;
|
||||
try {
|
||||
core.Withdraw(id, Double.parseDouble(amt.trim()));
|
||||
JOptionPane.showMessageDialog(this, "取款成功。", "取款成功", JOptionPane.INFORMATION_MESSAGE);
|
||||
queryAccountAction();
|
||||
} catch (NumberFormatException ex) {
|
||||
JOptionPane.showMessageDialog(this, "无效的金额格式。", "取款错误", JOptionPane.ERROR_MESSAGE);
|
||||
} catch (Exception ex) {
|
||||
JOptionPane.showMessageDialog(this, ex.getMessage(), "取款错误", JOptionPane.ERROR_MESSAGE);
|
||||
}
|
||||
});
|
||||
|
||||
TransferButton.addActionListener(e -> {
|
||||
String from = accountIdField.getText().trim();
|
||||
if (from.isEmpty()) {
|
||||
JOptionPane.showMessageDialog(this, "请输入源账户ID。", "转账错误", JOptionPane.ERROR_MESSAGE);
|
||||
return;
|
||||
}
|
||||
String to = JOptionPane.showInputDialog(this, "请输入目标账户ID:", "转账", JOptionPane.PLAIN_MESSAGE);
|
||||
if (to == null || to.trim().isEmpty()) return;
|
||||
String amt = JOptionPane.showInputDialog(this, "请输入转账金额:", "转账", JOptionPane.PLAIN_MESSAGE);
|
||||
if (amt == null || amt.trim().isEmpty()) return;
|
||||
try {
|
||||
core.Transfer(from, to.trim(), Double.parseDouble(amt.trim()));
|
||||
JOptionPane.showMessageDialog(this, "转账成功。", "转账成功", JOptionPane.INFORMATION_MESSAGE);
|
||||
queryAccountAction();
|
||||
} catch (NumberFormatException ex) {
|
||||
JOptionPane.showMessageDialog(this, "无效的金额格式。", "转账错误", JOptionPane.ERROR_MESSAGE);
|
||||
} catch (Exception ex) {
|
||||
JOptionPane.showMessageDialog(this, ex.getMessage(), "转账错误", JOptionPane.ERROR_MESSAGE);
|
||||
}
|
||||
});
|
||||
|
||||
DeleteButton.addActionListener(e -> {
|
||||
String id = accountIdField.getText().trim();
|
||||
if (id.isEmpty()) {
|
||||
JOptionPane.showMessageDialog(this, "请输入要删除的账户ID。", "删除错误", JOptionPane.ERROR_MESSAGE);
|
||||
return;
|
||||
}
|
||||
int res = JOptionPane.showConfirmDialog(this, "确认删除账户 " + id + "?", "删除确认", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
|
||||
if (res == JOptionPane.YES_OPTION) {
|
||||
try {
|
||||
core.DeleteAccount(id);
|
||||
JOptionPane.showMessageDialog(this, "删除成功。", "删除成功", JOptionPane.INFORMATION_MESSAGE);
|
||||
accountIdField.setText("");
|
||||
clearFields();
|
||||
} catch (Exception ex) {
|
||||
JOptionPane.showMessageDialog(this, ex.getMessage(), "删除错误", JOptionPane.ERROR_MESSAGE);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void queryAccountAction() {
|
||||
String id = accountIdField.getText().trim();
|
||||
if (id.isEmpty()) {
|
||||
JOptionPane.showMessageDialog(this, "账户ID不能为空。", "查询错误", JOptionPane.ERROR_MESSAGE);
|
||||
clearFields();
|
||||
return;
|
||||
}
|
||||
try {
|
||||
Account acc = core.QueryAccount(id);
|
||||
NameField.setText(acc.getName());
|
||||
BalanceField.setText(String.format("%.2f", acc.getBalance()));
|
||||
} catch (Exception ex) {
|
||||
JOptionPane.showMessageDialog(this, ex.getMessage(), "查询错误", JOptionPane.ERROR_MESSAGE);
|
||||
clearFields();
|
||||
}
|
||||
}
|
||||
|
||||
private void clearFields() {
|
||||
NameField.setText("");
|
||||
BalanceField.setText("");
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user