声明银行账户类Account,成员变量包括账号、储户姓名、开户时间、身份证号码、存款余额等账户信息,成员方法包括开户、存款、取款、查询(余额、明细)、销户等操作。Java操作
时间: 2023-09-12 10:04:52 浏览: 134
Java关于银行账户类
好的,以下是一个简单的Java实现:
```java
import java.util.ArrayList;
import java.util.Date;
public class Account {
private String accountNumber; // 账号
private String accountHolder; // 储户姓名
private Date openingDate; // 开户时间
private String idNumber; // 身份证号码
private double balance; // 存款余额
private ArrayList<String> transactionHistory; // 交易明细
// 构造函数
public Account(String accountNumber, String accountHolder, String idNumber) {
this.accountNumber = accountNumber;
this.accountHolder = accountHolder;
this.idNumber = idNumber;
this.openingDate = new Date(); // 开户时间为当前时间
this.balance = 0.0; // 初始存款余额为0
this.transactionHistory = new ArrayList<>();
}
// 存款
public void deposit(double amount) {
this.balance += amount;
this.transactionHistory.add("存款 " + amount + " 元");
}
// 取款
public void withdraw(double amount) {
if (this.balance < amount) {
System.out.println("余额不足,无法取款!");
return;
}
this.balance -= amount;
this.transactionHistory.add("取款 " + amount + " 元");
}
// 查询余额
public void checkBalance() {
System.out.println("账户余额为:" + this.balance + " 元");
}
// 查询交易明细
public void checkTransactionHistory() {
System.out.println("交易明细:");
for (String transaction : this.transactionHistory) {
System.out.println(transaction);
}
}
// 销户
public void closeAccount() {
this.accountNumber = null;
this.accountHolder = null;
this.idNumber = null;
this.balance = 0.0;
this.transactionHistory = null;
System.out.println("账户已销户!");
}
}
```
使用示例:
```java
public class Test {
public static void main(String[] args) {
Account account = new Account("1234567890", "张三", "123456789012345678");
account.deposit(1000.0);
account.withdraw(500.0);
account.checkBalance();
account.checkTransactionHistory();
account.closeAccount();
}
}
```
输出结果:
```
账户余额为:500.0 元
交易明细:
存款 1000.0 元
取款 500.0 元
账户已销户!
```
阅读全文