设计一个Bank类,实现银行某账户的资金往来账目管理。程序要求: 1)创建账户:包括账号、创建日期、余额 (创建账户时存入的钱数)。 2)存钱:执行存钱操作,并记录存钱日期和存钱数目。 3)取钱:执行取钱操作,并记录取钱日期和取钱数目。
时间: 2024-02-12 10:09:47 浏览: 191
以下是用Java设计的Bank类示例代码,实现银行账户的资金往来账目管理功能:
```java
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class Bank {
private int account; // 账户
private double balance; // 余额
private Date createTime; // 创建时间
private List<String> history; // 历史记录
// 构造函数
public Bank(int account, double balance) {
this.account = account;
this.balance = balance;
this.createTime = new Date();
this.history = new ArrayList<>();
}
// 存钱
public void deposit(double amount) {
balance += amount;
Date depositTime = new Date();
history.add("Deposit: " + amount + ", time: " + depositTime.toString());
}
// 取钱
public boolean withdraw(double amount) {
if (amount > balance) {
return false;
}
balance -= amount;
Date withdrawTime = new Date();
history.add("Withdraw: " + amount + ", time: " + withdrawTime.toString());
return true;
}
// 获取账户余额
public double getBalance() {
return balance;
}
// 获取账户创建时间
public Date getCreateTime() {
return createTime;
}
// 获取账户交易历史记录
public List<String> getHistory() {
return history;
}
public static void main(String[] args) {
// 创建账户
Bank account = new Bank(1001, 10000.0);
// 存钱
account.deposit(5000.0);
// 取钱
if (account.withdraw(2000.0)) {
System.out.println("Withdraw success!");
} else {
System.out.println("Withdraw failed!");
}
// 获取账户余额
System.out.println("Balance: " + account.getBalance());
// 获取账户创建时间
System.out.println("Create time: " + account.getCreateTime().toString());
// 获取账户交易历史记录
List<String> history = account.getHistory();
System.out.println("History: ");
for (String h : history) {
System.out.println(h);
}
}
}
```
注意,这只是一个简单的示例,实际的银行账户管理系统需要更加完善和安全。
阅读全文