java面向对象实验 银行存储系统
概述:用gui实现界面,功能实现用户创建,存取款,转账。(死数据)
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class BankSystemGUI extends JFrame implements ActionListener {
private JLabel label1, label2, label3, label4;
private JTextField txtName, txtBalance, txtTo, txtAmount;
private JButton btnCreate, btnDeposit, btnWithdraw, btnTransfer;
private Bank bank;
public BankSystemGUI() {
setTitle("银行管理系统");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
bank = new Bank();
Container contentPane = getContentPane();
contentPane.setLayout(new FlowLayout());
JPanel jp1=new JPanel();
label1 = new JLabel("姓名:");
txtName = new JTextField(10);
contentPane.add(label1);
contentPane.add(txtName);
label2 = new JLabel("余额:");
txtBalance = new JTextField(10);
contentPane.add(label2);
contentPane.add(txtBalance);
btnCreate = new JButton("创建账户");
btnCreate.addActionListener(this);
jp1.add(label1);
jp1.add(txtName);
jp1.add(label2);
jp1.add(txtBalance);
jp1.add(btnCreate);
contentPane.add(jp1);
JPanel jp2=new JPanel();
label3 = new JLabel("转账对象:");
txtTo = new JTextField(10);
jp2.add(label3);
jp2.add(txtTo);
label4 = new JLabel("金额:");
txtAmount = new JTextField(10);
jp2.add(label4);
jp2.add(txtAmount);
contentPane.add(jp2);
JPanel jp3=new JPanel();
btnDeposit = new JButton("存款");
btnDeposit.addActionListener(this);
jp3.add(btnDeposit);
btnWithdraw = new JButton("取款");
btnWithdraw.addActionListener(this);
jp3.add(btnWithdraw);
btnTransfer = new JButton("转账");
btnTransfer.addActionListener(this);
jp3.add(btnTransfer);
contentPane.add(jp3);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnCreate) {
String name = txtName.getText();
double balance = Double.parseDouble(txtBalance.getText());
Account account = new Account(name, balance);
bank.addAccount(account);
JOptionPane.showMessageDialog(null, "账户已创建");
} else if (e.getSource() == btnDeposit) {
String name = txtName.getText();
double amount = Double.parseDouble(txtAmount.getText());
Account account = bank.getAccount(name);
if (account == null) {
JOptionPane.showMessageDialog(null, "账户不存在");
} else {
account.deposit(amount);
JOptionPane.showMessageDialog(null, "存款成功,当前余额为:" + account.getBalance());
}
} else if (e.getSource() == btnWithdraw) {
String name = txtName.getText();
double amount = Double.parseDouble(txtAmount.getText());
Account account = bank.getAccount(name);
if (account == null) {
JOptionPane.showMessageDialog(null, "账户不存在");
} else if (account.getBalance() < amount) {
JOptionPane.showMessageDialog(null, "余额不足");
} else {
account.withdraw(amount);
JOptionPane.showMessageDialog(null, "取款成功,当前余额为:" + account.getBalance());
}
} else if (e.getSource() == btnTransfer) {
String name = txtName.getText();
String to = txtTo.getText();
double amount = Double.parseDouble(txtAmount.getText());
Account account = bank.getAccount(name);
Account toAccount = bank.getAccount(to);
if (account == null || toAccount == null) {
JOptionPane.showMessageDialog(null, "账户不存在");
} else if (account.getBalance() < amount) {
JOptionPane.showMessageDialog(null, "余额不足");
} else {
account.transfer(toAccount, amount);
JOptionPane.showMessageDialog(null, "转账成功,当前余额为:" + account.getBalance());
}
}
}
public static void main(String[] args) {
new BankSystemGUI();
}
}
class Account {
private String name;
private double balance;
public Account(String name, double balance) {
this.name = name;
this.balance = balance;
}
public String getName() {
return name;
}
public double getBalance() {
return balance;
}
public void deposit(double amount) {
balance += amount;
}
public void withdraw(double amount) {
balance -= amount;
}
public void transfer(Account to, double amount) {
withdraw(amount);
to.deposit(amount);
}
}
class Bank {
private Account[] accounts;
private int numAccounts;
public Bank() {
accounts = new Account[100];
numAccounts = 0;
}
public void addAccount(Account account) {
accounts[numAccounts] = account;
numAccounts++;
}
public Account getAccount(String name) {
for (int i = 0; i < numAccounts; i++) {
if (accounts[i].getName().equals(name)) {
return accounts[i];
}
}
return null;
}
}