用java实现声明银行账户类Account,成员变量包括账号、储户姓名、开户时间、身份证号码、存 款余额等账户信息,成员方法包括开户、存款、取款、查询(余额、明细)、销户等操作
时间: 2023-10-26 17:06:48 浏览: 265
这是一个关于银行存取款的程序。.rar_banking_java 银行_存取款操作_编程一个个人账户存取款_银行业务
5星 · 资源好评率100%
以下是用Java实现声明银行账户类Account的代码:
```java
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class Account {
private String accountNumber;
private String accountHolderName;
private Date accountOpeningDate;
private String idNumber;
private double balance;
private List<String> transactionHistory;
public Account(String accountNumber, String accountHolderName, String idNumber) {
this.accountNumber = accountNumber;
this.accountHolderName = accountHolderName;
this.idNumber = idNumber;
this.accountOpeningDate = new Date();
this.balance = 0;
this.transactionHistory = new ArrayList<>();
}
public void deposit(double amount) {
if (amount > 0) {
this.balance += amount;
String transaction = "Deposit: +" + amount + ", Balance: " + this.balance;
this.transactionHistory.add(transaction);
System.out.println("Deposit successful!");
} else {
System.out.println("Invalid deposit amount!");
}
}
public void withdraw(double amount) {
if (amount > 0 && amount <= this.balance) {
this.balance -= amount;
String transaction = "Withdrawal: -" + amount + ", Balance: " + this.balance;
this.transactionHistory.add(transaction);
System.out.println("Withdrawal successful!");
} else {
System.out.println("Invalid withdrawal amount or insufficient funds!");
}
}
public void checkBalance() {
System.out.println("Current balance: " + this.balance);
}
public void transactionHistory() {
for (String transaction : this.transactionHistory) {
System.out.println(transaction);
}
}
public void closeAccount() {
this.balance = 0;
this.transactionHistory.clear();
System.out.println("Account closed successfully!");
}
public String getAccountNumber() {
return accountNumber;
}
public String getAccountHolderName() {
return accountHolderName;
}
public Date getAccountOpeningDate() {
return accountOpeningDate;
}
public String getIdNumber() {
return idNumber;
}
}
```
在这个类中,我们定义了五个成员变量:账号、储户姓名、开户时间、身份证号码、存款余额和交易历史。在我们创建一个新的账户时,账号和储户姓名是必须的,开户时间会自动设置为当前时间,存款余额会初始化为0,交易历史会初始化为空列表。我们还定义了五个成员方法:存款、取款、查询余额、查询交易历史和销户。在存款和取款操作中,我们检查金额是否合法,如果合法则更新余额和交易历史。在查询余额和查询交易历史操作中,我们简单地打印出相应的信息。在销户操作中,我们清除余额和交易历史。
这是一个简单的银行账户类的实现,可以根据需要进行扩展和修改。
阅读全文