java银行存取款系统
时间: 2023-09-12 07:04:27 浏览: 106
好的,银行存取款系统是一个比较复杂的系统,需要设计多个类来实现不同的功能。这里给出一个简单的示例代码,帮助你了解如何实现一个基本的银行存取款系统。
首先,我们需要定义一个银行账户类(Account),用于记录用户的账户信息、余额和交易记录等。
```java
public class Account {
private String accountNumber; // 账号
private String accountName; // 账户名
private double balance; // 余额
private List<Transaction> transactions; // 交易记录
public Account(String accountNumber, String accountName, double balance) {
this.accountNumber = accountNumber;
this.accountName = accountName;
this.balance = balance;
this.transactions = new ArrayList<>();
}
public String getAccountNumber() {
return accountNumber;
}
public String getAccountName() {
return accountName;
}
public double getBalance() {
return balance;
}
public void deposit(double amount) {
balance += amount;
transactions.add(new Transaction(TransactionType.DEPOSIT, amount));
}
public void withdraw(double amount) {
if (balance < amount) {
throw new IllegalArgumentException("余额不足");
}
balance -= amount;
transactions.add(new Transaction(TransactionType.WITHDRAW, amount));
}
public List<Transaction> getTransactions() {
return transactions;
}
public enum TransactionType {
DEPOSIT,
WITHDRAW
}
public static class Transaction {
private TransactionType type;
private double amount;
public Transaction(TransactionType type, double amount) {
this.type = type;
this.amount = amount;
}
public TransactionType getType() {
return type;
}
public double getAmount() {
return amount;
}
}
}
```
接下来,我们需要定义一个银行类(Bank),用于管理所有的账户,并提供存取款等操作。
```java
public class Bank {
private Map<String, Account> accounts; // 所有账户
public Bank() {
this.accounts = new HashMap<>();
}
public void addAccount(Account account) {
accounts.put(account.getAccountNumber(), account);
}
public Account getAccount(String accountNumber) {
return accounts.get(accountNumber);
}
public void deposit(String accountNumber, double amount) {
Account account = getAccount(accountNumber);
if (account == null) {
throw new IllegalArgumentException("账户不存在");
}
account.deposit(amount);
}
public void withdraw(String accountNumber, double amount) {
Account account = getAccount(accountNumber);
if (account == null) {
throw new IllegalArgumentException("账户不存在");
}
account.withdraw(amount);
}
public List<Account> getAllAccounts() {
return new ArrayList<>(accounts.values());
}
}
```
最后,我们可以编写一个测试类(TestBank),用于演示如何使用银行类实现存取款操作。
```java
public class TestBank {
public static void main(String[] args) {
Bank bank = new Bank();
Account account1 = new Account("1001", "张三", 1000);
Account account2 = new Account("1002", "李四", 2000);
bank.addAccount(account1);
bank.addAccount(account2);
bank.deposit("1001", 500);
bank.withdraw("1002", 1000);
List<Account> accounts = bank.getAllAccounts();
for (Account account : accounts) {
System.out.println(account.getAccountNumber() + " " + account.getAccountName() + " " + account.getBalance());
List<Account.Transaction> transactions = account.getTransactions();
for (Account.Transaction transaction : transactions) {
System.out.println(transaction.getType().name() + " " + transaction.getAmount());
}
}
}
}
```
以上就是一个简单的银行存取款系统的实现。当然,实际的银行系统还需要考虑各种安全性、并发性等问题,这里仅提供一个基本的示例。
阅读全文