用java 实现一个银行账户管理系统:要求能够添加账户、删除账户、查询账户余额、存款、取款等功能。 */
时间: 2023-07-10 11:14:55 浏览: 130
好的,下面是用Java实现的银行账户管理系统。建议你将代码复制到Java IDE中并运行,以便更好地理解代码。
BankAccount类:
```
public class BankAccount {
private int accountNumber;
private String accountHolderName;
private double balance;
public BankAccount(int accountNumber, String accountHolderName, double balance) {
this.accountNumber = accountNumber;
this.accountHolderName = accountHolderName;
this.balance = balance;
}
public int getAccountNumber() {
return accountNumber;
}
public String getAccountHolderName() {
return accountHolderName;
}
public double getBalance() {
return balance;
}
public void deposit(double amount) {
balance += amount;
}
public void withdraw(double amount) {
if (balance >= amount) {
balance -= amount;
} else {
System.out.println("Insufficient balance");
}
}
@Override
public String toString() {
return "BankAccount{" +
"accountNumber=" + accountNumber +
", accountHolderName='" + accountHolderName + '\'' +
", balance=" + balance +
'}';
}
}
```
Bank类:
```
import java.util.ArrayList;
import java.util.List;
public class Bank {
private List<BankAccount> accounts;
public Bank() {
this.accounts = new ArrayList<>();
}
public void addAccount(BankAccount account) {
accounts.add(account);
}
public void removeAccount(int accountNumber) {
for (BankAccount account : accounts) {
if (account.getAccountNumber() == accountNumber) {
accounts.remove(account);
return;
}
}
System.out.println("Account not found");
}
public BankAccount getAccount(int accountNumber) {
for (BankAccount account : accounts) {
if (account.getAccountNumber() == accountNumber) {
return account;
}
}
System.out.println("Account not found");
return null;
}
@Override
public String toString() {
return "Bank{" +
"accounts=" + accounts +
'}';
}
public static void main(String[] args) {
Bank bank = new Bank();
BankAccount account1 = new BankAccount(1001, "Tom", 1000.0);
BankAccount account2 = new BankAccount(1002, "Jerry", 2000.0);
bank.addAccount(account1);
bank.addAccount(account2);
System.out.println(bank);
bank.removeAccount(1002);
System.out.println(bank);
BankAccount account3 = bank.getAccount(1001);
if (account3 != null) {
System.out.println("Tom's balance: " + account3.getBalance());
account3.deposit(500.0);
System.out.println("Tom's balance after deposit: " + account3.getBalance());
account3.withdraw(200.0);
System.out.println("Tom's balance after withdraw: " + account3.getBalance());
}
}
}
```
这里简单演示了如何使用这两个类。你可以根据自己的需求修改代码。
阅读全文